1

I am getting RGB Values using this php code

<?php
$im = imagecreatefrompng("img/t4.png");
$rgb = imagecolorat($im, 15, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
?>

And i need CMYK percentages also

Can someone please give me som guide lines.

Thanks.

Jimbo
  • 25,790
  • 15
  • 86
  • 131
sridhar
  • 93
  • 3
  • 11

1 Answers1

2
<?
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)) * 255;
   $magenta = @(($magenta - $black) / (255 - $black)) * 255;
   $yellow  = @(($yellow  - $black) / (255 - $black)) * 255;
   return array('c' => $cyan / 255,
                'm' => $magenta / 255,
                'y' => $yellow / 255,
                'k' => $black / 255);
}

$color=rgb2cmyk(hex2rgb('#FF0000')); 
pdf_setcolor($pdf, "both", "cmyk", $color['c'], $color['m'], $color['y'], $color['k']);
?>
Sushant Khurana
  • 843
  • 1
  • 10
  • 13
  • This is a good answer! The fine detail about this is, that this function already performs UCR ("under color removal") which replaces equal shares of color with black. This generally leads to more "neutral" colors, especially grey tones often lose their color tint this way. – flomei Dec 07 '17 at 10:48