Jump to content

[SOLVED] Trouble with Arrays, output of function


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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.