Lorax
03-08-2007, 06:35 PM
Hi I'm Lorax, as any of you with more scripting knowledge knows, I'm a big fan of HSL (Hue, saturation, luminance).. I use it mainly for finding middle of objects as to use HSL is a great way for finding objects..
I have been asked to make this tutorial several times, and I know I'm not good at writing tutorials, but I will try anyway..
Introduction to HSL:
HSL is a way of defining colours. Just like RGB, Hex and the colours you use in SCAR (Color32)..
In RGB every colour has a Red value, a Green value and a Blue value.. And with a mix of those 3 values you can create any colour.. as some of you know RGB values range from 0-225..
HSL<>RGB:
RGB uses Integer values (jf. 0-225), but HSL uses floating points and ranges from (0.0-100.0)
When you set Red to 255, Green to 0 and Blue to 0 in RGB you will get the colour 0..
In HSL, it's the variable Hue which determines what colour you are having..
Examples using a color converter:
R 255, G 0, B 0 --> H 0.0, S 50.0, L 100.0
R 0, G 255, B 0 --> H 33.33, S 50.0, L100.0
What colour lies where is rather irelevant, but I can tell you that HSL is made like a circle and therefor Hue 100.0 and 0.0 is allmost the same color..
So Hue is the colour, what are the remaining two variables then?
Let's start of by talking about Luminance..
Luminance is (what the word suggests) lightness of the color..
If you set the Luminance to 100.0 you would get white and if you set it to 0.0 you would get black.. all Luminance values in between is simply a color with a lightness grade between black and white..
Saturation?
Saturation is what I prefer to call as "Grayness".. it simply shows how gray our color is..
if you set Saturation to 0.0 you would get the color gray no matter the Hue parameter.. if you set Saturation to 100.0 you would get a clean color, like 255, 0, 0..
So great, now I know what HSL is that's fine - but what do I use it for?
Don't make such stupid questions :)
I can tell you that Luminance can be used for detecting lines, because when there is a line you will see a radical change in Luminance..
All in all, HSL gives us some very neat variables to work on, and try to use a Color picker (with HSL) and run over for example a rock..
That's how I knew how I would make my findMiddle (of rock) procedure in my newest powerminer.. --> when a rock ends there's a radical change in Saturation..
Simply go ahead and explore HSL.. it's a fun tool..
And some fun, code :)
function SLFindMiddleOfTree(var ax, ay: Integer; tColor: Integer): Boolean;
var
Count, x1, y1, x2, y2, bCount, aCount: Integer;
arLuminance: array[0..1] of Extended;
Hue, Saturation: Extended;
begin
if(ax < 10)then
ax := 10;
if(ay < 10)then
ay := 10;
if(FindColorSpiralTolerance(ax, ay, tColor, ax - 10, ay - 10, ax + 10, ay + 10, 5))and
(LoggedIn)then
begin
for bCount := 0 to 3 do
begin
ColorToHSL(GetColor(ax, ay), Hue, Saturation, arLuminance[1]);
Count := 0;
if(bCount = 2)then
ax := (x2 + x1) div 2;
repeat
arLuminance[0] := arLuminance[1];
case bCount of
0: ColorToHSL(GetColor(ax - Count, ay), Hue, Saturation, arLuminance[1]);
1: ColorToHSL(GetColor(ax + Count, ay), Hue, Saturation, arLuminance[1]);
2: ColorToHSL(GetColor(ax, ay - Count), Hue, Saturation, arLuminance[1]);
3: ColorToHSL(GetColor(ax, ay + Count), Hue, Saturation, arLuminance[1]);
end;
if(Hue > 14)and(Hue < 34)then
begin
if(arLuminance[0] < arLuminance[1] - 1.5)or
(arLuminance[0] > arLuminance[1] + 1.5)then
aCount := 0
else
aCount := aCount + 1;
end else
aCount := aCount + 1;
if(aCount = 3)then
begin
case bCount of
0: x1 := ax - Count + 3;
1: x2 := ax + Count - 3;
2: y1 := ay - Count + 3;
3: y2 := ay + Count - 3;
end;
end;
Count := Count + 1;
until(Count = 60)or(aCount = 3);
if(Count = 60)then
begin
case bCount of
0: x1 := ax - Count;
1: x2 := ax + Count;
2: y1 := ay - Count;
3: y2 := ay + Count;
end;
end;
end;
ay := (y2 + y1) div 2;
if((x2 - x1) * (y2 - y1) >= MinWCount * MinHCount)then
Result := True;
end;
end;
This is what I use in our wood cutter for finding the middle of a tree, it's simple but works great..
Now go ahead and make your own object finders using HSL..
I myself have allready made (A tree middle finder, a rock middle finder, a fishing spot middle finder)..
I have been asked to make this tutorial several times, and I know I'm not good at writing tutorials, but I will try anyway..
Introduction to HSL:
HSL is a way of defining colours. Just like RGB, Hex and the colours you use in SCAR (Color32)..
In RGB every colour has a Red value, a Green value and a Blue value.. And with a mix of those 3 values you can create any colour.. as some of you know RGB values range from 0-225..
HSL<>RGB:
RGB uses Integer values (jf. 0-225), but HSL uses floating points and ranges from (0.0-100.0)
When you set Red to 255, Green to 0 and Blue to 0 in RGB you will get the colour 0..
In HSL, it's the variable Hue which determines what colour you are having..
Examples using a color converter:
R 255, G 0, B 0 --> H 0.0, S 50.0, L 100.0
R 0, G 255, B 0 --> H 33.33, S 50.0, L100.0
What colour lies where is rather irelevant, but I can tell you that HSL is made like a circle and therefor Hue 100.0 and 0.0 is allmost the same color..
So Hue is the colour, what are the remaining two variables then?
Let's start of by talking about Luminance..
Luminance is (what the word suggests) lightness of the color..
If you set the Luminance to 100.0 you would get white and if you set it to 0.0 you would get black.. all Luminance values in between is simply a color with a lightness grade between black and white..
Saturation?
Saturation is what I prefer to call as "Grayness".. it simply shows how gray our color is..
if you set Saturation to 0.0 you would get the color gray no matter the Hue parameter.. if you set Saturation to 100.0 you would get a clean color, like 255, 0, 0..
So great, now I know what HSL is that's fine - but what do I use it for?
Don't make such stupid questions :)
I can tell you that Luminance can be used for detecting lines, because when there is a line you will see a radical change in Luminance..
All in all, HSL gives us some very neat variables to work on, and try to use a Color picker (with HSL) and run over for example a rock..
That's how I knew how I would make my findMiddle (of rock) procedure in my newest powerminer.. --> when a rock ends there's a radical change in Saturation..
Simply go ahead and explore HSL.. it's a fun tool..
And some fun, code :)
function SLFindMiddleOfTree(var ax, ay: Integer; tColor: Integer): Boolean;
var
Count, x1, y1, x2, y2, bCount, aCount: Integer;
arLuminance: array[0..1] of Extended;
Hue, Saturation: Extended;
begin
if(ax < 10)then
ax := 10;
if(ay < 10)then
ay := 10;
if(FindColorSpiralTolerance(ax, ay, tColor, ax - 10, ay - 10, ax + 10, ay + 10, 5))and
(LoggedIn)then
begin
for bCount := 0 to 3 do
begin
ColorToHSL(GetColor(ax, ay), Hue, Saturation, arLuminance[1]);
Count := 0;
if(bCount = 2)then
ax := (x2 + x1) div 2;
repeat
arLuminance[0] := arLuminance[1];
case bCount of
0: ColorToHSL(GetColor(ax - Count, ay), Hue, Saturation, arLuminance[1]);
1: ColorToHSL(GetColor(ax + Count, ay), Hue, Saturation, arLuminance[1]);
2: ColorToHSL(GetColor(ax, ay - Count), Hue, Saturation, arLuminance[1]);
3: ColorToHSL(GetColor(ax, ay + Count), Hue, Saturation, arLuminance[1]);
end;
if(Hue > 14)and(Hue < 34)then
begin
if(arLuminance[0] < arLuminance[1] - 1.5)or
(arLuminance[0] > arLuminance[1] + 1.5)then
aCount := 0
else
aCount := aCount + 1;
end else
aCount := aCount + 1;
if(aCount = 3)then
begin
case bCount of
0: x1 := ax - Count + 3;
1: x2 := ax + Count - 3;
2: y1 := ay - Count + 3;
3: y2 := ay + Count - 3;
end;
end;
Count := Count + 1;
until(Count = 60)or(aCount = 3);
if(Count = 60)then
begin
case bCount of
0: x1 := ax - Count;
1: x2 := ax + Count;
2: y1 := ay - Count;
3: y2 := ay + Count;
end;
end;
end;
ay := (y2 + y1) div 2;
if((x2 - x1) * (y2 - y1) >= MinWCount * MinHCount)then
Result := True;
end;
end;
This is what I use in our wood cutter for finding the middle of a tree, it's simple but works great..
Now go ahead and make your own object finders using HSL..
I myself have allready made (A tree middle finder, a rock middle finder, a fishing spot middle finder)..