n3ss3s
2008-03-18 15:06:40
NEW VERSION! HSL, RGB and XYZ Support!
I am sorry, but the RGB must be used as an Extended, because procedure pointers think like "Variant <> Extended", but it's not hard, red for example would be (255.0, 0.0, 0.0) :)
As far as I know, this works, I tested it with 0, 65536 which are both black, and 255 - red, and with the correct tolerances, as supposed to, when I writelned the length of the array it returns, it said 2.
0 and 65536 were one, and 255 one.
You who have been struggling with finding willows - your problems are over.
You can do a GetColors to a ATPA gotten with TPAToATPA(ex), and then put it through this with a quite small tol since the grass color in a block doesn't vary so much, if it returns 1 or so, it's grass ;)
The algorithm is based on Wizzie's TPAToATPAEx, so he can get half of the cookie :)
Glad to make an useful function for a while…
Now, go spread terror and mass destruction over the world… (with the function)
EDIT: You're free to use as long as you credit properly.
I am sorry, but the RGB must be used as an Extended, because procedure pointers think like "Variant <> Extended", but it's not hard, red for example would be (255.0, 0.0, 0.0) :)
As far as I know, this works, I tested it with 0, 65536 which are both black, and 255 - red, and with the correct tolerances, as supposed to, when I writelned the length of the array it returns, it said 2.
0 and 65536 were one, and 255 one.
You who have been struggling with finding willows - your problems are over.
You can do a GetColors to a ATPA gotten with TPAToATPA(ex), and then put it through this with a quite small tol since the grass color in a block doesn't vary so much, if it returns 1 or so, it's grass ;)
Program New;
Type
ColorSpace =(RGB, XYZ, HSL);
Procedure ConvertHSL(Color: Integer; Var H, S, L: Extended);
Begin
ColorToHSL(Color, H, S, L);
End;
Procedure ConvertRGB(Color: Integer; Var R, G, B: Extended);
Var
Re, Gr, Bl: Integer;
Begin
ColorToRGB(Color, Re, Gr, Bl);
R := Re;
G := Gr;
B := Bl;
End;
Procedure ConvertXYZ(Color: Integer; Var X, Y, Z: Extended);
Begin
ColorToXYZ(Color, X, Y, Z);
End;
Function RevertHSL(H, S, L: Extended): Integer;
Begin
Result := HSLToColor(H, S, L);
End;
Function RevertRGB(R, G, B: Extended): Integer;
Begin
Result := RGBToColor(Round(R), Round(G), Round(B));
End;
Function RevertXYZ(X, Y, Z: Extended): Integer;
Begin
Result := XYZToColor(X, Y, Z);
End;
Function SortColors(Colors: TIntegerArray; HRX, SGY, LBZ: Extended; CS: ColorSpace): T2DIntArray;
Var
aHSL: Array of Array [0..2] Of Extended;
fRes: Array Of Array of Array [0..2] Of Extended;
a, b, c, l, i: LongInt;
conversionProc: Procedure(Color: Integer; Var C1, C2, C3: Extended);
reversionFunc: Function(C1, C2, C3: Extended): Integer;
Begin
Case CS Of
RGB: Begin conversionProc := @ConvertRGB; reversionFunc := @RevertRGB; End;
HSL: Begin conversionProc := @ConvertHSL; reversionFunc := @RevertHSL; End;
XYZ: Begin conversionProc := @ConvertXYZ; reversionFunc := @RevertXYZ; End;
End
SetLength(fRes, 0);
SetLength(aHSL, Length(Colors));
L := High(Colors);
C := 0;
For I := 0 To L Do
conversionProc(Colors[i], aHSL[i][0], aHSL[i][1], aHSL[i][2]);
For a := 0 To L Do
begin
For b := 0 To c -1 Do
If (Abs(aHSL[a][0] - fRes[b][0][0]) <= HRX) And (Abs(aHSL[a][1] - fRes[b][0][1]) <= SGY)
And (Abs(aHSL[a][2] - fRes[b][0][2]) <= LBZ) Then
Break;
If b < c Then
begin
SetLength(fRes[b], Length(fRes[b]) + 1);
i := High(fRes[b]);
conversionProc(Colors[a], fRes[b][i][0], fRes[b][i][1], fRes[b][i][2]);
End Else
Begin
SetLength(fRes, c + 1);
SetLength(fRes[c], 1);
conversionProc(Colors[a], fRes[c][0][0], fRes[c][0][1], fRes[c][0][2]);
Inc(c);
End;
End;
L := High(fRes);
SetLength(Result, L + 1);
For I := 0 To L Do
Begin
b := High(fRes[i]);
SetLength(Result[i], b + 1);
For a := 0 To b Do
Result[i][a] := reversionFunc(fRes[i][a][0], fRes[i][a][1], fRes[i][a][2]);
End;
End;
Begin
Writeln(IntToStr(Length(SortColors([0, 65536, 255], 67.0, 100.0, 1.0, HSL))));
End.
The algorithm is based on Wizzie's TPAToATPAEx, so he can get half of the cookie :)
Glad to make an useful function for a while…
Now, go spread terror and mass destruction over the world… (with the function)
EDIT: You're free to use as long as you credit properly.