SRL Archive - Development

ColorToleranceSpeed 3


mastaraymond

2009-11-13 17:50:12

Title says it all:

We haven't been able to crack down Freddies algorithm for CTS 3. Any suggestions for a possible replacement of this algorithm? Or a decrypt version of his algorithm?

I don't see how keeping a color tolerance algorithm private helps the community/program in any way.

Wizzup?

2009-11-13 17:52:17

I feel more for a different algorithm. His may seem very "accurate", but it allows very little editing. You can't even change X/Y/Z mod's. Just one "tolerance".

mastaraymond

2009-11-13 17:54:25

From: Wizzup?
I feel more for a different algorithm. His may seem very "accurate", but it allows very little editing. You can't even change X/Y/Z mod's. Just one "tolerance".

Might not be very customisable, but it IS effective + 'easy to use'. Plus it's good for scar compatibility.

nielsie95

2009-11-14 02:58:44

The modifier cts 3 uses is just a modifier of the tolerance. So modifier 3 is 3 * tolerance with modifier 1.

Didn't someone have the Assembly source?

Wizzup?

2009-11-14 06:39:44

From: nielsie95
The modifier cts 3 uses is just a modifier of the tolerance. So modifier 3 is 3 * tolerance with modifier 1.

Didn't someone have the Assembly source?


We've already spent some time reverse engineering it… I don't feel like spending more time on it until the more import things are done… :p
Unless CTS 3 (aka XYZ) helps with rs UpText?

mastaraymond

2009-11-14 10:55:01

From: Wizzup?
We've already spent some time reverse engineering it… I don't feel like spending more time on it until the more import things are done… :p
Unless CTS 3 (aka XYZ) helps with rs UpText?

Who says you have to do this :D. The whole point of this thread was to get other people to reverse the code :o.

The Claw

2009-11-15 04:43:05

And how does one suppose one does that

Wizzup?

2009-11-15 05:19:30

From: The Claw
And how does one suppose one does that


By reading assembly. :)

Nava2

2009-11-15 15:43:52

Has any one tried to find a relationship from using:
program New;
var
tol, tmod: Integer;
begin
ColorToleranceSpeed(3);
for tmod := 0 to 200 do
begin
SetColorspeed3Modifiers(tMod);
Tol := 0;
while not SimilarColors(clBlack, clWhite, tol) do
inc(tol);
GetColorspeed3Modifiers(tMod);
Writeln(IntToStr(tmod) + ' ' + IntToStr(tol));
end;
end.

Also, what do we know for sure about CTS3 from fredders… do we know that is is a comparison of XYZ?

mastaraymond

2009-11-15 15:55:14

This is an 'old' decryption of the algo:
Don't mind the var names, their a bit foolish chosen.
ExtA1 = X1; ExtB1 = Y1; etc.

RGBToXYZ(R1,g1,b1,ExtA1,ExtB1,Extc1);
RGBToXYZ(R2,g2,b2,ExtA2,ExtB2,ExtC2);
Sum := ExtA1 + ExtB1 + ExtC1;
if Sum = 0 then
Sum := 1;
Dnx := (ExtA1 / sum) * xyzMod;
Dny := (ExtB1 / sum) * xyzMod;
Sum := ExtA1 + ExtB1 + ExtC1;
if Sum = 0 then
Sum := 1;
Dnx := Dnx - ((ExtA2 / Sum) * xyzMod);
Dny := Dny - ((ExtB2 / Sum) * xyzMod);
result := (sqrt(dnX*dnX + dnY * dnY) <= Tol);
end;

Nava2

2010-04-17 12:25:32

Hump a bump.

So, just got to thinking:

What if:
Assume, col1, and col2's xyz represented a vector in R3.

If you took the projection of col1 onto col2 and if the new vector had a norm <= tol wouldn't that represent a `close' colour, geometrically?

(Not cts3 necessarily..)

E2: The mathematical formula is better displayed here:

Nava2

2010-04-17 13:39:27

So, I put some of my pseudo-code into actual code:
program new;

function cts3comp(r1, g1, b1, r2, g2, b2: Integer): extended;
var
XYZ1, XYZ2: array [0..2] of extended;
i: Integer;
dot: Extended;
norm: Extended;
dist: Extended;
begin
RGBToXYZ(r1, g1, b1, XYZ1[0], XYZ1[1], XYZ1[2]);
RGBToXYZ(r2, g2, b2, XYZ2[0], XYZ2[1], XYZ2[2]);

