I’m having toubles with converting triple HEX color codes in an RGB color code.
What I’ve got so far for HEX to RGB is:
if(strlen($hex) == 3) {
$color['r'] = hexdec(substr($hex, 0, 1) . $r);
$color['g'] = hexdec(substr($hex, 1, 1) . $g);
$color['b'] = hexdec(substr($hex, 2, 1) . $b);
}
When I convert the RGB code back to HEX it’s a different one.
E.g.: #FFF becomes 15, 15, 15 but 15, 15, 15 is #0F0F0F
I’m also not sure about converting RGB back to triple HEX code. My code for RGB to HEX looks like this:
$hex = str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
$hex.= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
$hex.= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
Any help is greatly appreciated! Thanks in advance!
It looks like you need to handle triplets in a different way: #XYZ = #XXYYZZ. #FFF should be, for example, the same as #FFFFFF, or well, (255, 255, 255), instead of (15, 15, 15).
So, a way to do this is with the following code:
Note I’m not including $r, $g and $b, as I don’t know why are you using them.