• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Style Color... how to convert rgb() to hex

Chris D

XenForo developer
Staff member
#2
You might have to write a custom class for this...

There are some colour related helpers but the end result is always RGB or RGBA.

This one may help:

Code:
function rgb2hex($rgb)
{
$hex = "#";​
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);​
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);​
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);​
return $hex; // returns the hex value including the number sign (#)​
}
 

Mr. Goodie2Shoes

Well-known member
#5
oh... forgot to mention, the input is actually an array... so you have to separate them by
PHP:
$rgb = str_replace(' ', '', $rgb) //just to make sure...
$rgb = explode(',', $rgb);