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
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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.