颜色RGB和十六进制和HSV转换

1、RGB转十六进制

/*
     *转16进制数 //十六进制的颜色字符串
     */
    private fun toBrowserHexValue(number: Int): String {
        val builder = StringBuilder(Integer.toHexString(number and 0xff))
        while (builder.length < 2) {
            builder.append("0")
        }
        return builder.toString().toUpperCase()
    }

 //十六进制的颜色字符串
 var colorValue = "#" + toBrowserHexValue(r) + toBrowserHexValue(g) + toBrowserHexValue(b)
 Log.i("打印选择的颜色","$colorValue")

 2、十六进制转RGB

 Log.i("打印选择的值:","$colorValue")//#FF00FF
        var str = colorValue
        var str2 = str.substring(1,3)
        var str3 = str.substring(3,5)
        var str4 = str.substring(5,7)
        var red = Integer.parseInt(str2,16)
        var green = Integer.parseInt(str3,16)
        var blue = Integer.parseInt(str4,16)
        Log.i("打印十六进制转颜色值","r=$red,g=$green,b=$blue")

 3、RGB转HSV

 // 将 rgb 转换成 hsv,s 即为饱和度  色调(H),饱和度(S),明度(V)
        val c = Color.argb(255, red, green, blue)
        val hsv = FloatArray(3)
        Color.colorToHSV(c, hsv)
        Log.i("打印选择的值","R=${hsv[0]} ,G=${hsv[1]} ,B=${hsv[2]}")
        //var blue = hsv[2]

 4、HSV转RGB

 public static void HSVToRGB(float hsv[], @Size(3) float rgb[]){
        int color=Color.HSVToColor(hsv);
        rgb[0]=Color.red(color);
        rgb[1]=Color.green(color);
        rgb[2]=Color.blue(color);
        //  Color.colorToHSV();
    }

  5、通过RGB获取色温值

public static double setRGBToTemperature(int rgb_R,int rgb_G,int rgb_B) {
        double trimX = 0;
        double trimY = 0;
        double trimZ = 0;
        double coorX = 0, coorY = 0;
        double CCT = 0;
        double n = 0;
        int R = rgb_R;//255;
        int G = rgb_G;//231;
        int B = rgb_B;//131;
        //以下公式实现RGB转三刺激值
        trimX = 2.789 * R + 1.7517 * G + 1.1302 * B;
        trimY = 1 * R + 4.5907 * G + 0.0601 * B;
        trimZ = 0 * R + 0.0565 * G + 5.5943 * B;
        //以下公式实现三刺激值转色坐标
        coorX = trimX / (trimX + trimY + trimZ);
        coorY = trimY / (trimX + trimY + trimZ);
        n = (coorX - 0.3320) / (0.1858 - coorY);
        //以下公式实现色坐标转色温
                CCT = 437 * n * n * n + 3601 * n * n + 6831 * n + 5517;
        System.out.println("X:" + trimX + "Y:" + trimY + "Z:" + trimZ+"\n CCT:"+CCT);
        return CCT;
    }


//调用
 val rgb = MyColorUtils.setRGBToTemperature(r,g,b)
 Log.i("RGB转色温值后:","$rgb")
 colorTempValue = "${(rgb).toInt()}K"

 6、色温值转RGB

/**
     A temperature to color conversion, inspired from a blogpost from PhotoDemon
     (http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/).
     @param temperature The temperature of an ideal black body, in Kelvins;
     @param alpha       If true, the return value will be RGBA instead of RGB.
     @return The corresponding RGB color.
     */
    public static int[] getRgbFromTemperature(double temperature, boolean alpha)
    {
        // Temperature must fit between 1000 and 40000 degrees
        temperature = MathUtils.clamp(temperature, 1000, 40000);

        // All calculations require tmpKelvin \ 100, so only do the conversion once
        temperature /= 100;

        // Compute each color in turn.
        int red, green, blue;
        // First: red
        if (temperature <= 66)
            red = 255;
        else
        {
            // Note: the R-squared value for this approximation is .988
            red = (int) (329.698727446 * (Math.pow(temperature - 60, -0.1332047592)));
            red = MathUtils.clamp(red, 0, 255);
        }

        // Second: green
        if (temperature <= 66)
            // Note: the R-squared value for this approximation is .996
            green = (int) (99.4708025861 * Math.log(temperature) - 161.1195681661);
        else
            // Note: the R-squared value for this approximation is .987
            green = (int) (288.1221695283 * (Math.pow(temperature - 60, -0.0755148492)));

        green = MathUtils.clamp(green, 0, 255);

        // Third: blue
        if (temperature >= 66)
            blue = 255;
        else if (temperature <= 19)
            blue = 0;
        else
        {
            // Note: the R-squared value for this approximation is .998
            blue = (int) (138.5177312231 * Math.log(temperature - 10) - 305.0447927307);

            blue = MathUtils.clamp(blue, 0, 255);
        }

        if (alpha)
            return new int[]
                    {
                            red, green, blue, 255
                    };
        else
            return new int[]
                    {
                            red, green, blue
                    };
    }

  调用

fun setCenterColorFromColorTemp(color: String){//传入:2700K

        var zhi = color.replace("K", "").toDouble()

        Log.i("打印选择的值1:","$zhi")
        var rgb = IntArray(3)
        rgb = MyColorUtils.getRgbFromTemperature(zhi,false)
        Log.i("打印选择的值2","R=${rgb[0]} ,G=${rgb[1]} ,B=${rgb[2]}")
        var blue = rgb[2]

        val c = Color.argb(255,rgb[0], rgb[1], rgb[2])
        Log.i("打印选择的值3","c=${c}")
    }

  

 

 

posted on 2022-03-10 17:54  巫山老妖  阅读(1621)  评论(0编辑  收藏  举报