Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

php

hex

rgb


I have an array:

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

like image 267
Vera Avatar asked Nov 17 '25 14:11

Vera


2 Answers

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']);
like image 185
Ignacio Vazquez-Abrams Avatar answered Nov 20 '25 03:11

Ignacio Vazquez-Abrams


PHP Convert RGB from/to HTML hex color

rgb2html($array[0], $array[1], $array[2])
like image 36
Sascha Galley Avatar answered Nov 20 '25 04:11

Sascha Galley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!