PDA

View Full Version : UpdateColorFromArray



n3ss3s
01-21-2008, 07:11 PM
Its back, better than last time ;)

I wanted to try and do it some other than the classic

"If Abs(HSL1 - HSL2) < Dif[x] Then..." from Hue to Lum :)


Fixed:


program new;

Function Mean(Values: TExtendedArray): Extended;
Var
I: Integer;
Begin
For I := 0 To High(Values) Do
Result := Result + Values[i];
Result := Result / GetArrayLength(Values);
End;


Function UpdateColorFromArray(Ref: Integer; Colors: TIntegerArray): Integer;
Var
Dif: Array of Array [0..2] Of Extended;
HSL, HSL2: Array [0..2] Of Extended;
SortedDif: TExtendedArray;
C, I, L, D: Integer;
R: Extended;
Begin
ColorToHSL(Ref, HSL[0], HSL[1], HSL[2]);
SetArrayLength(Dif, GetArrayLength(Colors));
L := High(Colors);
For I := 0 To L Do
Begin
ColorToHSL(Colors[i], HSL2[0], HSL2[1], HSL2[2]);
For C := 0 To 2 Do
Dif[i][c] := Abs(HSL[c] - HSL2[c]);
End;
L := High(Dif);
SetArrayLength(SortedDif, L + 1);
For D := 0 To L Do
SortedDif[d] := Mean([Dif[d][0], Dif[d][1], Dif[d][2]]);
R := AMinE(SortedDif);
For C := 0 To L Do
If SortedDif[c] = R Then
Begin
Result := Colors[c];
Exit;
End;
End;

begin
Writeln(IntToStr(UpdateColorFromArray(255, [210, 22, 244])));
End.



If the results don't meet what you want, change HSL to RGB or some other format...



Ehm - what it does :p

It picks the color closest to ref from the array you fill colors in - Colors.

It's useful for stuff like for example if you have a script, that "learns as it goes" (...like tarajunky?), you can then do a quick UpdateColorFromArray to nill your color array of all the past colors, and start all over again with a new fresh color :)

nielsie95
01-21-2008, 08:01 PM
I guess it's not working as designed? It always returns the first one in the array for me.

Writeln(IntToStr(UpdateColorFromArray(clBlack, [243, 242, 255])));
Writeln(IntToStr(UpdateColorFromArray(clRed, [243, 242, 255])));
Writeln(IntToStr(UpdateColorFromArray(clBlack, [255, 243, 242])));
Writeln(IntToStr(UpdateColorFromArray(clRed, [242, 242, 255])));

n3ss3s
01-22-2008, 12:06 PM
Aww crap, I forgot about that since the other version had another kind of bug -.-

Fixing..

Done :)