Announcement

Collapse
No announcement yet.

Text Color Over Background Color

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Text Color Over Background Color

    I am working on a mapping project where captions will be printed over colored regions on a map. Colors come from user selection.

    Does anyone have a good algorithm for selecting whether the text should be white or black over a given RGB color?

    Currently my best try at it is to add the R, G and B values, then select white or black based on the combined value. (For example: If R+G+B < 225 white, else black.)

    This is fairly satifactory, but just wanted to see if anyone had something a little more sophisticated.
    The boy just ain't right.

  • #2
    Let the user pick the text color to be used when his choice of background color is used?
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Well, there's that.

      This really should be a GUI project, but as it is, it's a Console app. Users are editing an .ini file. I suppose adding one more parameter to the criteria line won't make it any more user-hostile. (Current criteria line is low-data-value, high-data-value, R, G, B)

      But then, it's a project for my own amusement, and will be made available to others in the same hobby with little to no support promised.

      I'll leave the issue open for any suggestions about doing it automatically.
      The boy just ain't right.

      Comment


      • #4
        Perhaps something like this...

        Code:
        'auto select foreground for best contrast with background
        'algorithm to select foreground as either black or white
        '-------------------------------------------------------------------------------------
        
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "WIN32API.INC"
        
        FUNCTION PBMAIN () AS LONG 
           LOCAL m, n, r, g, b, x AS LONG
           LOCAL hDlg, hGraph AS DWORD
           DIALOG NEW PIXELS, 0, "Simple Graphics Control",,, 320, 200, %WS_SYSMENU TO hDlg
           CONTROL ADD GRAPHIC, hDlg, 101, "", 0, 0, 320, 200, %SS_NOTIFY
           CONTROL HANDLE hDlg, 101 TO hGraph
           GRAPHIC ATTACH hGraph, 0 
           r = 228
           g = 255
           b = 0
           n = RGB(r, g, b)   'worst case:  n = RGB(128, 128, 128)
           x = (r + g + b)\3
           IF x < 128 THEN x = 255 ELSE x = 0
           m = RGB(x, x, x)
           GRAPHIC CLEAR n   
           GRAPHIC COLOR m, n
           GRAPHIC FONT "Times New Roman", 24, 0
           GRAPHIC SET POS (50, 50)
           GRAPHIC PRINT "This is a test"
           DIALOG SHOW MODAL hDlg
        END FUNCTION

        Comment


        • #5
          Here's What I Use

          This is what I use. I cooked it up a few years ago with some trial and error; there is not much logic to it that can be explained---just some thrashing around because the G in RGB seems to be much brighter to the eye than the R or B.

          This won't pass DIM ALL test--minor adjustments would have to be made for that. Some API macros are included (GetRValue, etc.)

          I've used it ever since and been totally satisfied with it. I use 'hue' as shorthand for 'color' though I realize that technically speaking it probably means something different than 'color.'

          Code:
          FUNCTION oppBWhue&(BYVAL anyhue&)
            LOCAL rr AS LONG : LOCAL mogadjust AS LONG : LOCAL gg AS LONG : LOCAL bb AS LONG
            rr=CLNG(GetRValue(anyhue&)) : gg=CLNG(GetGValue(anyhue&)) : bb=CLNG(GetBValue(anyhue&))
            mogadjust=2*gg-(rr+bb) : IF mogadjust<0 THEN mogadjust=(5000&+rr+bb)-gg
            IF mogadjust>4999& THEN
              mogadjust=mogadjust-5485&
              IF mogadjust<0 THEN mogadjust=0& ELSE mogadjust=mogadjust*5&
            END IF
            mogadjust=(7&*rr)\6&+(7&*gg)\4&+bb+mogadjust
            IF mogadjust<663& THEN FUNCTION=16777215& ELSE FUNCTION=0&
          END FUNCTION
          Steve

          Comment

          Working...
          X