取色器

package com.demo.palettetest;

import android.graphics.Color;

public class ColorUtils {
    public static int parseBackgroundColor2(int color) {
        int counter = 0;
        counter += Color.red(color) >= 128 ? 1 : 0;
        counter += Color.green(color) >= 128 ? 1 : 0;
        counter += Color.blue(color) >= 128 ? 1 : 0;
        return counter >= 2 ? Color.BLACK : Color.WHITE;
    }

    // 通过分析背景色来决定当前文字的匹配颜色,使文字颜色自适应背景颜色
    public static int parseBackgroundColor(int color) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        if (red >= 128 && green >= 128      // 三选二
                || red >= 128 && blue >= 128
                || green >= 128 && blue >= 128) {
            return Color.rgb(0, 0, 0);
        }
        return Color.rgb(255, 255, 255);
    }

    // #FF55FF => color
    // int color = Color.parseColor("#b64242");

    // color -> #FF55FF
    public static String toRGBHexString(final int color) {
        return toRGBHexString(Color.red(color), Color.green(color), Color.blue(color));
    }

    // (r,g,b) -> #FF55FF
    public static String toRGBHexString(int red, int green, int blue) {
        return toARGBHexString(-1, red, green, blue);
    }

    // default prefix: "#"
    // (a,r,g,b) -> #FF55FF55
    public static String toARGBHexString(int alpha, int red, int green, int blue) {
        return toARGBHexString("#", alpha, red, green, blue);
    }

    public static String toARGBHexString(String prefix, int alpha, int red, int green, int blue) {
        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        if (alpha != -1) {
            String mAlphaStr = Integer.toHexString(alpha);
            sb.append(mAlphaStr.length() == 1 ? "0" + mAlphaStr : mAlphaStr);
        }
        String mRedStr = Integer.toHexString(red);
        sb.append(mRedStr.length() == 1 ? "0" + mRedStr : mRedStr);
        String mGreenStr = Integer.toHexString(green);
        sb.append(mGreenStr.length() == 1 ? "0" + mGreenStr : mGreenStr);
        String mBlueStr = Integer.toHexString(blue);
        sb.append(mBlueStr.length() == 1 ? "0" + mBlueStr : mBlueStr);
        return sb.toString().toUpperCase();
    }
}
package com.demo.palettetest

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.content.ContextCompat
import android.support.v7.graphics.Palette
import android.widget.Button
import android.widget.TextView
import com.facebook.binaryresource.FileBinaryResource
import com.facebook.cache.common.SimpleCacheKey
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.view.SimpleDraweeView

class MainActivity : AppCompatActivity() {
    private var interImg: SimpleDraweeView? = null
    private var cacheImg: SimpleDraweeView? = null
    private var bgImg: SimpleDraweeView? = null
    private var tv1: TextView?= null
    private var tv3: TextView?= null
    private var tv2: TextView?= null
    private var tv4: TextView?= null
    private var tv5: TextView?= null
    private var tv6: TextView?= null
    private var tv7: TextView?= null
    private var tv8: TextView?= null
    private var btn: Button? = null
    private val imgUrl = "http://d.lanrentuku.com/down/png/1712/22xiaodongwu/22xiaodongwu_12.png"
    private var count = 1
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Fresco.initialize(this)//初始化Fresco
        setContentView(R.layout.activity_main)
        interImg = findViewById(R.id.interImg)//网络请求的图片的id
        cacheImg = findViewById(R.id.cacheImg)//从本地加载的图片的id
        bgImg = findViewById(R.id.bgImg)
        tv1 = findViewById(R.id.tv1)
        tv2 = findViewById(R.id.tv2)
        tv3 = findViewById(R.id.tv3)
        tv4 = findViewById(R.id.tv4)
        tv5 = findViewById(R.id.tv5)
        tv6 = findViewById(R.id.tv6)
        tv7 = findViewById(R.id.tv7)
        tv8 = findViewById(R.id.tv8)
        btn = findViewById(R.id.btnChange)
        btn?.setOnClickListener {
            if (count <= 5) {
                setColor(count)
                count ++
            } else {
                count = 1
                setColor(count)
            }
        }
        val parse = Uri.parse(imgUrl)
        interImg?.setImageURI(parse)
        /**         * 获取缓存之后的图片
         * */
       /* if (parse != null) {
            val resource = Fresco.getImagePipelineFactory().mainFileCache.getResource(SimpleCacheKey(parse.toString())) as FileBinaryResource
            val file = resource.file
            *//**         * 将文件转为uri形式          *//*
            val uri = Uri.fromFile(file)
            cacheImg!!.setImageURI(uri)
        }*/

        setColor(count)
    }

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    fun setColor(count: Int){
        val bgDraw = when(count){
            1 -> R.drawable.a1
            2 -> R.drawable.a2
            3 -> R.drawable.a3
            4 -> R.drawable.a4
            else -> R.drawable.a5
        }
        val bitmap = BitmapFactory.decodeResource(resources, bgDraw)
        bgImg?.setActualImageResource(bgDraw)
        Palette.from(bitmap?:return).generate {
            tv1?.setTextColor(it.getDominantColor(Color.WHITE))
            tv2?.setTextColor(it.getLightVibrantColor(Color.WHITE))
            tv3?.setTextColor(it.getMutedColor(Color.WHITE))
            tv4?.setTextColor(it.getDarkMutedColor(Color.WHITE))
            tv5?.setTextColor(it.getDarkVibrantColor(Color.WHITE))
            tv6?.setTextColor(it.getLightMutedColor(Color.WHITE))
            tv7?.setTextColor(it.getVibrantColor(Color.WHITE))
            tv8?.setTextColor(it.dominantSwatch?.titleTextColor?:return@generate)
        }
    }

    private fun getBitmap(uri: String?): Bitmap?{
        if (uri == null) return null
        var bitmap: Bitmap?= null
        try {
            val resource = Fresco.getImagePipelineFactory().mainFileCache.getResource(SimpleCacheKey(uri)) as FileBinaryResource
            val file = resource.file
            bitmap = BitmapFactory.decodeFile(file.path)
        } catch (e: Exception){

        }
        return bitmap
    }
}


     
    

         
    
    
    

    

    

    

    

    
        
    
    
    

你可能感兴趣的:(取色器)