PHP: HEX for CMYK

How to convert HEX color value to CMYK equivalent in php?

I want to write a function that does this. But I did not understand how to convert hex to CMYK

eg: 
<?php

hex2CMYK('#000000'); //result: array('0.0','0.0','0.0','0.0');

?>
+3
source share
1 answer
function hex2rgb($hex) {
   $color = str_replace('#','',$hex);
   $rgb = array(
      'r' => hexdec(substr($color,0,2)),
      'g' => hexdec(substr($color,2,2)),
      'b' => hexdec(substr($color,4,2)),
   );
   return $rgb;
}

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

$color=rgb2cmyk(hex2rgb('#FF0000'));
+8
source

Source: https://habr.com/ru/post/1756537/


All Articles