Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Color generation Part II
Message
From
12/04/2002 10:26:58
 
 
To
11/04/2002 14:11:41
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00643022
Message ID:
00644274
Views:
20
Hi!

Well, I just looked into the LViewPro and 2.0 and see that the model they propose are for colours only. There is a difficulty to select gray colour in the colours selector they provided. I do nto know about later versions. When you switch to the HSL mode, I see that vertically they change Hue and horisontally both Sat and Lum values are changed. This produces a table of colours that is not complete (no grey colours with all 3 values equal). For example, I do not see a way there to pick up colour RGB(128,128,128) that is a pure grey colour. You can add a line of grey colours to your table separartely.

Again, think about this more. You have 3 dimensions in here and you want to represent them in 2 dimentions table. RGB is obviously is not a good thing, because third dimension will be represented as a sequence of blocks, as in my sample.

LViePro did this another way - they just used Lum and Sat (brightness and contrast) together as a single dimention, but this caused lose of many useful colours.

Windows color picker dialog box (GetColor() function in VFP) use 3 dimentions with no a single problem.

Just look what is betetr for you. Below is some sort of solution that uses approach from LViewPro, but also adds columns with a lot of "missed" colours, iclude greyscale colours. I used following article from MSDN:

<<<<<<<<<<<<<<<<<
HOWTO: Converting Colors Between RGB and HLS (HBS)

Q29240


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Windows Software Development Kit (SDK) 3.1
Microsoft Win32 Application Programming Interface (API), used with:
Microsoft Windows NT Server versions 3.5, 3.51, 4.0
Microsoft Windows NT Workstation versions 3.5, 3.51, 4.0
Microsoft Windows 95

--------------------------------------------------------------------------------


SUMMARY
The code fragment below converts colors between RGB (Red, Green, Blue) and HLS/HBS (Hue, Lightness, Saturation/Hue, Brightness, Saturation).



MORE INFORMATION
RGBtoHLS() takes a DWORD RGB value, translates it to HLS, and stores the results in the global vars H, L, and S. HLStoRGB takes the current values of H, L, and S and returns the equivalent value in an RGB DWORD.
............ Skip ...............
<<<<<<<<<<<<<<<<<

Below is a translated code for functions, as well as a sample array with colours. I displayed result in the grid, aC array stores color integer values - used in DynamicBackColor, and aN stores the character string in form (RRR,GGG,BBB) - what is displayed in the grid. If needed, I will send you the form I used, but it is very simple - create cursor with only integer field in Load event, put grid on form with record source pointed to that cursor and with a single column and thats all. The rest of code is below.

As you see, I added a LOT of colors that are missed in the usual table in LViewPro. Looks much better. You can re-sample it to what you need by adjusting constants below.
* Init event of the form - prepare color arrays 
HLSMAX = 480

* it is recommended to make BaseColours and ShadowColours divisible by 6 and number of Brightnesses is an even number.
BaseColours = 24
ShadowColours = 12
Brightnesses = 32

* aC are colours, aN - appropriate RGB representation of them in character string
public array aC(Brightnesses-1,BaseColours+ShadowColours+1), aN(Brightnesses-1,BaseColours+ShadowColours+1)

* prepare base (rainbow) colors
* note that we skip the 0 and 32 brightness to do not show RGB(0,0,0) and RGB(255,255,255) in all 24 columns in the first row
for Hue = 1 to BaseColours 
	for Lum = 1 to Brightnesses - 1 
		nColor = this.HLStoRGB( (Hue-1)*(HLSMAX/BaseColours ), Lum*(HLSMAX/Brightnesses ), HLSMAX-abs(Lum*(HLSMAX/Brightnesses)*2 - HLSMAX))
		aC[Lum, Hue] = nColor
		aN[Lum, Hue] = allt(str(nColor % 256)) + "," + ;
			allt(str(int(nColor/256) % 256)) + "," + allt(str(int(nColor/65536) % 256))
	endfor
