SRL Archive - Test corner

Thought I would share...


n3ss3s

2007-12-20 06:24:16

this small autocolor proc.

Now, lets say that you are finding a color.

Every succesfull time found you save the color in an array.

Now, here is the thing - you don't do for loops for finding the color like
FindColor(X, Y, c, x1, y1, x2, y2)

Lets say, every fifth time a color is succesfully found, you do

"UpdateCFromArray(RefColor, ColorsArray);"

and voila, you have the color closest to RefColor.

:) Simple, effective.


Procedure UpdateCFromArray(Var Color: Integer; Colors: TIntegerArray);
Var
I, LTol, C, N, L: Integer;
Begin
LTol := iAbs(Color - Colors[0]);
N := 0;
L := High(Colors);
For I := 0 To L Do
Begin
C := iAbs(Color - Colors[i]);
If C < LTol Then
Begin
N := i;
LTol := c;
End;
End;
Color := Colors[n];
End;

Santa_Clause

2007-12-20 06:46:10

This one's not Santy-Proof, but I won't be bothered shortening :p

Awesome job n3ss3s.

n3ss3s

2007-12-20 06:52:25

Thanks, btw, only thing you could take away is the "N := 0;"

Santa_Clause

2007-12-20 07:07:40

No…:p Oh, I'm only joking around when I shorten your functions by the way.

 L := High(Colors);
For I := 0 To L Do

Can be:

For I := 0 To High(Colors) Do

bullzeye95

2007-12-20 07:48:58

But that would make it slower, since it has to call High() every time it goes through the loop.

N3ss3s, what's the concept of this? I'm not understanding names like i, c, and n :p

n3ss3s

2007-12-20 08:17:06

You mean what it does?
Sorry, your English > mine…

First, it gets the difference between the ref color and the Colors[0].

Then, it loops through the array and when the difference between ref color and a color is smaller than the earlier one, it will set the "LTol" to the difference, and set "N" as the array index the int was in and does that until in end of array.

When gone through the array, it sets the ref color to the Colors[n] :)

Lalaji

2007-12-20 09:09:08

Nice, Didnt Tara have something like thing aswell?

SmartColors?

n3ss3s

2007-12-20 09:48:07

Don't know, haven't looked in his code too deep, but I think he just adds succesful colors to an array and then has a for loop…

Markus

2007-12-20 12:21:03

Color1: R: 255, G: 0, B: 0 = 255
Color2: R: 1, G: 0, B: 1 = 65537
RefColor: R: 1, G: 0, B: 0 = 1

It'll get Color1 I think, but the closest one is actually color 2 -.-
Summary: don't use iAbs for color comparing.

n3ss3s

2007-12-20 12:36:01

Yeah, I should change it to something a tad more advanced, but, will do for stuff like

GetColor(X1, Y1) would be 1001
GetColor(X2, Y2) would be 1002
RefColor would be 1000

It would get the 1001, which is what I want :)