RGB To Hexadecimal
This algorithm converts RGB color model to hexadecimal color code.
class RGB
{
public $R;
public $G;
public $B;
}
function DecimalToHexadecimal($dec)
{
if ($dec < 1) return "00";
$hex = $dec;
$hexStr = "";
while ($dec > 0)
{
$hex = $dec % 16;
if ($hex < 10)
$hexStr = substr_replace($hexStr, chr($hex + 48), 0, 0);
else
$hexStr = substr_replace($hexStr, chr($hex + 55), 0, 0);
$dec = floor($dec / 16);
}
return $hexStr;
}
function RGBToHexadecimal($rgb) {
$rs = DecimalToHexadecimal($rgb->R);
$gs = DecimalToHexadecimal($rgb->G);
$bs = DecimalToHexadecimal($rgb->B);
return "#" . $rs . $gs . $bs;
}
Example
$data = new RGB();
$data->R = 82;
$data->G = 0;
$data->B = 87;
$value = RGBToHexadecimal($data);
Output
#520057