endfor
* prepare other (shadow) colors
for Sat = BaseColours+1 to BaseColours + ShadowColours
	for Lum = 1 to Brightnesses - 1 
		nColor = this.HLStoRGB( (Sat-BaseColours-1)*(HLSMAX/ShadowColours ), Lum*(HLSMAX/Brightnesses ), abs(Lum*(HLSMAX/Brightnesses )*2 - HLSMAX))
		aC[Lum, Sat] = nColor
		aN[Lum, Sat] = allt(str(nColor % 256)) + "," + ;
			allt(str(int(nColor/256) % 256)) + "," + allt(str(int(nColor/65536) % 256))

	endfor
endfor
* prepare greyscale colors. Note the difference - to show black and white colours we divide brightness by another way
for Lum = 1 to Brightnesses - 1 
	nColor = min((Lum-1) * (256/(Brightnesses-2)),255)
	nColor = RGB(nColor,nColor,nColor)
	aC[Lum, BaseColours+ShadowColours+1] = nColor
	aN[Lum, BaseColours+ShadowColours+1] = allt(str(nColor % 256)) + "," + ;
		allt(str(int(nColor/256) % 256)) + "," + allt(str(int(nColor/65536) % 256))
endfor


* prepare cursor and grid to display colours
for i=1 to Brightnesses - 1 
	insert into aaa values(i)
endfor
go top in aaa

with this.Grid1
	.ColumnCount = BaseColours+ShadowColours+1
	for i = 1 to .ColumnCount
		.columns(i).ControlSource = "aN[aaa.nIndex," + allt(str(i)) + "]"
		.columns(i).DynamicBackColor = "aC[aaa.nIndex," + allt(str(i)) + "]"
		.columns(i).Sparse = .T.
	endfor
endwith
* HLStoRGB method
lparameters hue, lum, sat

local RR, GG, BB
local Magic1, Magic2
local HLSMAX, RGBMAX
HLSMAX = 480
RGBMAX = 256

if sat=0
	store int((lum*RGBMAX)/HLSMAX) to RR,GG,BB
else
	if lum <= int(HLSMAX/2)
		Magic2 = int((lum*(HLSMAX + sat) + int(HLSMAX/2)) / HLSMAX)
	else
		Magic2 = lum + sat - int(((lum*sat) + int(HLSMAX/2))/HLSMAX)
	endif
    Magic1 = 2*lum-Magic2

    RR = int((this.HueToRGB(Magic1,Magic2,hue+int(HLSMAX/3))*RGBMAX + int(HLSMAX/2))/HLSMAX)
	GG = int((this.HueToRGB(Magic1,Magic2,hue)*RGBMAX + (HLSMAX/2)) / HLSMAX)
	BB = int((this.HueToRGB(Magic1,Magic2,hue-int(HLSMAX/3))*RGBMAX + int(HLSMAX/2))/HLSMAX)
endif
return RGB(min(RR,255),min(GG,255),min(BB,255))
* HUEtoRGB method
lparameters n1,n2,hue
local HLSMAX
HLSMAX = 480
if hue < 0
	hue = hue + HLSMAX
endif
if hue > HLSMAX
	hue = hue - HLSMAX
endif
if (hue < int(HLSMAX/6))
	return ( n1 + int(((n2-n1)*hue+int(HLSMAX/12))/int(HLSMAX/6)) )
endif
if (hue < int(HLSMAX/2))
	return ( n2 )
endif
if (hue < int((HLSMAX*2)/3))
	return ( n1 + int(((n2-n1)*(int((HLSMAX*2)/3)-hue)+int(HLSMAX/12))/(HLSMAX/6)) )
else
	return ( n1 )
endif
>>As I said, try to find a formula to convert Hue/Sat/Lum parameters for color to RGB. This will give you a smooth colours approach.
>
>I worked on that yesterday and was not able to find the proper way of doing it. The actual color picker is the one which I found the best so far. Of course, if I could succeed in the LViewPro type approach, that'd be perfect. It's just a question of knowing the proper way to do it.
Vlad Grynchyshyn, Project Manager, MCP
vgryn@yahoo.com
ICQ #10709245
The professional level of programmer could be determined by level of stupidity of his/her bugs

It is not appropriate to say that question is "foolish". There could be only foolish answers. Everybody passed period of time when knows nothing about something.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform