|
Posted by EnigmaticSource on October 5, 2007, 2:16 am
Please log in for more thread options
Try looking at some contrast formulas, and possibly try writing a
binary search algorithm
http://juicystudio.com/services/colourcontrast.php
Here's a bit of code, it's reasonably fast, and you should be able to
adapt it to your needs (if you know php). The Variable $target is
your target contrast value, 500 is considered pretty good, but that
isn't always reachable (or not in a time friendly way). if it finds a
500 or better combination it quits, or it quits when it has come
pretty close. Note: this will not always give the optimal color, but
it's close enough, the following returns a color of contrast value
400, which is 80% of optimal.
I'm sure it could be better, as I wrote it just for your post, but if
you decide to use it, it'd be nice, but not necessary to give credit.
<?php
/*
Color Selection By Contrast
Mark C. Roduner, Jr.
*/
function color_contrast($color_1, $color_2) {
$red_1 = ($color_1 & 0xff0000) >> 16;
$red_2 = ($color_2 & 0xff0000) >> 16;
$grn_1 = ($color_1 & 0xff00) >> 8;
$grn_2 = ($color_2 & 0xff00) >> 8;
$blu_1 = ($color_1 & 0xff);
$blu_2 = ($color_2 & 0xff);
return (abs($red_1 - $red_2) + abs($grn_1 - $grn_2) + abs($blu_1 -
$blu_2));
}
$source = 0xff00ff;
$seeker = $marker = 0xffffff;
$target = 500;
$best_contrast = 0;
$best_color = 0;
$contrast = color_contrast($source, $seeker);
while(($contrast < $target) and ($marker != 0)) {
$marker = intval($marker / 2);
if($contrast > $target) {
$seeker -= $marker;
} else if($contrast < $target) {
$seeker += $marker;
}
$contrast = color_contrast($source, $seeker);
if($contrast > $best_contrast) {
$best_contrast = $contrast;
$best_color = $seeker;
}
}
echo("\n Color: " . dechex($best_color) . " Contrast $best_contrast");
?>
> Imagine you're in a game where your opponent picks the background colour
> of your webpage, in #RRGGBB format, and you have to pick the foreground
> text colour that contrasts best with their choice. I realise that this
> task is almost impossible given various forms of colour-blindness, but
> my target audience is unlikely to be colour-blind (bomb disposal people
> don't last long if they cannot tell live from neutral) so we can assume
> perfect vision.
>
> If they pick #000000 then of course your text would be #FFFFFF (and vice
> versa).
>
> If they picked #FF0000 then the choice is less obvious. #00FFFF seems to
> offer the best "mathematical" contrast, but in tests, (to my eyes)
> #FFFFFF is a better contrast. Seehttp://swiftys.org.uk/contrast.html
> where I am playing with this concept.
>
> If they pick a background of #7F7F7F then yellow (FFFF00) or cyan
> (00FFFF) offer a good contrast, but magenta (FF00FF) is truly horrible.
> This may be an artefact either of my display, or my use of TrueType, but
> my HTML cannot possibly know about either of these facts.
>
> Can anyone offer an algorithm that would do a reasonable job faced with
> an arbitrary background colour? I'd hate to end up with a lookup table
> containing 16,777,216 rows!
>
> --
> Steve Swifthttp://www.swiftys.org.uk/swifty.htmlhttp://www.ringers.org.uk
|