Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C++
Article

Contrasting Colors

Rate me:
Please Sign up or sign in to vote.
4.08/5 (16 votes)
4 May 20042 min read 164.4K   2.1K   30   37
New method of calculating a contrasting color for a given color
Image 1

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



Comments and Discussions

 
GeneralContrasting color? What's new Pin
asnaghi8-Jul-04 23:02
asnaghi8-Jul-04 23:02 
GeneralRe: Contrasting color? What's new Pin
btoum.roumada12-Jul-04 8:40
btoum.roumada12-Jul-04 8:40 
GeneralRe: Contrasting color? What's new Pin
asnaghi14-Jul-04 20:29
asnaghi14-Jul-04 20:29 
QuestionRe: Contrasting color? What's new Pin
murx9-Jul-15 8:06
murx9-Jul-15 8:06 
GeneralTOLERANCE Pin
Roger657-May-04 5:18
Roger657-May-04 5:18 
GeneralRe: TOLERANCE Pin
User 10455757-May-04 20:32
User 10455757-May-04 20:32 
GeneralRGB Color Cube Pin
Todd Smith5-May-04 8:43
Todd Smith5-May-04 8:43 
GeneralBlack and White / Gray Pin
Patje5-May-04 4:33
Patje5-May-04 4:33 
GeneralRe: Black and White / Gray Pin
User 10455755-May-04 5:38
User 10455755-May-04 5:38 
GeneralRe: Black and White / Gray Pin
Henry Jacobs5-May-04 10:43
Henry Jacobs5-May-04 10:43 
GeneralRe: Black and White / Gray Pin
Samuel Gonzalo11-Jun-04 0:13
Samuel Gonzalo11-Jun-04 0:13 
GeneralRe: Black and White / Gray Pin
User 104557513-Jul-04 22:43
User 104557513-Jul-04 22:43 
QuestionSimpler? Pin
Kippesoep5-May-04 4:28
Kippesoep5-May-04 4:28 
AnswerRe: Simpler? Pin
Patje5-May-04 4:34
Patje5-May-04 4:34 
GeneralRe: Simpler? Pin
Kippesoep5-May-04 9:48
Kippesoep5-May-04 9:48 
GeneralRe: Simpler? Pin
Patje6-May-04 4:43
Patje6-May-04 4:43 
AnswerRe: Simpler? Pin
User 10455755-May-04 5:24
User 10455755-May-04 5:24 
GeneralRe: Simpler? Pin
Kippesoep5-May-04 9:47
Kippesoep5-May-04 9:47 
GeneralRe: Simpler? Pin
User 10455756-May-04 4:14
User 10455756-May-04 4:14 
GeneralRe: Simpler? Pin
Kippesoep6-May-04 5:13
Kippesoep6-May-04 5:13 
GeneralRe: Simpler? Pin
User 10455757-May-04 1:24
User 10455757-May-04 1:24 
AnswerRe: Simpler? Pin
Steve Mayfield6-May-04 13:50
Steve Mayfield6-May-04 13:50 
GeneralRe: Simpler? Pin
Kippesoep6-May-04 21:02
Kippesoep6-May-04 21:02 
GeneralSome hints about RGB->HSL Pin
Kochise4-May-04 22:39
Kochise4-May-04 22:39 
Assuming a color in a COLORREF (format 0xaaBBGGRR) value :
COLORREF l_nColor = 0x00FF7F1F; // Sky blue

Getting the dark and light values :
COLORREF l_nColorDark  =  (l_nColor >> 1) & 0x007F7F7F);               // DARK
[EDIT=Fix from Paolo Messina]
COLORREF l_nColorLight = ((l_nColor >> 1) & 0x007F7F7F) + 0x00808080); // LIGHT
[/EDIT]

Now getting the grey values (very accurate 16 bits fixed point version) :
COLORREF l_nColorGrey  = ( (((int) (l_nColor & 0x000000FF) >>  0) * 0x00004C8B) // Red   * 0.299
                         + (((int) (l_nColor & 0x0000FF00) >>  8) * 0x0000941F) // Green * 0.5786
                         + (((int) (l_nColor & 0x00FF0000) >> 16) * 0x00001D2F) // Blue  * 0.114
                         ) >> 16; // From 0 to 255 (8 bits value)

         l_nColorGrey  = (l_nColorGrey << 0)  // Red
                       | (l_nColorGrey << 8)  // Green
                       | (l_nColorGrey << 16) // Blue
                       ; // Get a true grey RGB value

This is also how we can get other interresting values, such HSL (Hue, Saturation, Light) :
// Hue filter
//
// l_nColor      = Red Orange Yellow Green  Cyan   Blue Purple Magenta Red
// l_nColorHue   = 0             63          127        191            255

COLORREF l_nColorHue   = ( (((int) (l_nColor & 0x000000FF) >>  0) * 0x00008000) // Red   *  0.5
                         + (((int) (l_nColor & 0x0000FF00) >>  8) * 0xFFFF94D1) // Green * -0.4187
                         + (((int) (l_nColor & 0x00FF0000) >> 16) * 0xFFFFEB30) // Blue  * -0.0813
                         ) >> 16
                         + 128; // From 0 to 255 (8 bits value)

// Saturation filter
//
// l_nColor      = Grey     Light Color       Pastel Color      Pure Color
// l_nColorSat   = 0             63          127        191            255

COLORREF l_nColorSat   = ( (((int) (l_nColor & 0x000000FF) >>  0) * 0xFFFFD4D1) // Red   * -0.1687
                         + (((int) (l_nColor & 0x0000FF00) >>  8) * 0xFFFFAB30) // Green * -0.3313
                         + (((int) (l_nColor & 0x00FF0000) >> 16) * 0x00008000) // Blue  *  0.5
                         ) >> 16
                         + 128; // From 0 to 255 (8 bits value)

// Light filter
//
// l_nColor      = Black   Dark Color   Pure Color   Light Color     White
// l_nColorLight = 0             63          127        191            255

COLORREF l_nColorLight = l_nColorGrey;

Now the contrast can be get from the difference between two grey values.
Imagine you have a dark color, thus the grey value will be below 128. To get a good contrast, write in white above the color.
Imagine you have a light color, thus the grey value will be above 128. To get a good contrast, write in black above the color.

Kochise

In Code we trust !
GeneralRe: Some hints about RGB-&gt;HSL Pin
Paolo Messina5-May-04 3:13
professionalPaolo 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.