Choosing the Right Background / Foreground Color Using PHP

I am looking to calculate the appropriate background color and the color of the text that will go on top, obviously I need to consider readability and accessibility.

I would need to select two colors from an array, the colors are stored in their hexadecimal representations.

#CC9966
#996633
#FFCC99
#CCCC99
#000000
#333333
#666633
#663333
#CC9933
#FFCCCC

Can I use a PHP library like GD / imageMagick?

Any suggestions (note that I am using PHP)

+5
source share
3 answers

First of all, I think you should take a look at this great Particletree blog post .

smarty_modifier_contrast() , :

function color_contrast($bgcolor, $color1, $color2, $color3, ...)
{
    return (hexdec($bgcolor) > 0xffffff / 2) ? min(array_slice(func_get_args(), 1)) : max(array_slice(func_get_args(), 1));
}

, ( #!), , , , - $bgcolor :

function color_contrast($bgcolor, $colors = array())
{
    return (hexdec($bgcolor) > 0xffffff / 2) ? min($colors) : max($colors);
}

$bgcolor, , , , , , , .

+2

, . , , .

, "" , , , .

distance = abs(2*(r1-r2) + 3*(g1-g2) + (b1-b2)), , . , , , , . -, .

+1

Divide each color into its RGB components, add 0x80 (or some other similar value, possibly 0x66) to each, mask at 0xff and find the nearest value in the table.

0
source

All Articles