Difference between revisions of "Tolerance"

From SCAR Divi Manual
Jump to: navigation, search
(Source Code (Check Tolerance))
(Source Code (Check Tolerance))
 
Line 14: Line 14:
 
   r2, g2, b2: Byte;
 
   r2, g2, b2: Byte;
 
begin
 
begin
   if Tolerance <> 0 then
+
   if Tolerance == 0 then Exit(c1 = c2);
   begin
+
   r1 := c1 and $FF; g1 := (c1 shr 8) and $FF; b1 := (c1 shr 16) and $FF;
    r1 := c1 and $FF; g1 := (c1 shr 8) and $FF; b1 := (c1 shr 16) and $FF;
+
  r2 := c2 and $FF; g2 := (c2 shr 8) and $FF; b2 := (c2 shr 16) and $FF;
    r2 := c2 and $FF; g2 := (c2 shr 8) and $FF; b2 := (c2 shr 16) and $FF;
+
  Result := False;
    Result := False;
+
  if Abs(b2 - b1) > Tolerance then Exit;
    if Abs(b2 - b1) > Tolerance then Exit;
+
  if Abs(g2 - g1) > Tolerance then Exit;
    if Abs(g2 - g1) > Tolerance then Exit;
+
  if Abs(r2 - r1) > Tolerance then Exit;
    if Abs(r2 - r1) > Tolerance then Exit;
+
  Result := True;
    Result := True;
 
  end else
 
    Result := c1 = c2;
 
 
end;
 
end;
 
</source>
 
</source>

Latest revision as of 11:00, 28 June 2011

SCAR Divi uses special algorithms to do tolerance calculations to determine how similar colors are. This allows users to search for not just a single color, but a color and colors similar to it within a certain range. The function ColorToleranceSpeed can be used to change the active algorithm which is used by all tolerance functions.

Algorithms

There are currently 4 algorithms being used in SCAR to determine color tolerances, each has it's advantages and disadvantages. Algorithms that provide a more accurate comparison are often slower in execution, but it's usually sufficient to use the default algorithm.

Algorithm 0

The first algorithm uses very simple calculations to determine whether a value is similar. It compares each RGB value of a color individually by checking if the absolute difference of the 2 values is always smaller then or equal to the given tolerance. This algorithm is very fast.

Source Code (Check Tolerance)

function CheckTolerance(c1, c2, Tolerance: Integer): Boolean;
var
  r1, g1, b1: Byte;
  r2, g2, b2: Byte;
begin
  if Tolerance == 0 then Exit(c1 = c2);
  r1 := c1 and $FF; g1 := (c1 shr 8) and $FF; b1 := (c1 shr 16) and $FF;
  r2 := c2 and $FF; g2 := (c2 shr 8) and $FF; b2 := (c2 shr 16) and $FF;
  Result := False;
  if Abs(b2 - b1) > Tolerance then Exit;
  if Abs(g2 - g1) > Tolerance then Exit;
  if Abs(r2 - r1) > Tolerance then Exit;
  Result := True;
end;

Source Code (Calculate Tolerance)

function CalcTolerance(c1, c2: Integer): Integer;
var
  r1, g1, b1: Byte;
  r2, g2, b2: Byte;
begin
  if c1 = c2 then Exit(0);
  r1 := c1 and $FF; g1 := (c1 shr 8) and $FF; b1 := (c1 shr 16) and $FF;
  r2 := c2 and $FF; g2 := (c2 shr 8) and $FF; b2 := (c2 shr 16) and $FF;
  Result := Max(Max(Abs(b2 - b1), Abs(g2 - g1)), Abs(r2 - r1));
end;

Algorithm 1 (Default)

The second tolerance algorithm is set by default, it makes use of distance calculation to determine the tolerance using RGB as a 3-dimensional space. This algorithm is very fast.

Source Code (Check Tolerance)

function CheckTolerance(c1, c2, Tolerance: Integer): Boolean;
var
  r1, g1, b1: Byte;
  r2, g2, b2: Byte;
begin
  if Tolerance == 0 then Exit(c1 = c2);
  r1 := c1 and $FF; g1 := (c1 shr 8) and $FF; b1 := (c1 shr 16) and $FF;
  r2 := c2 and $FF; g2 := (c2 shr 8) and $FF; b2 := (c2 shr 16) and $FF;
  Result := Sqrt(Sqr(r2 - r1) + Sqr(g2 - g1) + Sqr(b2 - b1)) <= Tolerance;
end;

Source Code (Calculate Tolerance)

function CalcTolerance(c1, c2: Integer): Integer;
var
  r1, g1, b1: Byte;
  r2, g2, b2: Byte;
begin
  if c1 = c2 then Exit(0);
  r1 := c1 and $FF; g1 := (c1 shr 8) and $FF; b1 := (c1 shr 16) and $FF;
  r2 := c2 and $FF; g2 := (c2 shr 8) and $FF; b2 := (c2 shr 16) and $FF;
  Result := Ceil(Sqrt(Sqr(r2 - r1) + Sqr(g2 - g1) + Sqr(b2 - b1)));
end;

Algorithm 2

The third tolerance algorithm uses the HSL color space to determine color tolerances. The algorithm is often useful when you want to compare values that are similar in brightness, tone or saturation as the algorithm provides modifiers which allow you to determine the impact the hue and saturation have on the calculations. This algorithm is fairly slow as it has to convert the RGB values to the HSL color space.

Algorithm 3

The fourth and currently last algorithm uses the XYZ color space to compare colors based on an approximation to human visual perception. This is currently the most accurate way to do color comparisons, however the algorithm is very slow because it has to convert the RGB values to the XYZ color space which requires very large calculations. This algorithm also provides a modifier that can be changed to set the sensitivity of the calculations, though the default setting is sufficient is most circumstances. It is not advised to use this algorithm in time critical scripts.