Comparing colors
One on one component - CTS 0
While comparing colors you may want a very fast algorithm. For example, when the screen you want to search on is big and the response from the script has to happen fast. CTS 0 might be the perfect algorithm to do that. It is the fastest algorithm and supports tolerance for improved results.
So what does CTS 0 do? As you probably already understand by now, every color is split up in three components. Red, Green and Blue. These components all have a range from 0 to 255. When comparing two colors it will check if the difference between them is less then the tolerance. A simple simba script doing the same:
Simba Code:
function CompareCTS0(red, green, blue, red2, green2, blue2, tol: Integer): Boolean;
begin
Result := False;
if Abs(red - red2) > tol then
Exit;
if Abs(green - green2) > tol then
Exit;
if Abs(blue - blue2) > tol then
Exit;
Result := True;
end;
COLOR1 = COLOR2?
Color 1
- Red = 255
- green = 17
- blue = 0
Color 2
- Red = 255
- green = 66
- blue = 0
Tolerance = 50
So yes, the algorithm will return true. The difference between the red and blue components is zero but the green components differ 49. Which is less then 50, our given tolerance.
COLOR1 = COLOR2?
Color 1
- Red = 204
- green = 17
- blue = 10
Color 2
- Red = 254
- green = 66
- blue = 60
Tolerance = 50
As you can see, even though the difference for our eyes are much bigger, the algorithm will say the biggest difference between components is 50. Which is almost the same as with the previous colors. The algorithm will return true.
COLOR1 = COLOR2?
Color 1
- Red = 204
- green = 0
- blue = 0
Color 2
- Red = 255
- green = 0
- blue = 0
Tolerance = 50
When you look at the color you might think these colors are closer to each other then the previous colors. But our algorithm disagrees. The biggest difference between the components is 51, which is more then our tolerance. This would return false.
So a short summary of this algorithm in pros and cons:
Pros
- Easiest to understand
- Very fast to calculate
cons
- Doesn't really equals our visual experience
- More prone to color changes
- No component individual mods
Pythagoras - CTS 1
This algorithm is a big improvement looking at CTS 0 in terms of how the algorithm its results compare to our visual experiences. However it is a slower method then CTS 0. Note that this is the default algorithm simba uses.
The greek Pythagoras
We all know square A plus square B equals square C from pythagoras. So how does this works with comparing colors you might ask? In the chapter about RGB I showed you an RGB cube. It is a cube where the three axes are representing the Red, Green and Blue values. Now imagine the two colors you want to compare are points in that cube. With pythagoras we will calculate the distance between the points. This will result into a value that needs to be less then the tolerance.
Four points represented in an 3D image
As you can see in the image, colors can easily be represented on three axes. In this example you will see four colors. With CTS 1 it will take the distance between the points in the 3D space and check if that is less then the tolerance. This is an effective way of comparing colors and will give results closer to the our visual experience. This returns a very accurate result.
Simba Code:
function CompareCTS1(red, green, blue, red2, green2, blue2, tol: Integer): Boolean;
var
difRed, difGreen, difBlue: Integer;
begin
difRed := red - red2;
difGreen := green - green2;
difBlue := blue - blue2;
Result := (Sqr(tol) > (Sqr(difRed) + Sqr(difBlue) + Sqr(difGreen)));
end;
As you can notice this functions requires four squares. But the actual speed difference between CTS 0 and CTS 1 is very small. It doesn't matter if you have to use abs or sqr. In both cases you will have a very fast algorithm. Also note how didn't used a square root. Square roots are very slow on every CPU. By squaring the tolerance I will gain a big speed improvement while the function will still function the same.
COLOR1 = COLOR2?
Color 1
- Red = 204
- green = 0
- blue = 0
Color 2
- Red = 255
- green = 0
- blue = 0
Tolerance = 30
Result = 30² > (204 - 255)² + (0 - 0)² + (0 - 0)²
Result = 30² > 41²
Result = false
As you can see the result will be false. The squared tolerance will only be 900 and the squared difference between the red components will be 1681. As you will notice, the tolerance has to be more then 41 to return true.
COLOR1 = COLOR2?
Color 1
- Red = 204
- green = 0
- blue = 204
Color 2
- Red = 255
- green = 0
- blue = 255
Tolerance = 45
Result = 45² > (204 - 255)² + (0 - 0)² + (204 - 255)²
Result = 45² > 41² + 41²
Result = 2025 > 1681 + 1681
Result = false
Again the result will be false. Even though the tolerance is now more then 41, the changes in both the blue and red components add up. Making a much higher tolerance needed. The required tolerance would be more then √(41² + 41²). Which is 58. This is a great advantage over CTS 0 in most of the situations.
So a short summary of this algorithm in pros and cons:
Pros
- Very fast to calculate
- Uses all three color components at once, so it compares the complete color against the tolerance.
cons
- Doesn't really equals our visual experience
- No component individual mods
Auto coloring with RGB
Sometimes the above cts methods aren't enough for what you want. As you might have noticed I already recreated the CTS 0 and CTS 1 algorithm in pascal script. The big con of using your own CTS algorithm is the big decrease of speed compared to regular methods. In most situations it is best to just use what comes shipped with simba, unless you want to add it yourself to simba's MMLCore or create a fast plugin containing your functions.
There is one exception, auto coloring. In the world of runescape colors changes quite often to make it harder to bot with screen scraper bots like simba and scar. Getting the perfect 'middle color' with a good tolerance can be very important. A common used problem solver with auto coloring are the roads on the minimap. In this piece of the tutorial I will go in depth on the auto color function used to get the color of a road color.
Though when you write a script yourself, you may want to take a bigger amount of samples as 5. This is just an example.
sample 1:
Red: 82
Green: 83
Blue: 76
Color: 5002066
sample 2:
Red: 82
Green: 79
Blue: 76
Color: 5001042
sample 3:
Red: 85
Green: 84
Blue: 76
Color: 5002325
sample 4:
Red: 79
Green: 80
Blue: 75
Color: 4935759
sample 5:
Red: 84
Green: 83
Blue: 77
Color: 5067604
As you may notice the blue component is the most stable component. However both the CTS methods using RGB don't support anything to indicate that blue is the most stable component. There are no CTS mods for CTS 0 and CTS 1. That is where we kick in with out auto color method.