note 51273 added to function.hexdec
| From: | zubfatal at osu1 dot php dot net | Date: | Fri, 25 Mar 2005 10:36:38 +0000 |
| Subject: | note 51273 added to function.hexdec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86936@lists.php.net to get a copy of this message | ||
This replaces my previous class.
I've added a few more input checks in the rgb2hex function.
Also it returned incorrect hex values for 1-digit values.
color::rgb2hex(array(0,0,0)) would output 000 not 00000.
<?php
/**
* Convert colors
*
* Usage:
* color::hex2rgb("FFFFFF")
* color::rgb2hex(array(171,37,37))
*
* @author Tim Johannessen <root@it.dk>
* @version 1.0.1
*/
class color {
/**
* Convert HEX colorcode to an array of colors.
* @return array Returns the array of colors as array(red,green,blue)
*/
function hex2rgb($hexVal = "") {
$hexVal = eregi_replace("[^a-fA-F0-9]", "", $hexVal);
if (strlen($hexVal) != 6) { return "ERR: Incorrect colorcode, expecting 6 chars (a-f,
0-9)"; }
$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
* @return string 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);
for ($x = 0; $x < count($arrColors); $x++) {
if (strlen($arrColors[$x]) < 1) {
return "ERR: One or more empty values found, expecting array with 3
values";
}
elseif (eregi("[^0-9]", $arrColors[$x])) {
return "ERR: One or more non-numeric values found.";
}
else {
if ((intval($arrColors[$x]) < 0) || (intval($arrColors[$x]) > 255)) {
return "ERR: Range mismatch in one or more values (0-255)";
}
else {
$arrColors[$x] = strtoupper(str_pad(dechex($arrColors[$x]), 2, 0,
STR_PAD_LEFT));
}
}
}
return implode("", $arrColors);
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.hexdec.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51273
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51273&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51273&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51273&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51273&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51273&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51273&report=yes
Search -- http://master.php.net/manage/user-notes.php