Page 219 - The Definitive Guide to Building Java Robots
P. 219
Preston_5564C06.fm Page 200 Friday, September 23, 2005 5:13 AM
200 CHAPTER 6 ■ VISION
Code Discussion
The ColorGram class itself is nothing more than a data structure. I have constructed it such
that the combination of any target ColorGram is the function of all the colors in this form:
x * red + y * green + z * blue + c, where red, green, and blue are the colors of a pixel, and x, y, z,
and c are the values of the ColorGram.
The function isMatch() just checks the current pixel color to see if it’s in the range of the
ColorGram’s minimum and maximum values. When it is, it returns true.
The very last method of the class is clone(). Here I’m creating an exact copy of the ColorGram.
Later in this section, I’ll continuously modify and optimize a ColorGram object, but right now
I just want to optimize the value of the ColorGram, so by cloning it I can get an exact copy
without making changes to its reference. See Example 6-20.
Example 6-20. ColorGram.java
package com.scottpreston.javarobot.chapter6;
import java.awt.Color;
public class ColorGram implements Cloneable{
private double[] colorGram;
public ColorGram() {
// blank
colorGram = new double[] {
0, 0, 0, 0, // min
0, 0, 0, 255, // max red color
0, 0, 0, 0, // min green color
0, 0, 0, 255, // max green color
0, 0, 0, 0, // min blue color
0, 0, 0, 255 };
}
public ColorGram(double[] cg) {
colorGram = cg;
}
public int getRedMin(Color c) {
return getColor(c, 0);
}
public int getRedMax(Color c) {
return getColor(c, 4);
}