{
Works with the projection formula of vectors onto subspaces in R3.
}
// compute c1 . c2
for i := 0 to 2 do
dot := dot + (XYZ1[i] * XYZ2[i]);

// compute the norm of c2.
for i := 0 to 2 do
norm := norm + sqr(XYZ1[i]);

norm := sqrt(norm);
if (norm = 0) then
norm := 1;

// ||proj|| = (c1 . c2) / ||c2||
dist := dot / norm;

Result := dist;
end;


begin
Writeln(cts3comp(209,210,212, 207,208,210));
Writeln( cts3comp(209,210,212, 209,210,212));
end.

The issue is that it returns:
1.1162714789800413E+0002
1.1404861804534232E+0002

That is essentially the tolerance between them. That being said, a logarithmic scale could almost be used here.

For best results, perhaps divide by a number to make the numbers slightly more reasonable.

E:
Simba is currently running this:
for c1 := clBlack to clWhite do
begin
ColorToRGB(c1, r1, g1, b1);
for c2 := clBlack to clWhite do
begin
colorToRGB(c2, r2, g2, b2);
v := cts3comp(r1, g1, b1, r2, g2, b2);
max := MaxE(max, v);
min := MinE(min, v);
end;
end;

I will post the results when they return.

E3: So, the time for that would have been… well rediculous.

I went and started fidgetting:
program new;

function dotprod(x, y: array of Extended): extended;
var
i, l: Integer;
begin
l := high(x);
for i := 0 to l do
result := result + (x[i]*y[i]);
end;

function normproj(x, onto: array of Extended): extended;
var
i, l: Integer;
dot: Extended;
norm: Extended;
begin
{
Works with the projection formula of vectors onto subspaces in Rn.
}
l := high(x);
// compute c1 . c2
dot := dotprod(x, onto);

// compute the norm of c2.
for i := 0 to l do
norm := norm + sqr(onto[i]);

if (norm = 0) then
norm := 1
else
norm := sqrt(norm);

// ||proj|| = (c1 . c2) / ||c2||
Result := dot / norm;
end;

function cts3comp(r1, g1, b1, r2, g2, b2: Integer): extended;
var
XYZ1, XYZ2: array [0..2] of extended;
rgbnorm: extended;
numerator, denominator: extended;
begin
RGBToXYZ(r1, g1, b1, XYZ1[0], XYZ1[1], XYZ1[2]);
RGBToXYZ(r2, g2, b2, XYZ2[0], XYZ2[1], XYZ2[2]);

//rgbnorm := normproj([r1, g1, b1], [r2, g2, b2]);
//writeln(Format('rgbnorm: %e', [sqrt(dotprod([r1, g1, b1], [r2, g2, b2]) / sqrt(dotprod([r2, g2, b2], [r2, g2, b2]) * dotprod([r1, g1, b1], [r2, g2, b2])))]));

numerator := dotprod(XYZ1, XYZ2);

denominator := sqrt(dotprod(XYZ1, XYZ1) * dotprod(XYZ2, XYZ2));
if (denominator = 0) then
Result := numerator
else
Result := numerator / denominator;

Result := sqrt(result);
//writeln(Format('xyznorm: %e', [Result]));

//Result := normproj(XYZ1, XYZ2);
end;


var
c1, c2: integer;
r1, r2, g1, g2, b1, b2: byte;
i, j: Integer;
max, min, v: extended;
x, y, z: extended;
begin
{ for c1 := clBlack to clWhite do
begin
ColorToRGB(c1, r1, g1, b1);
for c2 := clBlack to clWhite do
begin
colorToRGB(c2, r2, g2, b2);
v := cts3comp(r1, g1, b1, r2, g2, b2);
max := MaxE(max, v);
min := MinE(min, v);
end;
end; }

{ Writeln('Max:');
Writeln(max);
Writeln('Min:');
Writeln(min); }

rgbtoxyz(0, 1, 0, x, y, z);
writeln([x, y, z]);


Writeln(format('similar: %e', [cts3comp(209,210,212, 207,208,210)]));

Writeln(format('same: %e', [cts3comp(209,210,212, 209,210,212)]));
Writeln(format('very different: %e', [cts3comp(1,0,0, 255,255,255)]));
Writeln(format('very different: %e', [cts3comp(255,255,255, 0,0,1)]));
end.

ignore
program new;

function dotprod(x, y: array of Extended): extended;
var
i, l: Integer;
begin
l := high(x);
for i := 0 to l do
result := result + (x[i]*y[i]);
end;

function normproj(x, onto: array of Extended): extended;
var
i, l: Integer;
dot: Extended;
norm: Extended;
begin
{
Works with the projection formula of vectors onto subspaces in Rn.
}
l := high(x);
// compute c1 . c2
dot := dotprod(x, onto);

// compute the norm of c2.
for i := 0 to l do
norm := norm + sqr(onto[i]);

if (norm = 0) then
norm := 1
else
norm := sqrt(norm);

// ||proj|| = (c1 . c2) / ||c2||
Result := dot / norm;
end;

function cts3comp(r1, g1, b1, r2, g2, b2: Integer): extended;
var
XYZ1, XYZ2: array [0..2] of extended;
rgbnorm: extended;
numerator, denominator: extended;
begin
RGBToXYZ(r1, g1, b1, XYZ1[0], XYZ1[1], XYZ1[2]);
RGBToXYZ(r2, g2, b2, XYZ2[0], XYZ2[1], XYZ2[2]);

//rgbnorm := normproj([r1, g1, b1], [r2, g2, b2]);
//writeln(Format('rgbnorm: %e', [sqrt(dotprod([r1, g1, b1], [r2, g2, b2]) / sqrt(dotprod([r2, g2, b2], [r2, g2, b2]) * dotprod([r1, g1, b1], [r2, g2, b2])))]));

numerator := dotprod(XYZ1, XYZ2);

denominator := sqrt(dotprod(XYZ1, XYZ1) * dotprod(XYZ2, XYZ2));
if (denominator = 0) then
Result := numerator
else
Result := numerator / denominator;

Result := sqrt(result);

Writeln(Format('log 2: %e', [ln(result)]));
//writeln(Format('xyznorm: %e', [Result]));

//Result := normproj(XYZ1, XYZ2);
end;


var
c1, c2: integer;
r1, r2, g1, g2, b1, b2: byte;
i, j: Integer;
max, min, v: extended;
x, y, z: extended;
begin
{ for c1 := clBlack to clWhite do
begin
ColorToRGB(c1, r1, g1, b1);
for c2 := clBlack to clWhite do
begin
colorToRGB(c2, r2, g2, b2);
v := cts3comp(r1, g1, b1, r2, g2, b2);
max := MaxE(max, v);
min := MinE(min, v);
end;
end; }

{ Writeln('Max:');
Writeln(max);
Writeln('Min:');
Writeln(min); }

rgbtoxyz(0, 1, 0, x, y, z);
writeln([x, y, z]);


Writeln(format('similar: %e', [cts3comp(209,210,212, 207,208,210)]));

Writeln(format('same: %e', [cts3comp(209,210,212, 209,210,212)]));
Writeln(format('very different: %e', [cts3comp(1,0,0, 255,255,255)]));
Writeln(format('very different: %e', [cts3comp(255,255,255, 0,0,1)]));
end.

n3ss3s

2010-04-18 06:12:40

Anyone wanna point out some advantages of XYZ over RGB or HSL, considering we are making scripts for a game which's trees consist of like 3x3 pixel single color "leaves" ?

itSchRis917

2010-04-18 23:46:39

I don't know if anybody has checked out SCAR's documentation and found this:
ColorToleranceSpeed(3) uses the CIE colorspace to compare colors (XYZ), it's usually fairly slower than the other modes but it's extremely accurate.
But i quickly googled CIE colorspace and found this Wikipedia link: http://en.wikipedia.org/wiki/CIE_1931_color_space. Sorry if you already know about it, just thought i'd pitch in a little idea?

n3ss3s

2010-04-19 14:17:33

From: itSchRis917
I don't know if anybody has checked out SCAR's documentation and found this:
But i quickly googled CIE colorspace and found this Wikipedia link: http://en.wikipedia.org/wiki/CIE_1931_color_space. Sorry if you already know about it, just thought i'd pitch in a little idea?


What is the idea?

itSchRis917

2010-04-19 14:34:11

I don't have any real code or idea, i just thought i would offer up some research. Sorry

Daniel

2010-04-20 03:14:40

From: itSchRis917
I don't have any real code or idea, i just thought i would offer up some research. Sorry

Don't be sorry, you were only trying to help.

n3ss3s

2010-04-20 08:26:15

yea, well CTS 3 has been there for more than a year and ppl who are trying to crack it most likely do know its worth it :d

Frement

2010-04-20 08:36:46

Why cant freddy just tip us something about it that lets us then crack it?

Wizzup?

2010-04-20 09:27:40

From: Frement
Why cant freddy just tip us something about it that lets us then crack it?


For the same reason he doesn't give source for SCAR?

Nava2

2010-04-20 09:38:20

His only tip is that it uses both XYZ and HSL colour space.

E: Oh and all my spammed code, it works for RGB values, the concept is great for that, but for XYZ and HSL its not relevant because they do not behave like typical vectors.

They are created from formulas, thus my approach won't work.

Might make a better CTS1 though :)

Wizzup?

2010-04-20 09:46:30

From: Nava2
His only tip is that it uses both XYZ and HSL colour space.

E: Oh and all my spammed code, it works for RGB values, the concept is great for that, but for XYZ and HSL its not relevant because they do not behave like typical vectors.

They are created from formulas, thus my approach won't work.

Might make a better CTS1 though :)


You'd be better off creating your own algorithm. It's a waste to spent more time reverse engineering his algorithm. Hardly anyone uses it. Better create a new, better one. Perhaps, when he open sources it, we will use it too.

itSchRis917

2010-04-20 11:02:12

From: Nava2
His only tip is that it uses both XYZ and HSL colour space.

E: Oh and all my spammed code, it works for RGB values, the concept is great for that, but for XYZ and HSL its not relevant because they do not behave like typical vectors.

They are created from formulas, thus my approach won't work.

Might make a better CTS1 though :)


The Wiki link i posted has the formulas for XYZ, however they are based on the lengths of the light waves and i don't have a clue how that would work on a computer screen

Nava2

2010-04-20 11:06:53

From: itSchRis917
The Wiki link i posted has the formulas for XYZ, however they are based on the lengths of the light waves and i don't have a clue how that would work on a computer screen


Yup, I have all the formulas.

I just need to research the understanding behind what each of the values are representative of. Its there, but I need to do some more formal research on the subject.

If I can find a correlation between what the eye can see vs. the numerical representations of each, I can create a system to compare colours which are slightly modified.

Wizzup?

2010-04-20 11:06:57

From: itSchRis917
The Wiki link i posted has the formulas for XYZ, however they are based on the lengths of the light waves and i don't have a clue how that would work on a computer screen


We already have formula's for RGB,HSL and XYZ conversions. :)
We just need a good algorithm now. (It's probably fun to make)

itSchRis917

2010-04-20 16:05:04

I'd really like to take a look at the source for SIMBA, but I'm kind of confused as to how.. Lol i have Lazarus as well as the SIMBA source through GIT. Could anyone help me as to what project file I open/point my to a thread please? :) Thanks!

Nava2

2010-04-20 20:21:47

MML Core is probably what you want. The Simba project is in the SAMufasaGUI project

Daniel

2010-04-25 01:36:59

From: Wizzup?
You'd be better off creating your own algorithm. It's a waste to spent more time reverse engineering his algorithm. Hardly anyone uses it. Better create a new, better one. Perhaps, when he open sources it, we will use it too.


You speak as if he is going to open-source SCAR :p

Dan Cardin

2010-04-25 14:48:54

Wizzup thinks that in desperation, Fredders will release the source for one reason or the other.

Wizzup?

2010-04-25 15:41:29

[QUOTE=Dan's The Man;705126]You speak as if he is going to open-source SCAR :p[/QUOTE]

Nah, I was being purely hypothetical.

@ ManFlardin
I don't think a lot of people care much for the SCAR source anymore, unless they'd actually want to compare it to the Simba code. There isn't much SCAR can do that isn't in Simba's source.

Dan Cardin

2010-04-25 16:39:08

except for this ^? I'm sure it would be useful were it to happen, not that i particularly want it to.

Wizzup?

2010-04-25 17:02:00

From: Dan Cardin
except for this ^? I'm sure it would be useful were it to happen, not that i particularly want it to.


Hmm… I guess that most scripts do not use CTS 3 though. And Simba will probably and it's own algorithm in a bit. But yeah, it would be useful if you wanted Simba to use the exact algo.

i luffs yeww

2010-04-25 17:13:33

^ s/and/add/?

Wizzup?

2010-04-25 18:11:30

From: i luffs yeww
^ s/and/add/?


have :)