Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Determine when to use black or white as label forecolor
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Gestionnaire d'écran & Écrans
Versions des environnements
Visual FoxPro:
VFP 9
Divers
Thread ID:
01022663
Message ID:
01022826
Vues:
20
This message has been marked as a message which has helped to the initial question of the thread.
>Malcolm,
>
>I think you can convert the color number from RGB to Hue, Sat, Lum and I think either Sat or Lum is a single value that correlates to intensity. The GDI+ stuff might come in handy for this.
>
>>Just ran your example and yes, you are correct. Thanks for pointing that out.
>>
>>BTW: I'm still convinced there's a simple algorthim that can provide the answer. Just need to find it.
>>
>>Back to the drawing board!

There is an API function to do this, but unfortunately I forgot the name, but before knowing about this API, I found a microsoft article on how to change from RGB to HSL and I wrote a Color class to deal with several of this types of transformations, following are the 2 relevant for this. I use the Hue trick to highlight the selected record in a grid when the backcolor depends on the records, for example some records are red-ish some are green-ish etc, the to highlight the current color I change the HUE to make the selected record brighter.

Unfortunately I lost the link to the article where I found the algorithms
#Define _HLSMAX					240
#Define _RGBMAX					255
#Define _ACHROMATIC				(_HLSMAX * 2 / 3)

*+ hsl2rgbVal
Function hsl2rgbVal(tnHue, tnSat, tnLum)
	Local lnRed, lnGreen, lnBlue, lnHue, lnSat, lnLum, i, F, p, q, T, lnMagic1, lnMagic2

	If tnSat 	= 0			&& _ACHROMATIC
		lnLum		= Round(tnLum * _RGBMAX / _HLSMAX, 0)
		Return Rgb(lnLum, lnLum, lnLum)
	Endif
		* Chromatic
	lnLum			= tnLum
	lnSat			= tnSat
		* normalize hue to lie in 0<=h<360
	lnHue			= tnHue % 360
	lnHue			= Iif(lnHue < 0, lnHue * 360, 0) + lnHue
		* Set up magic numbers
	If (lnLum <= (_HLSMAX / 2))
		lnMagic2		= (lnLum * (_HLSMAX + lnSat) + (_HLSMAX / 2)) / _HLSMAX
	Else
		lnMagic2		= lnLum + lnSat - ((lnLum * lnSat) + (_HLSMAX / 2)) / _HLSMAX
	Endif
	lnMagic1		= 2 *lnLum - lnMagic2
	* Get RGB, change units from _HLSMAX to _RGBMAX
	lnRed			= (This.hue2rgbVal(lnMagic1, lnMagic2, lnHue + (_HLSMAX / 3)) * _RGBMAX +(_HLSMAX / 2)) / _HLSMAX
	lnGreen			= (This.hue2rgbVal(lnMagic1, lnMagic2, lnHue) * _RGBMAX + (_HLSMAX / 2)) / _HLSMAX
	lnBlue			= (This.hue2rgbVal(lnMagic1, lnMagic2, lnHue - (_HLSMAX/3)) * _RGBMAX +(_HLSMAX / 2)) / _HLSMAX

	* Adjust for round errors
	lnRed			= Int(Max(Min(lnRed, 255), 0))
	lnGreen			= Int(Max(Min(lnGreen, 255), 0))
	lnBlue			= Int(Max(Min(lnBlue, 255), 0))
	Return Rgb(lnRed, lnGreen, lnBlue)
Endfunc

*+ rgb2hsl
Function rgb2hsl(tnRed, tnGreen, tnBlue)
	Local lnMax, lnMin, lnHue, lnSaturation, lnLum, lnDiff, lnRDelta, lnGDelta, lnBDelta

	lnMax			= Max(tnRed, tnGreen, tnBlue)
	lnMin			= Min(tnRed, tnGreen, tnBlue)
	lnDiff			= (lnMax-lnMin)
	lnLum			= (((lnMax + lnMin) * _HLSMAX) + _RGBMAX) / (2 * _RGBMAX)

	If lnMax = lnMin 		&& _ACHROMATIC
		lnSaturation		= 0
		lnHue			= _ACHROMATIC
	Else				&& chromatic
		If (lnLum <= (_HLSMAX / 2))
			lnSaturation	= ((lnDiff * _HLSMAX) + ((lnMax + lnMin) / 2)) / (lnMax + lnMin)
		Else
			lnSaturation	= ((lnDiff * _HLSMAX) + (2 * _RGBMAX - lnMax - lnMin) / 2) / (2 * _RGBMAX - lnMax - lnMin)
		Endif
		lnRDelta		= (((lnMax - tnRed) * (_HLSMAX / 6)) + ((lnDiff) / 2)) / (lnDiff)
		lnGDelta		= (((lnMax - tnGreen) * (_HLSMAX / 6)) + ((lnDiff) / 2)) / (lnDiff)
		lnBDelta		= (((lnMax - tnBlue) * (_HLSMAX / 6)) + ((lnDiff) / 2)) / (lnDiff)
		Do Case
			Case tnRed = lnMax
				lnHue		= lnBDelta - lnGDelta
			Case tnGreen = lnMax
				lnHue		= (_HLSMAX / 3) + lnRDelta - lnBDelta
			Case tnBlue = lnMax
				lnHue		= ((2 * _HLSMAX) / 3) + lnGDelta - lnRDelta
		Endcase
		lnHue			= Int(lnHue) + Iif(lnHue < 0, _HLSMAX, Iif(lnHue > _HLSMAX, -_HLSMAX, 0))
	Endif
	Return Alltrim(Str(lnHue)) + ',' + Alltrim(Str(Int(lnSaturation))) + ',' + Alltrim(Str(Int(lnLum)))
Endfunc
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform