Convert RGB Color Value To Hexadecimal

A simple way to convert RGB colors to their hexadecimal values by using the dechex() function.
function RGBToHex($RGBValues)
{
        $HexValue = "";
        foreach ($RGBValues as $RGBValue)
        {
                $HexValue .= (strlen(dechex($RGBValue)) == 1) ? '0' . dechex($RGBValue) : dechex($RGBValue);
        }
        return strtoupper($HexValue);
}
 
// How to use the function to convert an RGB color to hex
echo RGBToHex(array('R'=>255, 'G'=>202, 'B'=>91));

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top