Tolerance

From SCAR Divi Manual
Revision as of 11:48, 28 June 2011 by Freddy (talk | contribs) (Created page with "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 color...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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
  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;
  if Tolerance <> 0 then
  begin
    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 else
    Result := c1 = c2;
end;

Source Code (Calculate Tolerance)

function CalcTolerance(c1, c2: Integer): Integer;
var
  r1, g1, b1: Byte;
  r2, g2, b2: Byte;
begin
  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;
  if c1 = c2 then Exit(0);
  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
  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;
  if Tolerance <> 0 then
    Result := Sqrt(Sqr(r2 - r1) + Sqr(g2 - g1) + Sqr(b2 - b1)) <= Tolerance
  else
    Result := c1 = c2;
end;

Source Code (Calculate Tolerance)

function CalcTolerance(c1, c2: Integer): Integer;
var
  r1, g1, b1: Byte;
  r2, g2, b2: Byte;
begin
  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;
  if c1 = c2 then Exit(0);
  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 area 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.