note 105637 added to function.hexdec
| From: | chanerec at gmail dot com | Date: | Fri, 02 Sep 2011 17:33:31 +0000 |
| Subject: | note 105637 added to function.hexdec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182907@lists.php.net to get a copy of this message | ||
<?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)
* @link: https://gist.github.com/1189016
* @base-link: http://www.php.net/manual/en/function.hexdec.php#99478
*/
function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
# Normalize a proper hex string with length 6
$hexStr = preg_replace("/[^0123456789abcdef]/", '',strtolower($hexStr));
if( strlen($hexStr) == 3 )
$hexStr = preg_replace("/(.)(.)(.)/", '\1\1\2\2\3\3', $hexStr);
#converHex2RGB
if (preg_match('/^[0123456789abcdef]{6}$/',$hexStr)) {
$rgbResult=array();
#2 characters will correspond to each color
foreach(array('red','green','blue') as $str_pos=>$color_index)
$rgbResult[$color_index]=hexdec(substr($hexStr,2*$str_pos,2));
#return array o string depending on second paramenter
return $returnAsString ? implode($seperator, $rgbResult) : $rgbResult;
}
return false; //Invalid hex color code
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 189.130.16.152
----
Manual Page -- http://www.php.net/manual/en/function.hexdec.php
Edit -- https://master.php.net/note/edit/105637
Del: integrated -- https://master.php.net/note/delete/105637/integrated
Del: useless -- https://master.php.net/note/delete/105637/useless
Del: bad code -- https://master.php.net/note/delete/105637/bad+code
Del: spam -- https://master.php.net/note/delete/105637/spam
Del: non-english -- https://master.php.net/note/delete/105637/non-english
Del: in docs -- https://master.php.net/note/delete/105637/in+docs
Del: other reasons-- https://master.php.net/note/delete/105637
Reject -- https://master.php.net/note/reject/105637
Search -- https://master.php.net/manage/user-notes.php