Click here to get back home

Contrasting colours

 HomeNewsGroups | Search | About
 comp.infosystems.www.authoring.html    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Contrasting colours Steve Swift 10-04-2007
Posted by Steve Swift on October 4, 2007, 11:07 pm
Please log in for more thread options
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. See http://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 Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk

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



Posted by Andy Dingley on October 5, 2007, 5:37 am
Please log in for more thread options
> 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.

Convert from RGB to HSV, then rotate the hue by 180degrees. Convert
back to RGB.

The rest is standard algorithms and web searching.


Posted by William Gill on October 5, 2007, 10:34 am
Please log in for more thread options


Andy Dingley wrote:
>
> Convert from RGB to HSV, then rotate the hue by 180degrees. Convert
> back to RGB.

Sorry Andy, it's not so simple. I tried this, and a few other
algorithms a while back, and failed. I back-burnered the effort, but if
I'm missing something, please help me get it straight.

#EADBD1 (hue 24 deg); rotate to 204 deg == #D1E1EB
brightness difference == 0
color difference == 57



Posted by Andy Dingley on October 5, 2007, 11:45 am
Please log in for more thread options
> Sorry Andy, it's not so simple.

It depends a bit what you menan by "contrast". If you take a
reasonably strong saturation, then rotating the hue is enough - it's a
pure chroma-based contrast. If you're using a "pastel" shade and you
want the strongest perceived contrast, then you'll want to vary the
saturation too. Read Gombrich or Denning for the relevant theory.


Similar ThreadsPosted
"Web-safe" colours September 2, 2005, 7:35 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap