0

I would like to display RGB value of an images as percentage.

For example, if hex color is #000000, How do I display it as % code (percentage) like rgb(0%,0%,0%)?

1

1 Answer 1

0

try this,

function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb); // returns the rgb values separated by commas
   return $rgb; // returns an array with the rgb values
}

OUTPUT:

Array ( [0] => 204 [1] => 204 [2] => 0 )
//rgb(204,204,0)

DEMO

Not the answer you're looking for? Browse other questions tagged or ask your own question.