Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   RGB To Hex To RGB Conversion (http://www.go4expert.com/articles/rgb-hex-rgb-conversion-t5846/)

pradeep 14Aug2007 13:10

RGB To Hex To RGB Conversion
 

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"


gkumar 3Jul2009 15:02

Re: RGB To Hex To RGB Conversion
 
This coding convert the RGB value into HEXA value
Code:

function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}


This coding convert the HEXA value into RGB value

Code:

R = HexToR("#FFFFFF");
G = HexToG("#FFFFFF");
B = HexToB("#FFFFFF");

function HexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function HexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function HexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}


LenoxFinlay 22Jul2009 14:59

Re: RGB To Hex To RGB Conversion
 
1) from RGB to Hex value
Put RGB values into the text field R, G, and B. Leave the mouse into the one of text fields. And hit the return key. RGB values must be between 0 to 255.

2) from Hex to RGB value

Put Hex value into the text field Hex. Hex value must be 6 digits of "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F". Leave the mouse into the text field Hex. And hit the return key.

avneeshatri 22Aug2010 08:31

Re: RGB To Hex To RGB Conversion
 
i ned to make a invisibel watermarking program in java . how can i use this concept in my project.plz help


All times are GMT +5.5. The time now is 09:33.