/**
- * Hex RGB color value interchange
- * Edit bbs.it-home.org
- * At 2013/5/11
- */
- Function HexToRGB ($ color ){
- If ($ color [0] = '#'){
- $ Color = substr ($ color, 1 );
- }
- If (strlen ($ color) = 6 ){
- List ($ r, $ g, $ B) = array (
- $ Color [0]. $ color [1],
- $ Color [2]. $ color [3],
- $ Color [4]. $ color [5]
- );
- } Elseif (strlen ($ color) = 3 ){
- List ($ r, $ g, $ B) = array (
- $ Color [0]. $ color [0],
- $ Color [1]. $ color [1],
- $ Color [2]. $ color [2]
- );
- } Else {
- Return false;
- }
- $ R = hexdec ($ r );
- $ G = hexdec ($ g );
- $ B = hexdec ($ B );
- Return array (
- 'Red' => $ r,
- 'Green' => $ g,
- 'Blue' => $ B
- );
- }
- Function RGBToHex ($ rgb ){
- $ Regexp = "/^ rgb \ ([0-9] {}) \, \ s * ([0-9 })\, \ s * ([0-9] {0, 3 })\)/";
- $ Re = preg_match ($ regexp, $ rgb, $ match );
- $ Re = array_shift ($ match );
- $ HexColor = "#";
- $ Hex = array (
- '0 ',
- '1 ',
- '2 ',
- '3 ',
- '4 ',
- '5 ',
- '6 ',
- '7 ',
- '8 ',
- '9 ',
- 'A ',
- 'B ',
- 'C ',
- 'D ',
- 'E ',
- 'F'
- );
- For ($ I = 0; $ I <3; $ I ++ ){
- $ R = null;
- $ C = $ match [$ I];
- $ HexAr = array ();
While ($ c> 16 ){
- $ R = $ c % 16;
- $ C = ($ c/16)> 0;
- Array_push ($ hexAr, $ hex [$ r]);
- }
- Array_push ($ hexAr, $ hex [$ c]);
$ Ret = array_reverse ($ hexAr );
- $ Item = implode ('', $ ret );
- $ Item = str_pad ($ item, 2, '0', STR_PAD_LEFT );
- $ HexColor. = $ item;
- }
- Return $ hexColor;
- }
// Call example
- Echo implode (",", HexToRGB ("# F7F7DA "))."
";
- Echo RGBToHex ("rgb (247,247,218 )");
- ?>
|