Jump to content

guess complimentary color?


michaellunsford

Recommended Posts

This is probably a theory question. Is there a good way to determine whether text color should be white or black based on a provided background color? I can't figure out how to make PHP guess a good contrast.

Background colors are HTML colors -- could be something like #00b162 (which displays fine in black), or #2e2c29 (which is very difficult to read in black).

Thanks for any direction!
Link to comment
https://forums.phpfreaks.com/topic/35035-guess-complimentary-color/
Share on other sites

unless I misunderstood, I would answer like this
Take all teh color's of hexes, and put them in a giant array, color name, hex number.

Then create a program to test the color, if it's a dark color generally white would look good, if it's a light color white would look good.  Then during testing you can take note of what looks bad and what doesn't and build onto the knowledge base overtime to make it smarter and smarter at discerning colors.
HA! It might not be perfect in every situation, but it's working for me.
[code]<?php
function test_color($rgb) { //returns 0 if white, 1 if black.
$r=substr($rgb,0,2);
$g=substr($rgb,2,2);
$b=substr($rgb,4,2);
$detect=imagecreate(1,1);
imagecolorallocate($detect,0xFF,0xFF,0xFF);
imagecolorallocate($detect,0x00,0x00,0x00);
$detect_color=imagecolorclosest($detect,hexdec($r),hexdec($g),hexdec($b));
imagedestroy($detect);
return($detect_color);
}
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.