Click here to Skip to main content
Licence 
First Posted 25 Apr 2004
Views 93,613
Downloads 1,131
Bookmarked 27 times

Contrasting Colors

By | 4 May 2004 | Article
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.

License

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

alucardx



Singapore Singapore

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralContrasting color? What's new Pinmemberasnaghi23:02 8 Jul '04  
GeneralRe: Contrasting color? What's new Pinmembermaxismclaren8:40 12 Jul '04  
GeneralRe: Contrasting color? What's new Pinmemberasnaghi20:29 14 Jul '04  
GeneralTOLERANCE PinmemberRoger635:18 7 May '04  
GeneralRe: TOLERANCE Pinmemberalucardx20:32 7 May '04  
GeneralRGB Color Cube PinmemberTodd Smith8:43 5 May '04  
GeneralBlack and White / Gray PinmemberPatje4:33 5 May '04  
GeneralRe: Black and White / Gray Pinmemberalucardx5:38 5 May '04  
GeneralRe: Black and White / Gray PinmemberHenry Jacobs10:43 5 May '04  
GeneralRe: Black and White / Gray PinmemberSamuel Gonzalo0:13 11 Jun '04  
GeneralRe: Black and White / Gray Pinmemberalucardx22:43 13 Jul '04  
QuestionSimpler? PinmemberKippesoep4:28 5 May '04  
AnswerRe: Simpler? PinmemberPatje4:34 5 May '04  
GeneralRe: Simpler? PinmemberKippesoep9:48 5 May '04  
GeneralRe: Simpler? PinmemberPatje4:43 6 May '04  
AnswerRe: Simpler? Pinmemberalucardx5:24 5 May '04  
GeneralRe: Simpler? PinmemberKippesoep9:47 5 May '04  
GeneralRe: Simpler? Pinmemberalucardx4:14 6 May '04  
GeneralRe: Simpler? PinmemberKippesoep5:13 6 May '04  
GeneralRe: Simpler? Pinmemberalucardx1:24 7 May '04  
AnswerRe: Simpler? PinmemberSteve Mayfield13:50 6 May '04  
GeneralRe: Simpler? PinmemberKippesoep21:02 6 May '04  
GeneralSome hints about RGB->HSL PinmemberKochise22:39 4 May '04  
GeneralRe: Some hints about RGB->HSL PinmemberPaolo Messina3:13 5 May '04  
GeneralYop, true ;) PinmemberKochise4:15 5 May '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 5 May 2004
Article Copyright 2004 by alucardx
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid