471,598 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,598 software developers and data experts.

Contrasting colours

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
Oct 5 '07 #1
7 3174
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");
?>

On Oct 5, 3:07 am, Steve Swift <Steve.J.Sw...@gmail.comwrote:
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

Oct 5 '07 #2
On 5 Oct, 04:07, Steve Swift <Steve.J.Sw...@gmail.comwrote:
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.

Oct 5 '07 #3


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
Oct 5 '07 #4
On 5 Oct, 15:34, William Gill <nore...@example.invalidwrote:
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.

Oct 5 '07 #5


Andy Dingley wrote:
On 5 Oct, 15:34, William Gill <nore...@example.invalidwrote:
>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.
I read several color theory books, and articles on line. Was trying to
create a helper tool that allowed me to start with a color (from
client's logo or such) create a basic scheme, and satisfy w3
accessibility spec. Failed miserably, and gave up with a headache.

Will look into the references you cite, thanks.
Oct 5 '07 #6
Steve Swift wrote:

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.
That's because the brightness contrast matters. You've just considered the
tone contrast.

http://www.visionaustralia.org.au/info.aspx?page=628
If they pick a background of #7F7F7F then yellow (FFFF00) or cyan
(00FFFF) offer a good contrast, but magenta (FF00FF) is truly horrible.
Same thing.
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!
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.
Brightness contrast is important because our eye isn't a screen-to-RGB
converter.

1) Cones spectra overlap.
http://en.wikipedia.org/wiki/Image:Cones_smj2_E.png
Which implies that green photons stimulates red cones too.
(On the other hand, blue and red are at a larger distance).
This is especially a problem if the colors are very bright, because
receptors will be saturated.

2) On peripheral vision, most receptive cells are rods, which are very
sensible to brightness, but don't percieve the color tone.

Not strangely, it's the same foreground/background color pairs that are
most readable for you and for a half-blind grandma.
I realise that this task is almost impossible given various forms of
colour-
blindness
Not if you just think about ONE color blindness: achromatopsy. It regroups
all others.
Just make the text legible when it's converted to gray levels, and it'll
be legible for everbody.

--
If you've a question that doesn't belong to Usenet, contact me at
<ta*****************@yahoDELETETHATo.fr>
Oct 5 '07 #7
Just make the text legible when it's converted to gray levels, and it'll
be legible for everbody.
This was the consensus in my employers newsgroups as well (I like to get
two different sets of opinions, the variations are often enlightening in
themselves).

My plan is to convert the background colour to a grey-shade (simply by
averaging the red/green/blue components initially, then setting the text
to either black or white depending on the result.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
Oct 8 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by CSX | last post: by
8 posts views Thread by Doug Laidlaw | last post: by
4 posts views Thread by Tim Marshall | last post: by
1 post views Thread by Joe | last post: by
reply views Thread by Rui Oliveira | last post: by
9 posts views Thread by piyush2884 | last post: by
5 posts views Thread by Jameson | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.