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

Share your advanced PureBasic knowledge/code with the community.
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

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

Post 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 !
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

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

Post 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/
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

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

Post 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. :)
PureBasic! Purely the best 8)
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

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

Post by Mohawk70 »

@electrochrisso
Nice example. That's exactly why I posted the code, so others can modify it for their own needs. :D
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
Post Reply