1

I googled about a function how to convert hex to rgb color code

<?php
function html2rgb($color)
{
    if ($color[0] == '#')
        $color = substr($color, 1);

    if (strlen($color) == 6)
        list($r, $g, $b) = array($color[0].$color[1],
                                 $color[2].$color[3],
                                 $color[4].$color[5]);
    elseif (strlen($color) == 3)
        list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
    else
        return false;

    $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

    return array($r, $g, $b);
}
?>

I can't access the data like this echo html2rgb('#cccccc'); because it's an array

// Edit I just want to say thanks to the guys how answered. :)

Ben
  • 1,906
  • 10
  • 31
  • 47

3 Answers3

2

well you can access it like this:

$rgb = html2rgb('#cccccc');
$r = $rgb[0];
$g = $rgb[1];
$b = $rgb[2];

and then

echo "Red = $r, Green = $g, Blue = $b";

or just var_dump($rgb) or print_r($rgb)

Stefan
  • 2,028
  • 2
  • 36
  • 53
1

try print_r(html2rgb('#cccccc'));

This should help you understand: What's the difference between echo, print, and print_r in PHP?

Community
  • 1
  • 1
Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
  • Okey i have Array ( [0] => 204 [1] => 204 [2] => 204 ) but how to echo every one of them – Ben Mar 17 '12 at 22:15
1

I guess you'd want something more along the lines of:

$cc = html2rgb('#cccccc');
echo "[".$cc[0].",".$cc[1].",".$cc[2]."]";
Kaustubh Karkare
  • 1,083
  • 9
  • 25