问题
I need to convert CMYK values to RGBA values, but have yet to stumble upon an algorithm for this that just works.
I already have the RGBA to CMYK via the following:
- Convert RGBA to RGB
- Convert RGB to CMYK
Below are the algorithms I'm using for the above two bullet points:
RGBA to RGB
return substr($rgba, 0, -2);
... what? Nothing wrong with that! :D
RGB to CMYK
$c = 255 - $r;
$m = 255 - $g;
$y = 255 - $b;
$b = min($c, $m, $y);
$c = round(($c - $b) / (255 - $b));
$m = round(($m - $b) / (255 - $b));
$y = round(($y - $b) / (255 - $b));
$k = round($b / 255);
return sprintf('%s,%s,%s,%s', $c, $m, $y, $k);
On the return...
CMYK to RGB
$r = 255 - round(2.55 * ($c + $k));
$g = 255 - round(2.55 * ($m + $k));
$b = 255 - round(2.55 * ($y + $k));
if ($r < 0) $r = 0;
if ($g < 0) $g = 0;
if ($b < 0) $b = 0;
return sprintf('%s,%s,%s', $r, $g, $b);
RGB to RGBA
$min = min(array($r, $g, $b));
$a = (255 - $min) / 255;
$r = round(($r - $min) / $a);
$g = round(($g - $min) / $a);
$b = round(($b - $min) / $a);
$a = round($a);
return sprintf('%s,%s,%s,%s', $r, $g, $b, $a);
Now, doing the reverse, ie the previous two above, I get the output of converting CMYK to RGBA:
168,107,107,108
cmyk
13,0,0,1
rgba
Clearly, reversing this is not the best way to go about converting CMYK to RGBA. Perhaps my algorithm is completely wrong? It might be expecting percentages instead of values? I'm not entirely sure as colour profiles are not my strong point.
Is there an algorithm available (in any language - it can be translated) to assist with the correct conversion of CMYK values to RGBA?
来源:https://stackoverflow.com/questions/17234308/php-convert-cmyk-colour-values-to-rgba