R0b0t1
2007-08-20 17:28:10
Yep, when pwnaz0r made RGB.scar it inspired me to make HSL.scar.
I'm pretty sure its done now, unless someone tells me something that could be fixed.
I'm pretty sure its done now, unless someone tells me something that could be fixed.
//-----------------------------------------------------------------//
//-- Scar Standard Resource Library --//
//-- » HSL Color Routines --//
//-----------------------------------------------------------------//
program HSLFunctions;
{.Include SRL/SRL.scar}
Type //Must be set. //Idea by pwnazor, I changed lots of vars.
TTol = record
Red, Green, Blue : Integer;
Hue, Sat, Lum: Extended;
end
Type //You will not need to set this.
TLoc = record
X, Y: Integer;
end;
Type //Must be set
THSL = record
Hue, Sat, Lum: Extended;
Loc: TLoc;
Tol: TTol;
end;
Type //Only used for special fucntions. See RGB.scar for RGB.
TRGB = record
Red, Green, Blue: Integer;
Loc: TLoc;
Tol: TTol;
end;
Function HSLToRGB2(TheHSL: THSL): TRGB;
begin;
HSLToRGB(TheHSL.Hue, TheHSL.Sat, TheHSL.Lum, Result.Red, Result.Green, Result.Blue);
end;
{*******************************************************************************
Function AddHSL(Color, LocX, LocY, HTol, STol, LTol: Integer): THSL;
By: R0b0t1
Credits: N/A
Description: Creates an HSL type with the parameters.
*******************************************************************************}
Function AddHSL(Color, LocX, LocY, HTol, STol, LTol: Integer): THSL;
begin;
ColorToHSL(Color, Result.Hue, Result.Sat, Result.Lum);
Result.Tol.Hue:= HTol;
Result.Tol.Sat:= STol;
Result.Tol.Lum:= LTol;
Result.Loc.X:= 0;
Result.Loc.Y:= 0;
end;
{*******************************************************************************
Function AddRGB(Color, LocX, LocY, RTol, GTol, BTol: Integer): TRGB;
By: R0b0t1
Credits: N/A
Description: Creates a RGB type with the parameters.
*******************************************************************************}
Function AddRGB(Color, LocX, LocY, RTol, GTol, BTol: Integer): TRGB;
begin;
ColorToRGB(Color, Result.Red, Result.Green, Result.Blue);
Result.Tol.Red:= RTol;
Result.Tol.Green:= GTol;
Result.Tol.Blue:= BTol;
Result.Loc.X:= LocX;
Result.Loc.Y:= LocY;
end;
{*******************************************************************************
Function ConvertRGBComp(Color: Integer; Component: String): Integer;
By: R0b0t1
Credits: N/A
Description: Converts a color to a sings RGB component (A letter).
*******************************************************************************}
Function ConvertRGBComp(Color: Integer; Component: String): Integer;
Var Rs, Gs, Bs: Integer;
begin;
ColorToRGB(Color, Rs, Gs, Bs);
Case LowerCase(Component) of
'red': Result:= Rs;
'green': Result:= Gs;
'blue': Result:= Bs;
end;
end;
{*******************************************************************************
Function ConvertHSLComp(Color: Integer; Component: String): Extended;
By: R0b0t1
Credits: N/A
Description: Converts color to a single HSL component (A letter).
*******************************************************************************}
Function ConvertHSLComp(Color: Integer; Component: String): Extended;
Var Hs, Ss, Ls: Integer;
begin;
ColorToHSL(Color, Hs, Ss, Ls);
Case LowerCase(Component) of
'hue': Result:= Hs;
'sat': Result:= Ss;
'lum': Result:= Ls;
end;
end;
{*******************************************************************************
Function CompareHSLComp(Color1, Color2: Integer): Array [0..2] of Extended;
By: R0b0t1
Credits: N/A
Description: Compares a color using the HSL components and return the tolerances
in an array (HTol, STol, LTol).
*******************************************************************************}
Function CompareHSLComp(Color1, Color2: Integer): Array [0..2] of Extended;
Hc1, Sc1, Lc1, Hc2, Sc2, Lc2, PH, PS, PL: Extended;
begin;
ColorToHSL(Color1, Hc1, Sc1, Lc1);
ColorToHSL(Color2, Hc2, Sc2, Lc2);
Result[0]:= MaxE(Hc1, Hc2) - MinE(Hc1, Hc2);
Result[1]:= MaxE(Sc1, Sc2) - MinE(Sc1, Sc2);
Result[2]:= MaxE(Lc1, Lc2) - MinE(Lc1, Lc2);
end;
{*******************************************************************************
Function CompareSinHSLComp(Color1, Color2: Integer; Component: String): Extended;
By: R0b0t1
Credits: N/A
Description: Compares two colors useing the HSL components and retuns the
tolerance of one of them
*******************************************************************************}
Function CompareSinHSLComp(Color1, Color2: Integer; Component: String): Extended;
Var Hc1, Sc1, Lc1, Hc2, Sc2, Lc2, PH, PS, PL: Extended;
begin;
ColorToHSL(Color1, Hc1, Sc1, Lc1);
ColorToHSL(Color2, Hc2, Sc2, Lc2);
Case LowerCase(Component) of
'hue':Result:= MaxE(Hc1, Hc2) - MinE(Hc1, Hc2);
'sat':Result:= MaxE(Sc1, Sc2) - MinE(Sc1, Sc2);
'lum':Result:= MaxE(Lc1, Lc2) - MinE(Lc1, Lc2);
end;
end;
{*******************************************************************************
Function BestColorHSL(Colors: TIntegerArray): THSL;
By: R0b0t1
Credits: Wizzup? For his bettercolor func which this is based off of.
Description: Picks the best color and make a THSL with the tol specified.
*******************************************************************************}
Function BestColorHSL(Colors: TIntegerArray; TolH, TolS, TolL: Integer;): THSL;
Var I: Integer;
Var H, S, L: Array of Extended;
Var bH, bS, bL: Extended;
begin;
SetLength(H, Colors);
SetLength(S, Colors);
SetLength(L, Colors);
For I:= 0 to Length(Colors) - 1 do
begin;
ColorToHSL(Colors[I]. H[I], S[I], L[I]);
bH:= bH + H[I];
bS:= bS + S[I];
bL:= bL + L[I];
end;
If (Length(Colors) < 0) Then
begin;
bH:= bH div Length(Colors);
bS:= bS div Length(Colors);
bL:= bl div Length(Colors);
end;
Result:= AddHSL(HSLToColor(bH, bS, bL), 0, 0, TolH, TolS, TolL);
end;
{*******************************************************************************
Function GetColorHSL(x, y: Integer): THSL;
By: R0b0t1
Credits: N/A
Description: Gets color at xy an returns as HSL.
*******************************************************************************}
Function GetColorHSL(x, y: Integer): THSL;
begin;
Result:= AddHSL(GetColor(x, y), x, y, 0, 0, 0);
end;
{*******************************************************************************
Function FindHSLTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL): Boolean;
By: R0b0t1
Credits: N/A
Description: Finds an HSL with the tolerances
*******************************************************************************}
Function FindHSLTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColor(X, Y, HSLCol, x1, y1, x2, y2);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColor(X, Y, HSLCol, x1, y1, x2, y2);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLSpiralTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLSpiralTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL:Integer): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColorSpiral(X, Y, HSLCol, x1, y1, x2, y2);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColorSpiral(X, Y, HSLCol, x1, y1, x2, y2);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLSpiralTol2(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLSpiralTol2(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColorSpiral2(X, Y, HSLCol, x1, y1, x2, y2);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColorSpiral2(X, Y, HSLCol, x1, y1, x2, y2);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLCircleTol(var X, Y: Integer; TheHSL: THSL; midX, midY, Radius, TolH, TolS, TolL: Integer): Boolean;
By: R0b0t1
Credits: N/A
Description: Works like find color circles but with HSL
*******************************************************************************}
Function FindHSLCircleTol(var X, Y: Integer; TheHSL: THSL; midX, midY, Radius, TolH, TolS, TolL: Integer): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColorCircle(X, Y, HSLCol, midX, midY, Radius);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColorCircle(X, Y, HSLCol, midX, midY, Radius);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLTriangleTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, x3, y3, TolH, TolS, TolL: Integer): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLTriangleTol(var X, Y: Integer; x1, y1, x2, y2, x3, y3, TolH, TolS, TolL: Integer): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColorTriangle(X, Y, HSLCol, x1, y1, x2, y2, x3, y1);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColorTriangle(X, Y, HSLCol, x1, y1, x2, y2, x3, y1);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLAreaTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, MinArea, TolH, TolS, TolL: Integer): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLAreaTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2. MinArea, TolH, TolS, TolL: Integer): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColoredAreaTolerance(X, Y, HSLCol, x1, y1, x2, y2, MinArea, 0);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColoredAreaTolerance(X, Y, HSLCol, x1, y1, x2, y2, MinArea, 0);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLSkipBoxTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer; SkipBox: TBox): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLSkipBoxTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer; SkipBox: TBox): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColorSkipBox(X, Y, HSLCol, x1, y1, x2, y2, SkipBox);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColorSkipBox(X, Y, HSLCol, x1, y1, x2, y2, SkipBox);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLSkipBoxArrayTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer; SkipBoxes: TBoxArray): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLSkipBoxArrayTol(var X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer; SkipBoxes: TBoxArray): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColorSkipBoxArray(X, Y, HSLCol, x1, y1, x2, y2, SkipBoxes);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColorSkipBoxArray(X, Y, HSLCol, x1, y1, x2, y2, SkipBoxes);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLSkipCoordsTol(X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer; SkipPoints: TPointArray): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLSkipCoordsTol(X, Y: Integer; TheHSL: THSL; x1, y1, x2, y2, TolH, TolS, TolL: Integer; SkipPoints: TPointArray): Boolean;
Var I, B, C, HSLCol: Integer;
begin;
For I:= 0 to TolH do
begin;
For B:= 0 to TolS do
begin;
For C:= 0 to TolL do
begin;
HSLCol:= HSLToColor(TheHSL.Hue+I, TheHSL.Sat+B, TheHSL.Lum+C);
Result:= FindColorSkipCoords(X, Y, HSLCol, x1, y1, x2, y2, SkipPoints);
If (Result) Then Exit;
HSLCol:= HSLToColor(TheHSL.Hue-I, TheHSL.Sat-B, TheHSL.Lum-C);
Result:= FindColorSkipCoords(X, Y, HSLCol, x1, y1, x2, y2, SkipPoints);
If (Result) Then Exit;
end;
end;
end;
end;
{*******************************************************************************
Function FindHSLComp(var X, Y: Integer; x1, y1, x2, y2: Integer; Value: Extended; Component: String): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLComp(var X, Y: Integer; x1, y1, x2, y2: Integer; Value: Extended; Component: String): Boolean;
begin;
Case LowerCase(Component) of
'hue': Result:= FindHue(x, y, Value, x1, y1, x2, y2);
'sat': Result:= FindSaturation(x, y, Value, x1, y1, x2, y2;
'lum': Result:= FindLuminance(x, y, Value, x1, y1, x2, y2);
end
end;
{*******************************************************************************
Function FindHSLCompTol(var X, Y: Integer; x1, y1, x2, y2: Integer; Value, Tol: Extended; Component: String): Boolean;
By: R0b0t1
Credits: N/A
Description:
*******************************************************************************}
Function FindHSLCompTol(var X, Y: Integer; x1, y1, x2, y2: Integer; Value, Tol: Extended; Component: String): Boolean;
begin;
Case LowerCase(Component) of
'hue': Result:= FindHueTolerance(x, y, Value, x1, y1, x2, y2, Tol);
'sat': Result:= FindSaturationTolerance(x, y, Value, x1, y1, x2, y2, Tol);
'lum': Result:= FindLuminanceTolerance(x, y, Value, x1, y1, x2, y2, Tol);
end
end;
begin
end.