12,402,889 members (73,902 online)
alternative version

117.8K views
28 bookmarked
Posted

# Contrasting Colors

, 4 May 2004
 Rate this:
New method of calculating a contrasting color for a given color

## Introduction

The conventional way of calculating a contrasting color is to XOR the given color by 0xFFFFFF (or &HFFFFFF). However, the contrasting color obtained in this way may sometimes be similar to the original color, and our eyes can't really differentiate between the two distinctively. Thus this conventional way is not good enough. For example, grey, having the hex value of 0x808080, when XORed with 0xFFFFFF, gives 0x7F7F7F, which is very close to the original color.

I have come up with a new and simple solution that is based on this conventional method. This solution calculates a better contrasting color, but note that it is not always perfect. (Note: I'm not sure whether there already exists such a method).

## The solution

This method works by testing whether each of the components (namely red, green, and blue) of the given color is close to 0x80 (128 in decimal). The closeness is defined by the compile time macro `TOLERANCE`, which is simply a value. If all the components are close to 0x80, then the contrasting color calculated using the old method will not be a good contrast to the original color. The new method overcomes this problem by adding 0x7F7F7F to the given color, and then to prevent overflow (because color values range only from 0x000000 to 0xFFFFFF), the result is ANDed with 0xFFFFFF.

## Using the code

The main thing about this project is the function `CalcContrastingColor`, which is presented below.

At run time, click the "Background..." button to select a background color. Then the program will display a text using the calculated contrasting color. You can also change the font of the text.

```// crBG is in 0xRRGGBB or 0xBBGGRR format.
INT CalcContrastColor (INT crBg){

if (
abs(((crBg ) & 0xFF) - 0x80) <= TOLERANCE &&
abs(((crBg >> 8) & 0xFF) - 0x80) <= TOLERANCE &&
abs(((crBg >> 16) & 0xFF) - 0x80) <= TOLERANCE

) return (0x7F7F7F + crBg) & 0xFFFFFF;

else return crBg ^ 0xFFFFFF;
}```

## History

Improvements in version 2:
• Display the outputs as shown in the above image.
• Some text included in order to let user decide better whether it is a good contrasting color.
• Windows XP theme compatible.

A list of licenses authors might use can be found here

## Share

 Singapore
No Biography provided

## You may also be interested in...

 First Prev Next
 Contrasting color? What's new asnaghi8-Jul-04 23:02 asnaghi 8-Jul-04 23:02
 Re: Contrasting color? What's new maxismclaren12-Jul-04 8:40 maxismclaren 12-Jul-04 8:40
 Re: Contrasting color? What's new asnaghi14-Jul-04 20:29 asnaghi 14-Jul-04 20:29
 Re: Contrasting color? What's new murx9-Jul-15 8:06 murx 9-Jul-15 8:06
 TOLERANCE Roger637-May-04 5:18 Roger63 7-May-04 5:18
 Re: TOLERANCE alucardx7-May-04 20:32 alucardx 7-May-04 20:32
 RGB Color Cube Todd Smith5-May-04 8:43 Todd Smith 5-May-04 8:43
 Black and White / Gray Patje5-May-04 4:33 Patje 5-May-04 4:33
 Re: Black and White / Gray alucardx5-May-04 5:38 alucardx 5-May-04 5:38
 Re: Black and White / Gray Henry Jacobs5-May-04 10:43 Henry Jacobs 5-May-04 10:43
 Re: Black and White / Gray Samuel Gonzalo11-Jun-04 0:13 Samuel Gonzalo 11-Jun-04 0:13
 Re: Black and White / Gray alucardx13-Jul-04 22:43 alucardx 13-Jul-04 22:43
 Simpler? Kippesoep5-May-04 4:28 Kippesoep 5-May-04 4:28
 Re: Simpler? Patje5-May-04 4:34 Patje 5-May-04 4:34
 Re: Simpler? Kippesoep5-May-04 9:48 Kippesoep 5-May-04 9:48
 Re: Simpler? Patje6-May-04 4:43 Patje 6-May-04 4:43
 Re: Simpler? alucardx5-May-04 5:24 alucardx 5-May-04 5:24
 Re: Simpler? Kippesoep5-May-04 9:47 Kippesoep 5-May-04 9:47
 Re: Simpler? alucardx6-May-04 4:14 alucardx 6-May-04 4:14
 Re: Simpler? Kippesoep6-May-04 5:13 Kippesoep 6-May-04 5:13
 Re: Simpler? alucardx7-May-04 1:24 alucardx 7-May-04 1:24
 Re: Simpler? Steve Mayfield6-May-04 13:50 Steve Mayfield 6-May-04 13:50
 Re: Simpler? Kippesoep6-May-04 21:02 Kippesoep 6-May-04 21:02
 Some hints about RGB->HSL Kochise4-May-04 22:39 Kochise 4-May-04 22:39
 Re: Some hints about RGB->HSL Paolo Messina5-May-04 3:13 Paolo Messina 5-May-04 3:13
 Yop, true ;) Kochise5-May-04 4:15 Kochise 5-May-04 4:15
 Re: Some hints about RGB->HSL alucardx5-May-04 5:41 alucardx 5-May-04 5:41
 The difference of grey... Kochise5-May-04 6:59 Kochise 5-May-04 6:59
 Re: The difference of grey... alucardx6-May-04 4:25 alucardx 6-May-04 4:25
 OK, here is the solution explained Kochise7-May-04 10:45 Kochise 7-May-04 10:45
 Sorry, I must have been drunk yesterday night :/ (VERY LONG POST) Kochise8-May-04 9:17 Kochise 8-May-04 9:17
 I only want the returned color to be black or white pscholl11-Jun-09 4:37 pscholl 11-Jun-09 4:37
 Re: I only want the returned color to be black or white alucardx14-Jun-09 15:50 alucardx 14-Jun-09 15:50
 Super! Elcrombo4-May-04 21:18 Elcrombo 4-May-04 21:18
 Looks Good kcjt3-May-04 12:27 kcjt 3-May-04 12:27
 contrasting colors dbdduane30-Apr-04 6:24 dbdduane 30-Apr-04 6:24
 Good Job mkg27-Apr-04 5:07 mkg 27-Apr-04 5:07
 Last Visit: 31-Dec-99 18:00     Last Update: 27-Jul-16 22:52 Refresh 1