michaellunsford Posted January 21, 2007 Share Posted January 21, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/35035-guess-complimentary-color/ Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 unless I misunderstood, I would answer like thisTake 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. Quote Link to comment https://forums.phpfreaks.com/topic/35035-guess-complimentary-color/#findComment-165242 Share on other sites More sharing options...
michaellunsford Posted January 21, 2007 Author Share Posted January 21, 2007 Considering that there are over sixteen million possible color combinations. I'm thinking there's a way to convert to grayscale for purposes of detecting contrast. Quote Link to comment https://forums.phpfreaks.com/topic/35035-guess-complimentary-color/#findComment-165278 Share on other sites More sharing options...
scotmcc Posted January 21, 2007 Share Posted January 21, 2007 Check this place out, it might be able to help you :)[url=http://www.w3.org/TR/AERT#color-contrast]http://www.w3.org/TR/AERT#color-contrast[/url]Scot Quote Link to comment https://forums.phpfreaks.com/topic/35035-guess-complimentary-color/#findComment-165283 Share on other sites More sharing options...
michaellunsford Posted January 21, 2007 Author Share Posted January 21, 2007 HA! It might not be perfect in every situation, but it's working for me.[code]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/35035-guess-complimentary-color/#findComment-165347 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.