/ Published in: PHP
Expand |
Embed | Plain Text
<?php function HexToRGB($hex) { } } return $color; } function RGBToHex($r, $g, $b) { //String padding bug found and the solution put forth by Pete Williams (http://snipplr.com/users/PeteW) $hex = "#"; return $hex; } ?>
Comments
Subscribe to comments
You need to login to post a comment.

Oooh... thank you very much. :)
Thank you!
Thank you!
I'm afraid there's a bug in RGBToHex.
You need to pad each hex value, otherwise your full hex number could end up being 3, 4 or 5 digits.
For instance, the following would both generate #22222, despite being different colours: RGBToHex (2, 34, 34); RGBToHex (34, 34, 2);
Try this:
function RGBToHex($r, $g, $b) { $hex = "#"; $hex.= strpad(dechex($r), 2, '0', STRPADLEFT ); $hex.= strpad(dechex($g), 2, '0', STRPADLEFT ); $hex.= strpad(dechex($b), 2, '0', STRPAD_LEFT );
I'm afraid there's a bug in RGBToHex.
You need to pad each hex value, otherwise your full hex number could end up being 3, 4 or 5 digits.
For instance, the following would both generate #22222, despite being different colours: RGBToHex (2, 34, 34); RGBToHex (34, 34, 2);
Try this:
function RGBToHex($r, $g, $b) { $hex = "#"; $hex.= strpad(dechex($r), 2, '0', STRPADLEFT ); $hex.= strpad(dechex($g), 2, '0', STRPADLEFT ); $hex.= strpad(dechex($b), 2, '0', STRPAD_LEFT );Meh, Snipplr doesn't seem to want to display my code properly, but hopefully you get the idea.
Cool thanks for that info on the bug I fixed the issue.
Thank you!
in the line 3 use better str_replace