PHP - Convert CMYK colour values to RGBA

有些话、适合烂在心里 提交于 2019-12-08 08:38:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!