1. 程式人生 > 其它 >顏色RGB和十六進位制和HSV轉換

顏色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}")
    }