note 99478 added to function.hexdec
| From: | hafees at msn dot com | Date: | Thu, 19 Aug 2010 10:31:00 +0000 |
| Subject: | note 99478 added to function.hexdec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171843@lists.php.net to get a copy of this message | ||
Use this function to convert a hexa decimal color code to its RGB equivalent. Unlike many other
functions provided here, it will work correctly with hex color short hand notation.
Also, if a proper hexa decimal color value is given (6 digits), it uses bit wise operations for
faster results.
For eg: #FFF and #FFFFFF will produce the same result
<?php
/**
* Convert a hexa decimal color code to its RGB equivalent
*
* @param string $hexStr (hexadecimal color value)
* @param boolean $returnAsString (if set true, returns the value separated by the separator
character. Otherwise returns associative array)
* @param string $seperator (to separate RGB values. Applicable only if second parameter is true.)
* @return array or string (depending on second parameter. Returns False if invalid hex color value)
*/
function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex
string
$rgbArray = array();
if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead...
faster
$colorVal = hexdec($hexStr);
$rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
$rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
$rgbArray['blue'] = 0xFF & $colorVal;
} elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
$rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
$rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
$rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
} else {
return false; //Invalid hex color code
}
return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or
the associative array
} ?>
OUTPUT:
hex2RGB("#FF0") -> array( red =>255, green => 255, blue => 0)
hex2RGB("#FFFF00) -> Same as above
hex2RGB("#FF0", true) -> 255,255,0
hex2RGB("#FF0", true, ":") -> 255:255:0
----
Server IP: 69.147.83.197
Probable Submitter: 121.242.90.162
----
Manual Page -- http://www.php.net/manual/en/function.hexdec.php
Edit -- https://master.php.net/note/edit/99478
Del: integrated -- https://master.php.net/note/delete/99478/integrated
Del: useless -- https://master.php.net/note/delete/99478/useless
Del: bad code -- https://master.php.net/note/delete/99478/bad+code
Del: spam -- https://master.php.net/note/delete/99478/spam
Del: non-english -- https://master.php.net/note/delete/99478/non-english
Del: in docs -- https://master.php.net/note/delete/99478/in+docs
Del: other reasons-- https://master.php.net/note/delete/99478
Reject -- https://master.php.net/note/reject/99478
Search -- https://master.php.net/manage/user-notes.php