SRL Archive - SRL Archives

RGB.scar


pwnaz0r

2007-08-21 18:21:40

RGB.scar - A Work in Progress



Helpful Links and Scripts Using RGB.scar:

Updates:
  • 8/14/2007 - Project Started
  • 8/17/2007 - Project Beta Released
  • 8/17/2007 - RGB Variable better Utilized
  • 8/18/2007 - FindClosestRGB Released
  • 8/18/2007 - FindRGBTolerance Released / RGB Variabel ReVamped

About The Project:

Well in an effort to promote the use of RGB and to inform scripters of its great uses, I have decided to at least start on the project of bringing RGB colorfinding into the SRL include. The initial beta version only includes the creation of the RGB variable and a few RGB functions that may prove useful. I have only set out the tools, now you, the scripters can use them :).

RGB is useful for many things but the thing I personally believe it to be best for it Autocoloring and Finding tolerance faster.

Goals -
  • Moderately Increase AutoColoring Techniques [X]
  • Increase Color Tolerance Finding Techniques [X]
  • Finding the center of Colors, such as trees [ ]
  • RGB Variable [X]
More to come…

What it Includes
  • RGB Variable
  • CreateRGB
  • CompareColorsRGBEx
  • CompareColorsRGB
  • CompareColorArrayRGB
  • AutoColorMM
  • MinArray
  • FindClosestRGB
  • ConvertRGB
  • FindRGBTolerance

To find out how these procedures and functions work, please refer to the tutorial at the top.

Things to be added:
  • Experemental Colorfinding
  • This is one of my favorites. Its something I thought of while thinking of ways to speed up the colorfinding. It looks great and it will soon be released!
  • RGB Component Creator
  • This is on its way also. Will be just like RadialWalk Helper, etc.
  • Much More!

//-----------------------------------------------------------------//
//-- Scar Standard Resource Library --//
//-- » RGB Color Routines --//
//-----------------------------------------------------------------//
// » function CreateRGB(red, green, blue : integer) : RGB;
// » function CompareColorsRGBEx(color1ex,color2ex: integer) : array of integer;
// » function CompareColorsRGB(color1, color2 : integer; reportit : boolean) : array of integer; | By Pwnaz0r
// » function CompareColorArrayRGB(colors : array of integer; reportIt : boolean) : array of integer; | By Pwnaz0r
// » function Procedure AutoColorMM(var ResultColor : integer; Color : integer; Colors : array of integer); | By Pwnaz0r
// » function MinArray(var int : integer; thearray : array of integer) : integer;
// » function FindClosestRGB(R, G, B, x1, y1, x2, y2 : Integer) : Integer; | By Pwnaz0r
// » function ConvertRGB(Name : string; Color : integer) : Integer; | By Pwnaz0r
// » function FindRGBTolerance(var x,y, RsltColor : integer; Red, Green, Blue, RTol, GTol, BTol, x1, y1, x2, y2 : integer) : boolean; | By Pwnaz0r


Type
TTol = record
Red : Integer;
Green : Integer;
Blue : Integer;
end;

Type
TRGB = record
Red : Integer;
Green : Integer;
Blue : Integer;
X : Integer; // used for position purposes
Y : Integer; // ''
Tolerance : TTol;
end;

{*******************************************************************************
function CreateRGB(Red, Green, Blue : integer) : RGB;
By: Pwnaz0r / Slightly Modified by Iloveit8
Description: Creates an RGB Component.
*******************************************************************************}
function CreateRGB(color : integer) : TRGB;
begin
ColorToRGB(Color, result.Red, result.Green, result.Blue);
Result.X := 0;
Result.Y := 0;
Result.Tolerance.Red := 0;
Result.Tolerance.Green := 0;
Result.Tolerance.Blue := 0;
end;

{*******************************************************************************
function CompareColorsRGBEx(color1ex,color2ex: integer) : array of integer;
By: Pwnaz0r
Description: Used In CompareColorsRGB;
*******************************************************************************}
function CompareColorsRGBEx(color1ex,color2ex: integer) : array of integer;
var
functionColors : array [0..2] of integer;
tempr,tempg,tempb,R1,R2,G1,G2,B1,B2,Col1,Col2 : integer;
begin
ColorToRGB(color1ex,R1,G1,B1);
ColorToRGB(color2ex,R2,G2,B2);

Col1 := color1ex;
Col2 := color2ex;

//If SameValue(R1,R2)then result[0] := 0;
//If SameValue(G1,G2)then result[1] := 0;
//If SameValue(B1,B2)then result[2] := 0;

If R1 < R2 then
begin
tempr := R2;
R2 := R1;
R1 := tempr;
end;

If G1 < G2 then
begin
tempg := G2;
G2 := G1;
G1 := tempg;
end;

If B1 < B2 then
begin
tempb := B2;
B2 := B1;
B1 := tempb;
end;


functioncolors[0] := r1 - r2;
functioncolors[1] := g1 - g2;
functioncolors[2] := b1 - b2;

Setarraylength(result,3);

result[0] := functioncolors[0];
result[1] := functioncolors[1];
result[2] := functioncolors[2];

end;

{*******************************************************************************
function CompareColorsRGB(Color1, Color2: integer; ReportIt : boolean) : array of integer;
By: Pwnaz0r
Description: Gets Max R, G, and B Differences.
*******************************************************************************}
function CompareColorsRGB(Color1, Color2: integer; ReportIt : boolean) : array of integer;
var
K : integer;
begin
result := CompareColorsRGBEx(Color1,Color2);
If ReportIt Then
begin
AddToReport('(' + IntToStr(Color1) + ', ' + IntToStr(Color2) + ')');
AddToReport('---------------');
For k := 0 to 2 do
begin
case K of
0 : AddToReport('Max Red : ' + IntToStr(result[k]));
1 : AddToReport('Max Green : ' + IntToStr(result[k]));
2 : AddToReport('Max Blue : ' + IntToStr(result[k]));
end;
end;
AddToReport('---------------');
AddToReport(' ');
end;
end;

{*******************************************************************************
function CompareColorArrayRGB(Colors : array of integer; ReportIt : boolean) : array of integer;
By: Pwnaz0r
Description: Gets the Max R, G, and B differences in an array.
*******************************************************************************}
function CompareColorArrayRGB(Colors : array of integer; ReportIt : boolean) : array of integer;
var
TmpR, TmpG, TmpB, Al, K : integer;
tmpArray : array of integer;
begin
Al := (GetArrayLength(colors) - 2);
For K := 0 to Al do
begin
If not SameValue(Colors[K],Colors[K + 1])then
begin
SetArrayLength(tmpArray,0);//Clearing of Vars

tmpArray := CompareColorsRGB(Colors[K],Colors[K + 1],False);

If TmpR = 0 then
begin
TmpR := tmpArray[0];
end else
begin
tmpR := Max(tmpR,tmpArray[0]);
end;


If TmpG = 0 then
begin
TmpG := tmpArray[1];
end else
begin
tmpG := Max(tmpG,tmpArray[1]);
end;


If TmpB = 0 then
begin
TmpB := tmpArray[2];
end else
begin
tmpB := Max(tmpB,tmpArray[2]);
end;
end;
end;

SetArrayLength(result,3);

result[0] := tmpR;
result[1] := tmpG;
result[2] := tmpB;
If ReportIt Then
begin
AddToReport('---------------');
AddToReport('Max Red : ' + IntToStr(result[0]));
AddToReport('Max Green : ' + IntToStr(result[1]));
AddToReport('Max Blue : ' + IntToStr(result[2]));
AddToReport('---------------');
AddToReport(' ');
end;
end;

{*******************************************************************************
procedure AutoColorMM(var ResultColor : integer; Color : integer; Colors : array of integer);
By: Pwnaz0r
Description: Auto-Colors a MiniMap Color with Max tolerances found in 'Colors'.
*******************************************************************************}
procedure AutoColorMM(var ResultColor : integer; Color : integer; Colors : array of integer);
var
tmpTArray : array of TPoint;
tmpNum, x1x, y1y : integer;
tmpArray2, tmpArray3 : array of integer;
begin
Flag;
FindColorsSpiralTolerance(x1x , y1y, tmpTArray, Color, 563, -4, 722, 169, 40);

tmpArray2 := CompareColorArrayRGB(Colors,False);

For tmpNum := 0 to (GetArrayLength(tmpTArray) - 1) do
begin

tmpArray3 := CompareColorsRGB(Color,GetColor(tmpTArray[tmpNum].X,tmpTArray[tmpNum].Y),False);

x1x := tmpTArray[tmpNum].X;
y1y := tmpTArray[tmpNum].Y;

If tmpArray3[0] <= tmpArray2[0] then
If tmpArray3[1] <= tmpArray2[1] then
If tmpArray3[2] <= tmpArray2[2] then
begin
ResultColor := GetColor(x1x,y1y);
MMouse(x1x,y1y,0,0);
Exit;
end;
end;
Writeln('Color Not Found');
ResultColor := 0;
end;

{*******************************************************************************
function MinArray(var int : integer; thearray : array of integer) : integer;
By: Pwnaz0r
Description: Used in FindClosestRGB.
*******************************************************************************}

function MinArray(var int : integer; thearray : array of integer) : integer;
var
K, I, J, L, tmpInteger : integer;
begin

J := GetArrayLength(thearray) - 1;
k := 1;

For I := 0 to J do
begin
tmpInteger := Min(thearray[J], thearray[j + 1]);
K := K + 1;
If K = GetArrayLength(thearray) then
begin
result := tmpInteger;
J := GetArrayLength(thearray) - 1;
For L := 0 to J do
begin
If thearray[L] = tmpInteger then
begin
int := L;
end;
end;
Exit;
end;
end;
end;

{*******************************************************************************
function FindClosestRGB(R, G, B, x1, y1, x2, y2 : Integer) : Integer;
By: Pwnaz0r
Description: Finds the Closest Color with Tolerance.
*******************************************************************************}

function FindClosestRGB(R, G, B, x1, y1, x2, y2 : Integer) : Integer;
var
I, H, K, tmpInt, ClosestInt : Integer;
tmpTArray : Array of TPoint;
tmpIntArray, rsltArray : Array of Integer;
tmpRGBArray : array of TRGB;
begin
Flag;

For I := 0 to 80 do
begin
FindColorsSpiralTolerance(x, y, tmpTArray, (RGBToColor(R,G,B)), x1, y1, x2, y2, I);
If GetArrayLength(tmpTArray) = 0 then
begin
end else
begin

SetArrayLength(tmpIntArray,0);
For H := 0 to GetArrayLength(tmpTArray) - 1 do
begin
SetArrayLength(tmpIntArray,GetArrayLength(tmpIntArray) + 1);
tmpIntArray[H] := GetColor(tmpTArray[H].X, tmpTArray[H].Y);
end;

SetArrayLength(rsltArray,0);

for K := 0 to GetArrayLength(tmpIntArray) - 1 do
begin
SetArrayLength(rsltArray,GetArrayLength(rsltArray) + 1);
tmpRGBArray[k] := CreateRGB(tmpIntArray[K]);
tmpRGBArray[K].X := tmpTArray[K].X;
tmpRGBArray[K].Y := tmpTArray[K].Y;
rsltArray[K] := (tmpRGBArray[K].red + tmpRGBArray[K].green + tmpRGBArray[K].blue);
end;

ClosestInt := MinArray(tmpInt, rsltArray);

result := GetColor(tmpRGBArray[tmpInt].X,tmpRGBArray[tmpInt].Y);
end;
end;
end;

{*******************************************************************************
function ConvertRGB( Name : string; Color : integer ) : Integer;
By: Pwnaz0r
Description: Converts a single component of RGB. Used in FindRGBTolerance
*******************************************************************************}

function ConvertRGB( Name : string; Color : integer ) : Integer;
begin
Case Lowercase(Name) Of
'red' : Result := Color mod 256;
'green' : Result := (Color / 256) mod 256;
'blue' : Result := (Color / 256) / 256;
end;
end;


{*******************************************************************************
function FindRGBTolerance(var x,y, RsltColor : integer; Red, Green, Blue, RTol, GTol, BTol, x1, y1, x2, y2 : integer) : boolean;
By: Pwnaz0r
Credits : r0b0t1 - Helped with techniques and faster finding -
Description: Finds the Closest Color with Tolerance.
*******************************************************************************}

function FindRGBTolerance(var x,y, RsltColor : integer; Red, Green, Blue, RTol, GTol, BTol, x1, y1, x2, y2 : integer) : boolean;
var
I, tmpNum, K : integer;
begin

For K := Y1 to Y2 do
begin
For I := X1 to X2 do
begin
tmpNum := ConvertRGB('red',GetColor(I,K));
If (Max(tmpNum,Red) - Min(tmpNum,Red)) <= Rtol then
begin
tmpNum := ConvertRGB('green',GetColor(I,K));
If (Max(tmpNum,Green) - Min(tmpNum,Green)) <= Gtol then
begin
tmpNum := ConvertRGB('blue',GetColor(I,K));
If (Max(tmpNum,Blue) - Min(tmpNum,Blue)) <= Btol then
begin
Result := true;
RsltColor := GetColor(I,K);
x := I;
y := K;
Exit;
End;
end;
end;
end;
end;
Result := False;
end;

R0b0t1

2007-08-21 18:30:00

Nice, finally put it in here…


But I don't think you can find the center of things with RGB.

Bobarkinator

2007-08-21 20:41:35

If you can do it with HSL I'm pretty sure you can do it with any color space but maybe not.