note 105637 deleted from function.hexdec by danbrown

From: Date: Fri, 02 Sep 2011 17:34:13 +0000
Subject: note 105637 deleted from function.hexdec by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182908@lists.php.net to get a copy of this message
Note Submitter: chanerec at gmail dot com ---- <?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 } ?>

« previous php.notes (#182908) next »