It is currently Tue May 21, 2013 11:50 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Is White/Black the best color for a specific BG color ?
PostPosted: Tue Apr 24, 2012 10:16 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu May 11, 2006 1:04 am
Posts: 309
Location: Phoenix,AZ USA
Code:
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 !

_________________
Programmer n. : A person having the mysterious ability to transform caffeine into code.

Boysenberry Pi www.BoysenberryPi.com
Tupponce Hosting www.TupponceHosting.com


Top
 Profile  
 
 Post subject: Re: Is White/Black the best color for a specific BG color ?
PostPosted: Wed Apr 25, 2012 6:44 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu May 11, 2006 1:04 am
Posts: 309
Location: Phoenix,AZ USA
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/

_________________
Programmer n. : A person having the mysterious ability to transform caffeine into code.

Boysenberry Pi www.BoysenberryPi.com
Tupponce Hosting www.TupponceHosting.com


Top
 Profile  
 
 Post subject: Re: Is White/Black the best color for a specific BG color ?
PostPosted: Sun Apr 29, 2012 10:47 am 
Offline
Enthusiast
Enthusiast

Joined: Mon May 14, 2007 2:13 am
Posts: 731
Location: Darling River
Code:
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 Rocks! Even More! And More!
PureBasic 5, Now We're Really Rockin!


Top
 Profile  
 
 Post subject: Re: Is White/Black the best color for a specific BG color ?
PostPosted: Tue May 01, 2012 11:01 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu May 11, 2006 1:04 am
Posts: 309
Location: Phoenix,AZ USA
@electrochrisso
Nice example. That's exactly why I posted the code, so others can modify it for their own needs. :D

_________________
Programmer n. : A person having the mysterious ability to transform caffeine into code.

Boysenberry Pi www.BoysenberryPi.com
Tupponce Hosting www.TupponceHosting.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye