note 51270 deleted from function.hexdec by betz
| From: | betz@php.net | Date: | Fri, 25 Mar 2005 11:35:32 +0000 |
| Subject: | note 51270 deleted from function.hexdec by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-86938@lists.php.net to get a copy of this message | ||
Note Submitter: zubfatal, root at it dot dk
----
A small 'convert color' class ..
<?php
/**
* Convert colors
*
* Usage:
* color::hex2rgb("FFFFFF")
* color::rgb2hex(array(171,37,37))
*
* @author Tim Johannessen <root@it.dk>
* @version 1.0.0
*/
class color {
/**
* Convert HEX colorcode to an array of colors.
*
* @author Tim Johannessen <admin@stillborn.dk>
* @return array Returns the array of colors as array(red,green,blue)
*/
function hex2rgb($hexVal = "") {
$hexVal = eregi_replace("[^a-zA-Z0-9]", "", $hexVal);
if (strlen($hexVal) != 6) { return "ERR: Incorrect colorcode."; }
$arrTmp = explode(" ", chunk_split($hexVal, 2, " "));
$arrTmp = array_map("hexdec", $arrTmp);
return array("red" => $arrTmp[0], "green" => $arrTmp[1],
"blue" => $arrTmp[2]);
}
/**
* Convert RGB colors to HEX colorcode
*
* @author Tim Johannessen <admin@stillborn.dk>
* @return hex Returns the converted colors as a 6 digit colorcode
*/
function rgb2hex($arrColors = null) {
if (!is_array($arrColors)) { return "ERR: Invalid input, expecting an array of
colors"; }
if (count($arrColors) < 3) { return "ERR: Invalid input, array too small (3)";
}
array_splice($arrColors, 3);
$arrColors = array_map("dechex", $arrColors);
return implode("", $arrColors);
}
}
?>