|
deep inside webpages
 


Getting Red-Green-Blue from Hexadecimal Colors - PHP Bitwise

When working with CSS, it is common to use Hexadecimal Notation of colors, while on the back end, many PHP functions (like
imagecolorallocate
,
imagecolorclosest
,
imagecolorexact
etc, require that you pass on colors as red, green and blue
int
egers.

The below function can be helpful if you need to convert hexadecimal color literal to red, green and blue
int
values for use elsewhere. This function accepts hex, converts to RGB and stores the respective color values in
$red
,
$green
and
$blue
variables passed by reference.
  1. function get_rgb( $color , &$red , &$green , &$blue ) {
  2.    
  3.     if( ( ( int ) $color ) !== $color ) {
  4.         //$color is string
  5.         $color  = trim( $color , " \t\n\r#" );
  6.        
  7.         if( ctype_xdigit( $color ) && strlen( $color ) === 6 ) {
  8.             //Convert to int
  9.             $color  = ( int ) hexdec( $color );
  10.         } else {
  11.             return false;
  12.         }
  13.     }
  14.    
  15.     $red    = ( $color >> 16 ) & 0xFF;    //Red is the Left Most Byte
  16.     $green  = ( $color >> 8 ) & 0xFF;     //Green is the Middle Byte
  17.     $blue   = $color & 0xFF;              //Blue is the Right Most Byte
  18.    
  19.     return true;
  20. }
PHP Code

The
& 0xFF
part in
$red = ( $color >> 16 ) & 0xff;
ensures that if input color is an integer (or hexadecimal integer literal) and it exceeds
0xFFFFFF
(24
ON
bits), the part of the integer exceeding the limit is overlooked and does not affect our RGB values.
strlen( $color ) === 6
implements the same for
string
color literals.

trim($color," \t\n\r#")
removes all spaces, new line characters (Windows, Mac and Linux), tab characters and
#
character from around the color hex.

The function
return
s
true
on success and
false
on failure caused by invalid color passed to the function. So, before using
$red
,
$green
and
$blue
variables in your script, its better to apply an
if
check on the
return
value of the function:
  1. if( get_rgb( $color , $red , $green , $blue ) ) {
  2.     work_with_colors( $red , $green , $blue );
  3. }
PHP Code

Tests

get_rgb()
was tested with three different input patterns
  1. Hexadecimal representation of integers:
    0xFF9933
    , or simply
    int
    egers;
  2. Hexadecimal Color String, starting with
    #
    sign:
    '#FF9933'
    ;
  3. Hexadecimal Color String, without any
    #
    sign:
    'FF9933'
    ;
  1. //Test - 1
  2. $color  = 0xFF9933;
  3. get_rgb( $color , $red , $green , $blue );
  4.  
  5. //Test - 2
  6. $color  = '#FF9933';
  7. get_rgb( $color , $red , $green , $blue );
  8.  
  9. //Test - 3
  10. $color  = 'FF9933';
  11. get_rgb( $color , $red , $green , $blue );
  12.  
  13. //Same result for all of the above tests:
  14. echo $red;      //255
  15. echo $green;    //153
  16. echo $blue;     //51
PHP Code

Rate this script on Scripts.com



  1. Faster Random Numbers with PHP
  2. Implementing logical right shifting in PHP
  3. Finding Even or Odd numbers with PHP