Click here to Skip to main content
13,000,469 members (61,904 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 25 Apr 2004

Contrasting Colors

, 4 May 2004
Rate this:
Please Sign up or sign in to vote.
New method of calculating a contrasting color for a given color
<link rel="stylesheet" type=text/css href="global.css">


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;


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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Singapore Singapore
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralContrasting color? What's new Pin
asnaghi8-Jul-04 23:02
memberasnaghi8-Jul-04 23:02 
GeneralRe: Contrasting color? What's new Pin
maxismclaren12-Jul-04 8:40
membermaxismclaren12-Jul-04 8:40 
GeneralRe: Contrasting color? What's new Pin
asnaghi14-Jul-04 20:29
memberasnaghi14-Jul-04 20:29 
QuestionRe: Contrasting color? What's new Pin
murx9-Jul-15 8:06
membermurx9-Jul-15 8:06 
Roger637-May-04 5:18
memberRoger637-May-04 5:18 
GeneralRe: TOLERANCE Pin
alucardx7-May-04 20:32
memberalucardx7-May-04 20:32 
GeneralRGB Color Cube Pin
Todd Smith5-May-04 8:43
memberTodd Smith5-May-04 8:43 
GeneralBlack and White / Gray Pin
Patje5-May-04 4:33
memberPatje5-May-04 4:33 
GeneralRe: Black and White / Gray Pin
alucardx5-May-04 5:38
memberalucardx5-May-04 5:38 
GeneralRe: Black and White / Gray Pin
Henry Jacobs5-May-04 10:43
memberHenry Jacobs5-May-04 10:43 
GeneralRe: Black and White / Gray Pin
Samuel Gonzalo11-Jun-04 0:13
memberSamuel Gonzalo11-Jun-04 0:13 
GeneralRe: Black and White / Gray Pin
alucardx13-Jul-04 22:43
memberalucardx13-Jul-04 22:43 
QuestionSimpler? Pin
Kippesoep5-May-04 4:28
memberKippesoep5-May-04 4:28 
AnswerRe: Simpler? Pin
Patje5-May-04 4:34
memberPatje5-May-04 4:34 
GeneralRe: Simpler? Pin
Kippesoep5-May-04 9:48
memberKippesoep5-May-04 9:48 
GeneralRe: Simpler? Pin
Patje6-May-04 4:43
memberPatje6-May-04 4:43 
AnswerRe: Simpler? Pin
alucardx5-May-04 5:24
memberalucardx5-May-04 5:24 
GeneralRe: Simpler? Pin
Kippesoep5-May-04 9:47
memberKippesoep5-May-04 9:47 
GeneralRe: Simpler? Pin
alucardx6-May-04 4:14
memberalucardx6-May-04 4:14 
GeneralRe: Simpler? Pin
Kippesoep6-May-04 5:13
memberKippesoep6-May-04 5:13 
GeneralRe: Simpler? Pin
alucardx7-May-04 1:24
memberalucardx7-May-04 1:24 
AnswerRe: Simpler? Pin
Steve Mayfield6-May-04 13:50
memberSteve Mayfield6-May-04 13:50 
GeneralRe: Simpler? Pin
Kippesoep6-May-04 21:02
memberKippesoep6-May-04 21:02 
GeneralSome hints about RGB->HSL Pin
Kochise4-May-04 22:39
memberKochise4-May-04 22:39 
GeneralRe: Some hints about RGB-&gt;HSL Pin
Paolo Messina5-May-04 3:13
memberPaolo Messina5-May-04 3:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 5 May 2004
Article Copyright 2004 by alucardx
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid