SRL Archive - Research & Development Lounge

SortColorsArr


Quickmarch

2009-06-03 19:34:31

Don't know whether it's worth putting it in a plugin, but here it is:


function SortColorsArr(Colors: TIntegerArray; WhiteFirst: Boolean): TIntegerArray;
var
i, c, L: Integer;
R, G, B, M: TIntegerArray;

begin
L := Length(Colors);
SetLength(Result, L);
SetLength(R, L);
SetLength(G, L);
SetLength(B, L);
SetLength(M, L);
for i := 0 to L - 1 do
begin
ColorToRGB(Colors[i], R[i], G[i], B[i]);
M[i] := R[i]+G[i]+B[i];
end;
QuickSort(M, Low(Colors), High(Colors), not(WhiteFirst));
for i := 0 to L - 1 do
for c := 0 to L - 1 do
if ((R[c] + G[c] + B[c]) = M[i]) then
Result[i] := RGBToColor(R[c], G[c], B[c]);
end;

(Delphi code btw ;))

It sorts the colors using RGB by the most white or the least white.

Use like this:


T := SortColorsArr([0, 16777215, 255, 16758374, 15790320, 65536], True);
WriteLn(IntToStr(T[0]));

Full source and plugin:

senrath

2009-06-03 20:09:45

Um, couldn't you use a better algorithm than Bubble Sort? 'Cause Bubble Sort is really inefficient.

Naum

2009-06-03 20:14:48

QuickSort maybe? Wait thats in scar.

Good work rep++

Quickmarch

2009-06-03 20:16:57

From: senrath
Um, couldn't you use a better algorithm than Bubble Sort? 'Cause Bubble Sort is really inefficient.


I've never used any others, whats the best one?

senrath

2009-06-03 20:20:20

There is no "best" one, but Bubble Sort is pretty much always the worst one. Try Heap Sort or Quick Sort.

Quickmarch

2009-06-03 23:05:29

Added QuickSort.

Gu3st...

2009-06-03 23:44:57

You could do
"L := Length(TIA);" It would make the procedure a little faster :)

Quickmarch

2009-06-06 20:58:12

Updated! Added Zyt3x's idea aswell as making it not export the TIntegerArray type which confuses scar.

Naum

2009-06-07 12:08:02

QuickMarch nice work, couldn't you have used the High() conversion?

Quickmarch

2009-06-07 16:09:27

From: NaumanAkhlaQ
QuickMarch nice work, couldn't you have used the High() conversion?


Cba to change now. :p