Vera Vera - 5 months ago 38
PHP Question

PHP How can I get HEX Color code from array of R + G + B?

I have an array:

[red] => 252
[green] => 168
[blue] => 166
[alpha] => 0

It's an output of function imagecolorsforindex.

How can I get a HTML code from these elements? For example: #99CCFF


Strictly speaking you can't, since alpha is not supported. But since the alpha is 0, we can assume that it won't matter. As such, pass each value into sprintf() with a format specifier of %02x for each element.

c = sprintf('#%02x%02x%02x', val['red'], val['green'], val['blue']);