Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. RGB question..

RGB question..

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomtoolstutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rbid
    wrote on last edited by
    #1

    Hello, I have a GUI application where the user can set the background color for a plotting control. My question is how to select a foreground(text) color that will "contrast" with the user selected background?

    COLORREF bg = GetUserPlotColor();
    COLORREF fg = F(bg);

    That is, what is the code for the F() body? TIA. -- **Ricky Marek** (_AKA: rbid_) -- "Things are only impossible until they are not" --- Jean-Luc Picard My articles

    R B 2 Replies Last reply
    0
    • R rbid

      Hello, I have a GUI application where the user can set the background color for a plotting control. My question is how to select a foreground(text) color that will "contrast" with the user selected background?

      COLORREF bg = GetUserPlotColor();
      COLORREF fg = F(bg);

      That is, what is the code for the F() body? TIA. -- **Ricky Marek** (_AKA: rbid_) -- "Things are only impossible until they are not" --- Jean-Luc Picard My articles

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      I'm no colour expert, but I did encounter this while writing a custom-drawn tree control a while back. Have a look at this[^] article that should point you in the right direction. However, in the end I think I settled for a simple XOR technique, where I XOR'ed my selection colour with white (#FFFFFF), to get the corresponding contrasting text colour. This is definitely the easy route, and will probably give unsatisfactory results on certain colours, but it works for me. By the way, I found this[^] little web applet useful.


      I Dream of Absolute Zero

      N 1 Reply Last reply
      0
      • R rbid

        Hello, I have a GUI application where the user can set the background color for a plotting control. My question is how to select a foreground(text) color that will "contrast" with the user selected background?

        COLORREF bg = GetUserPlotColor();
        COLORREF fg = F(bg);

        That is, what is the code for the F() body? TIA. -- **Ricky Marek** (_AKA: rbid_) -- "Things are only impossible until they are not" --- Jean-Luc Picard My articles

        B Offline
        B Offline
        basementman
        wrote on last edited by
        #3

        This is an old function that generated a suitable shadow color for drawing 3D elements. If you reverese it, it should do what you want:

        COLORREF MakeShadowColor(COLORREF rgbColor)
        {
        COLORREF rgbRetval;

        // common color conversions...
        if (rgbColor == RGB(255,255,128)) // lyellow
        {
        rgbRetval = RGB(128,128,0);
        }
        else if (rgbColor == RGB(192,192,192)) // lgrey
        {
        rgbRetval = RGB(128,128,128);
        }
        else if (rgbColor == RGB(128,255,255)) // lcyan
        {
        rgbRetval = RGB(0,0,255);
        }
        else if (rgbColor == RGB(0,0,0)) // black
        {
        rgbRetval = RGB(128,128,128);
        }
        else
        {
        short iR = (GetRValue(rgbColor)+1)/2;
        short iG = (GetGValue(rgbColor)+1)/2;
        short iB = (GetBValue(rgbColor)+1)/2;

          rgbRetval = RGB(iR,iG,iB);
        }
        

        return rgbRetval;
        }

        onwards and upwards...

        1 Reply Last reply
        0
        • R RChin

          I'm no colour expert, but I did encounter this while writing a custom-drawn tree control a while back. Have a look at this[^] article that should point you in the right direction. However, in the end I think I settled for a simple XOR technique, where I XOR'ed my selection colour with white (#FFFFFF), to get the corresponding contrasting text colour. This is definitely the easy route, and will probably give unsatisfactory results on certain colours, but it works for me. By the way, I found this[^] little web applet useful.


          I Dream of Absolute Zero

          N Offline
          N Offline
          normanS
          wrote on last edited by
          #4

          I have played around a bit with inverting frame-grabbed images, and I found that the XOR technique usually works nicely. Where it will not work well is with grey-scales which are close to mid-range, for example, if you have RGB-15 (5-bits of each colour, high order bit unused), where the R, G, and B values are all half-range = 15 decimal, i.e. the pixel is 0011 1101 1110 1111, XORing will give 1100 0010 0001 0000, i.e R, G, and B are all 10000 binary = 16 decimal. For cases like this, the XORed text will be very difficult to make out. To get around this, you could saturate (force to maximum or minimum) each colour component, then XOR / invert the result. For my example, the 15 decimal component values are forced to 0, then inverted to give 11111 binary, which should be visible.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups