Introduction
The RGB color model is an additive model in which red, green, and blue (often used in additive light models) are combined in various ways to reproduce other colors. The name of the model and the abbreviation ‘RGB’ come from the three primary colors, red, green, and blue and the technological development of cathode ray tubes which could display color instead of a monochrome phosphoresence (including grey scaling) such as black and white film and television imaging.
A hex triplet is a six-digit, three-byte hexadecimal number used in HTML and CSS, and other computing applications, to represent colors. The bytes represent the red, green and blue components of the color. One byte represents a number in the range 00 to FF (in hexadecimal notation), or 0 to 255 in decimal notation. This represents the least (0) to the most (255) intensity of each of the color components. The hex triplet is formed by concatenating three bytes in hexadecimal notation, in the following order:
Byte 1: red value
Byte 2: green value
Byte 3: blue value
For example, consider the color where the red/green/blue values are decimal numbers: red=36, green=104, blue=160 (a greyish-blue color). The decimal numbers 36, 104 and 160 are 24, 68 and A0 respectively in hexadecimal notation. To obtain the hex triplet, we simply write the three hex bytes together without spaces, thus: 2468A0. If a byte is less than 16 (decimal) or 10 (hex) it must be represented with a leading zero to keep the number of digits in the triplet equal to six. For example, the decimal triplet 0,1,2 would be represented by the hex triplet 000102.
Algorithm
Converting a decimal RGB value to a hexadecimal value is fairly straightforward. RGB values are usually given in the 0-255 range; if they are in the 0-1 range, multiply the value by 255 before conversion. The number of times this number divides into 16 (without rounding) gives us the first hexadecimal digit (between 0 and F, see hexadecimal if unsure). The remainder gives us the second digit. For instance the RGB value 201 divides 12 times into 16, thus the first digit is C. A remainder of 9 gives us the hexadecimal number C9. This process is repeated for each color value. Most modern operating systems have a built-in calculator to perform conversions between number bases; this can also be done with some calculators.
The algorithm is reversed for Hex to RGB.
Implementation
Code: PHP
function RGB2Hex2RGB($c)
{
if(!$c)
return false;
$c = trim($c);
$out = false;
if(eregi("^[0-9ABCDEFabcdef\#]+$", $c))
{
$c = str_replace('#','', $c);
$l = strlen($c);
if($l == 3)
{
unset($out);
$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,1));
$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1,1));
$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2,1));
}
elseif($l == 6)
{
unset($out);
$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,2));
$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 2,2));
$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 4,2));
}
else
$out = false;
}
elseif (eregi("^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$", $c))
{
if(eregi(",", $c))
$e = explode(",",$c);
else if(eregi(" ", $c))
$e = explode(" ",$c);
else if(eregi(".", $c))
$e = explode(".",$c);
else return false;
if(count($e) != 3)
return false;
$out = '#';
for($i = 0; $i<3; $i++)
$e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i]));
for($i = 0; $i<3; $i++)
$out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i];
$out = strtoupper($out);
}
else
$out = false;
return $out;
}
Example Usage
Code: PHP
var_dump(RGB2Hex2RGB('#FFFFFF'))."\n";
var_dump(RGB2Hex2RGB('100,100,250'))."\n";
var_dump(RGB2Hex2RGB('255.255.255'))."\n";
Output:
Code:
array(9) {
["red"]=>
int(255)
["r"]=>
int(255)
[0]=>
int(255)
["green"]=>
int(255)
["g"]=>
int(255)
[1]=>
int(255)
["blue"]=>
int(255)
["b"]=>
int(255)
[2]=>
int(255)
}
string(7) "#6464FA"
string(7) "#FFFFFF"