PHP: HEX to CMYK

后端 未结 2 1082
無奈伤痛
無奈伤痛 2021-02-11 01:09

How do I convert a HEX color value to CMYK equivalent in php?

I want to write a function that does that. But Ive got no clue how to convert hex to CMYK

e         


        
2条回答
  •  星月不相逢
    2021-02-11 01:52

    I just came across this because I was looking for a conversion script. However, the rgb2cmyk function in the answer by Mark Baker doesn’t seem to be calculating the correct values. I compared the results with multiple online calculators and in order to get the correct values I had to modify the function like this:

    function rgb2cmyk($var1,$g=0,$b=0) {
        if (is_array($var1)) {
                $r = $var1['r'];
                $g = $var1['g'];
                $b = $var1['b'];
        } else {
                $r = $var1;
        }
        $cyan = 1 - $r/255;
        $magenta = 1 - $g/255;
        $yellow = 1 - $b/255;
        $black = min($cyan, $magenta, $yellow);
        $cyan = @round(($cyan - $black) / (1 - $black) * 100);
        $magenta = @round(($magenta - $black) / (1 - $black) * 100);
        $yellow = @round(($yellow - $black) / (1 - $black) * 100);
        $black = round($black * 100);
        return array(
                'c' => $cyan,
                'm' => $magenta,
                'y' => $yellow,
                'k' => $black,
        );
    }
    

    Now, I don’t fully understand what I did here but it appears to output the correct values (compared to all the other online calculators).

提交回复
热议问题