Jump to content

Recommended Posts

I'm trying to convert a Hex colour value to RGB using a function I found at

http://www.anyexample.com/programming/php/php_convert_rgb_from_to_html_hex_color.xml

Here's the code:

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);
}

$textcolour = addslashes($_POST["textcolour"]);

$colouroftext = html2rgb($textcolour);

echo "Text colour: $textcolour <br />
$colouroftext[0],$colouroftext[1],$colouroftext[2]";

 

The problem is, when I try to print the RGB values, only $colouroftext[0] and $colouroftext[1] work. $colouroftext[2] keeps showing empty, and I can't figure out why. Any help would be greatly appreciated in solving this.

Thanks.

But it shouldn't be empty. Here's an example.

 

I input: 735856

 

The output ends up being:

115,88,

 

Where as it should be:

115,88,86

 

For some reason $colouroftext[2] is always returning empty when it shouldn't, which is why I'm asking for help, cause I really have no idea why.

Thanks again.

 

-Edit: Nevermind, I think I figured it out.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.