Background Color, Text Color 
Author Message
 Background Color, Text Color

Anyone have a suggestion for an algorithm that would help determine
which text colour (either black or white) would be better to use based
on a given background colour? (stored in a COLORREF)

i.e.  
- If the background colour is fairly dark, use white text.
- If the background colour is fairly light, use black text.

Jack



Mon, 28 Nov 2005 03:35:56 GMT  
 Background Color, Text Color
No good solutions. I've tried a number of tricks, but none have been particularly
satisfactory, partly because there is an intermediate state where the computations break
down and you get low contrast. I usually just have a table of pairs. If I want a lot of
variance, I give the user a way to set foreground and background colors explicitly.
                                joe

Quote:

>Anyone have a suggestion for an algorithm that would help determine
>which text colour (either black or white) would be better to use based
>on a given background colour? (stored in a COLORREF)

>i.e.  
>- If the background colour is fairly dark, use white text.
>- If the background colour is fairly light, use black text.

>Jack

Joseph M. Newcomer [MVP]

Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Mon, 28 Nov 2005 05:12:35 GMT  
 Background Color, Text Color

Quote:

>i.e.  
>- If the background colour is fairly dark, use white text.
>- If the background colour is fairly light, use black text.

I've used this in the past :

DWORD  GetTextColourForBG (DWORD dwBack)
{
   UINT uRed, uGreen, uBlue;

   uRed   = GetRValue (dwBack);
   uBlue  = GetBValue (dwBack);
   uGreen = GetGValue (dwBack);

   if ((uRed + uGreen + uBlue) > 384) // three half intensities
   {
      return (0);
   }
   else
   {
      // Greens will look brighter than
      // the numbers would suggest, so
      // swap to black early.

      if (uGreen > 160)
         return (0);
      else
         return (0xFFFFFF);
   }

Quote:
}

Hacky but semi-OK. I tried colour solutions but you always get bogged
down in the special cases like blue-green colour blindness and mid
range contrast (as Joe suggests). Keep It Simple :-)

--
Bob Moore [WinSDK MVP]
http://www.mooremvp.freeserve.co.uk/
(this is a non-commercial site and does not accept advertising)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do not reply via email unless specifically requested to do so.
Unsolicited email is NOT welcome and will go unanswered.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Mon, 28 Nov 2005 20:36:03 GMT  
 Background Color, Text Color
Yes, I tried similar algorithms and got the same problems. I once tried converting to HSB
and using an HSB contrast function, and the results were actually less satisfactory.
                                joe

Quote:


>>i.e.  
>>- If the background colour is fairly dark, use white text.
>>- If the background colour is fairly light, use black text.

>I've used this in the past :

>DWORD  GetTextColourForBG (DWORD dwBack)
>{
>   UINT uRed, uGreen, uBlue;

>   uRed   = GetRValue (dwBack);
>   uBlue  = GetBValue (dwBack);
>   uGreen = GetGValue (dwBack);

>   if ((uRed + uGreen + uBlue) > 384) // three half intensities
>   {
>      return (0);
>   }
>   else
>   {
>      // Greens will look brighter than
>      // the numbers would suggest, so
>      // swap to black early.

>      if (uGreen > 160)
>         return (0);
>      else
>         return (0xFFFFFF);
>   }
>}

>Hacky but semi-OK. I tried colour solutions but you always get bogged
>down in the special cases like blue-green colour blindness and mid
>range contrast (as Joe suggests). Keep It Simple :-)

Joseph M. Newcomer [MVP]

Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Tue, 29 Nov 2005 02:57:08 GMT  
 Background Color, Text Color
On Thu, 12 Jun 2003 21:57:08 -0400, Joseph M. Newcomer

I found the following algorithm in an old MSJ article on drawing
gradient caption bars. (it's also used on a project found on
codeguru.com)

int AfxGetLuminosity(COLORREF color)
{
        /* This function determines the luminosity of a color */
        /*------Revision History------*/
        // - June 13, 2003

        // Constants
        const int HLSMAX = 240;
        const int RGBMAX = 255;

        // Step - Get the individual color values
        int nRed = GetRValue(color);
        int nGreen = GetGValue(color);
        int nBlue = GetBValue(color);

        // Step - Get the maximum and minimum value of the colors
        int nRGBMax = max(max(nRed, nGreen), nBlue);
        int nRGBMin = min(min(nRed, nGreen), nBlue);

        // Step - Use the "magic" formula
        return (((nRGBMax + nRGBMin) * HLSMAX) + RGBMAX) / (2 *
RGBMAX);

Quote:
}

if luminosity returns > 120, use black, else use white.  It seems to
work OK for most of the basic colors in CColorDialog, haven't tried
any custom colors yet.  Bright green is the color that seems to cause
problems.

Jack

Quote:
>Yes, I tried similar algorithms and got the same problems. I once tried converting to HSB
>and using an HSB contrast function, and the results were actually less satisfactory.
>                            joe



>>>i.e.  
>>>- If the background colour is fairly dark, use white text.
>>>- If the background colour is fairly light, use black text.

>>I've used this in the past :

>>DWORD  GetTextColourForBG (DWORD dwBack)
>>{
>>   UINT uRed, uGreen, uBlue;

>>   uRed   = GetRValue (dwBack);
>>   uBlue  = GetBValue (dwBack);
>>   uGreen = GetGValue (dwBack);

>>   if ((uRed + uGreen + uBlue) > 384) // three half intensities
>>   {
>>      return (0);
>>   }
>>   else
>>   {
>>      // Greens will look brighter than
>>      // the numbers would suggest, so
>>      // swap to black early.

>>      if (uGreen > 160)
>>         return (0);
>>      else
>>         return (0xFFFFFF);
>>   }
>>}

>>Hacky but semi-OK. I tried colour solutions but you always get bogged
>>down in the special cases like blue-green colour blindness and mid
>>range contrast (as Joe suggests). Keep It Simple :-)

>Joseph M. Newcomer [MVP]

>Web: http://www.flounder.com
>MVP Tips: http://www.flounder.com/mvp_tips.htm



Wed, 30 Nov 2005 04:30:34 GMT  
 Background Color, Text Color
i get the RGB of the background and i do :
if (R + G + B) < 384
{
i use white pen
Quote:
}

else
i use black pen

So I can always read the text .


Quote:
> Anyone have a suggestion for an algorithm that would help determine
> which text colour (either black or white) would be better to use based
> on a given background colour? (stored in a COLORREF)

> i.e.
> - If the background colour is fairly dark, use white text.
> - If the background colour is fairly light, use black text.

> Jack



Sun, 04 Dec 2005 08:58:57 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Background color and text color in menu

2. Change text color and background color in CEditView

3. Change a part of Background color or a part of text color from CListCtrl

4. set the text color and the background color of a Control Cbutton

5. Win2K: static background color vs. CFormView background color

6. Detecting Color Pallete ( 16 colors , or 256 colors, or Hi color) URGENT

7. CEditView Background Color & Font Color :: MFC

8. CTreeCtrl Color/Background Color Q

9. What Color represents the system text background?

10. Newbie - Question About Text and Background Colors

11. changing background color of the text

12. Text and background colors in BC++ 5.0

 

 
Powered by phpBB® Forum Software