note 73255 deleted from function.dechex by danbrown

From: Date: Tue, 16 Feb 2010 23:28:01 +0000
Subject: note 73255 deleted from function.dechex by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166039@lists.php.net to get a copy of this message
Note Submitter: poj ---- 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.

« previous php.notes (#166039) next »