note 73255 added to function.dechex
| From: | poj at osu1 dot php dot net | Date: | Wed, 14 Feb 2007 21:18:37 +0000 |
| Subject: | note 73255 added to function.dechex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123387@lists.php.net to get a copy of this message | ||
The rgbhex function by AsakuraStar won't work for small values, eg (15,15,15) should return
0F0F0F, not FFF. I suggest the following:
<?php
function rgbhex($red, $green, $blue) {
// force the passed value to be numeric by adding zero
// use max and min to limit the number to between 0 and 255
// shift the number to make it the correct future hex value
$red = 0x10000 * max(0,min(255,$red+0));
$green = 0x100 * max(0,min(255,$green+0));
$blue = max(0,min(255,$blue+0));
// convert the combined value to hex and zero-fill to 6 digits
return "#".str_pad(strtoupper(dechex($red + $green +
$blue)),6,"0",STR_PAD_LEFT);
}
?>
Maybe in future dechex will have a size parameter, as in:
dechex($red+$green+$blue , 6)
This would especially be useful for negative numbers.
dechex(-1 , 2) would then give ff.
----
Server IP: 216.235.15.211
Probable Submitter: 142.204.211.186
----
Manual Page -- http://www.php.net/manual/en/function.dechex.php
Edit -- https://master.php.net/note/edit/73255
Del: integrated -- https://master.php.net/note/delete/73255/integrated
Del: useless -- https://master.php.net/note/delete/73255/useless
Del: bad code -- https://master.php.net/note/delete/73255/bad+code
Del: spam -- https://master.php.net/note/delete/73255/spam
Del: non-english -- https://master.php.net/note/delete/73255/non-english
Del: in docs -- https://master.php.net/note/delete/73255/in+docs
Del: other reasons-- https://master.php.net/note/delete/73255
Reject -- https://master.php.net/note/reject/73255
Search -- https://master.php.net/manage/user-notes.php