Page 1 of 1

Is White/Black the best color for a specific BG color ?

Posted: Tue Apr 24, 2012 10:16 am
by Mohawk70

Code: Select all

Procedure GetTextColor(rgbRed.i,rgbGrn.i,rgbBlu.i)
 Caption.s = "INFO"
 Message.s = "H: #" + RSet(Hex(rgbRed),2,"0") + RSet(Hex(rgbGrn),2,"0") + RSet(Hex(rgbBlu),2,"0") + Chr(10)
 Message.s + "D: RGB(" + Str(rgbRed) + "," + Str(rgbGrn) + "," + Str(rgbBlu) + ")" + Chr(10)
 brightness.f = Sqr( (0.299 * Pow(rgbRed,2)) + (0.587 * Pow(rgbGrn,2)) + (0.114 * Pow(rgbBlu,2)) )
 If brightness.f < 130
  sOut.s = "#FFFFFF"
 Else
  sOut.s = "#000000"
 EndIf
 Message.s + "Text : " + sOut.s
 MessageRequester(Caption.s,Message.s,80)
 ProcedureReturn
EndProcedure

; Test Examples
GetTextColor(255,255,255)
GetTextColor(192,192,192)
GetTextColor(255,255,000)
GetTextColor(255,000,255)
GetTextColor(000,255,255)
GetTextColor(064,064,064)
GetTextColor(000,000,000)
If you have any type of color blindness, can you test the results for me ? Comment in this thread please !

Re: Is White/Black the best color for a specific BG color ?

Posted: Wed Apr 25, 2012 6:44 am
by Mohawk70
For those interested, the code snippet in the 1st post is used to automatically determine whether to use WHITE of BLACK text on any given background color. It is being used as part of a tool allowing quick generation of compatible colors for web designers to create color schemes. The screen shot below is what the work in progress looks like.

http://imageshack.us/f/846/colorschemerscreencap.png/

Re: Is White/Black the best color for a specific BG color ?

Posted: Sun Apr 29, 2012 10:47 am
by electrochrisso

Code: Select all

Global sOut

Procedure GetTextColor(rgbRed.i,rgbGrn.i,rgbBlu.i)
 brightness.f = Sqr( (0.299 * Pow(rgbRed,2)) + (0.587 * Pow(rgbGrn,2)) + (0.114 * Pow(rgbBlu,2)) )
 If brightness.f < 130
  sOut = $FFFFFF
 Else
  sOut = $0
 EndIf
 ProcedureReturn sOut
EndProcedure

OpenWindow(0,0,0,320,240,"Colour",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
StringGadget(0,10,10,100,20,"255,255,255")
ButtonGadget(1,120,10,50,20,"GO")
CreateImage(0,100,20)
ImageGadget(2,10,50,100,20,ImageID(0))

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          StartDrawing(ImageOutput(0))
            R=Val(StringField(GetGadgetText(0),1,","))
            G=Val(StringField(GetGadgetText(0),2,","))
            B=Val(StringField(GetGadgetText(0),3,","))
            GetTextColor(R,G,B)
            DrawingMode(#PB_2DDrawing_Transparent)
            Box(0,0,100,20,sOut)
            DrawText(0,0,"Testing",RGB(R,G,B))
          StopDrawing()
          SetGadgetState(2,ImageID(0))
      EndSelect
  EndSelect
Until Event = #PB_Event_CloseWindow

End
Had a bit of a play with your code, might come in use for someone. :)

Re: Is White/Black the best color for a specific BG color ?

Posted: Tue May 01, 2012 11:01 am
by Mohawk70
@electrochrisso
Nice example. That's exactly why I posted the code, so others can modify it for their own needs. :D