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. :)

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

Okey i have Array ( [0] => 204 [1] => 204 [2] => 204 ) but how to echo every one of them
1

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

$cc = html2rgb('#cccccc');
echo "[".$cc[0].",".$cc[1].",".$cc[2]."]";

1 Comment

echo "[$cc[0], $cc[1], $cc[2]]"; <-- Would be better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.