Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Determine Contrasting/Inverse Colors
Message
From
22/05/2009 20:09:45
 
 
To
22/05/2009 19:41:12
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
Miscellaneous
Thread ID:
01401618
Message ID:
01401620
Views:
98
This message has been marked as the solution to the initial question of the thread.
>I need to be able to determine the forecolor when a user selects the backcolor. This is to be able to display the text as readable based on what the backcolor is. Just taking the inverse color works for the outer color spectrum but when it is in the middle this does not work well.
>
>Does anyone have any ideas on how to calculate the best readable forecolor based on the backcolor setting? Thanks.

I got this from a c# method on CodeProject:

http://www.codeproject.com/KB/GDI-plus/IdealTextColor.aspx
oform = CREATEOBJECT("colorform")
oform.show()
read events


DEFINE CLASS colorform AS form


	Top = 0
	Left = 0
	DoCreate = .T.
	Caption = "Determine Ideal ForeColor"
	Name = "colorform"


	ADD OBJECT text1 AS textbox WITH ;
		Height = 23, ;
		Left = 48, ;
		Top = 48, ;
		Width = 264, ;
		Name = "Text1"


	ADD OBJECT command1 AS commandbutton WITH ;
		Top = 156, ;
		Left = 108, ;
		Height = 27, ;
		Width = 132, ;
		Caption = "Set TextBox ForeColor", ;
		Name = "Command1"


	ADD OBJECT command2 AS commandbutton WITH ;
		Top = 120, ;
		Left = 108, ;
		Height = 27, ;
		Width = 132, ;
		Caption = "Set TextBox BackColor", ;
		Name = "Command2"


	PROCEDURE color2rgb
		*PROCEDURE color2rgb
		lparameters RGBcolor
		return "rgb("+;
			ltrim(str(bitand(RGBColor,0x000000ff)))+","+ ;
			ltrim(str(bitrshift(bitand(RGBColor,0x0000ff00),8)))+","+ ;
			ltrim(str(bitrshift(bitand(RGBColor,0x00ff0000),16)))+")"
	ENDPROC


	PROCEDURE color2rgb1
		*PROCEDURE color2rgb1
		LPARAMETERS RGBColor
		return bitand(RGBColor,0x000000ff)
	ENDPROC


	PROCEDURE color2rgb2
		*PROCEDURE color2rgb2
		LPARAMETERS RGBColor
		return bitrshift(bitand(RGBColor,0x0000ff00),8)
	ENDPROC


	PROCEDURE color2rgb3
		*PROCEDURE color2rgb3
		LPARAMETERS RGBColor
		return bitrshift(bitand(RGBColor,0x00ff0000),16)
	ENDPROC


	PROCEDURE rgb2color
		*function rgbToColor
		LPARAMETERS lcRgb
		LOCAL laColors

		DIMENSION laColors[3]
		IF ALINES(laColors, STRTRAN(lcRgb, ",", CHR(13)+CHR(10))) <> 3
			ERROR "Invalid RGB code"
		ELSE
			ACTIVATE SCREEN
			?RGB(VAL(laColors[1]), VAL(laColors[2]), VAL(laColors[3]))
			RETURN RGB(VAL(laColors[1]), VAL(laColors[2]), VAL(laColors[3]))
		ENDIF
	ENDPROC


	PROCEDURE Init
		DODEFAULT()
		thisform.text1.value = "Example Text"
		thisform.text1.refresh()
	ENDPROC


	PROCEDURE Destroy
		DODEFAULT()
		CLEAR EVENTS
	ENDPROC


	PROCEDURE command1.Click
		lncolorback = thisform.text1.backcolor
		nThreshold = 105
		bgDelta1 = thisform.Color2rgb1(lncolorback)
		bgDelta2 = thisform.Color2rgb2(lncolorback)
		bgDelta3 = thisform.Color2rgb3(lncolorback)
		bgDelta1 = bgDelta1*0.299
		bgDelta2 = bgDelta2*0.587
		bgDelta3 = bgDelta3*0.114
		bgDelta = bgDelta1+bgDelta2+bgDelta3

		IF 255-bgDelta>nThreshold
			thisform.text1.forecolor = thisform.RGB2Color("RGB(255,255,255)")
		ELSE
			thisform.text1.forecolor = thisform.RGB2Color("RGB(0,0,0)")
		ENDIF
		THISFORM.text1.refresh()
		RETURN
	ENDPROC


	PROCEDURE command2.Click
		thisform.text1.backcolor = GETCOLOR()
		thisform.text1.refresh()
	ENDPROC


ENDDEFINE
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform