Commit | Line | Data |
---|---|---|
97491195 | 1 | import java.awt.Color; |
1c118933 | 2 | import java.util.List; |
6b48f570 | 3 | import java.util.Arrays; |
97491195 | 4 | |
1c118933 | 5 | public class Eye implements Comparable<Eye> { |
97491195 | 6 | private Color color; |
6b48f570 | 7 | private List<String> colorList = Arrays.asList("black", "green", "blue", "brown"); |
97491195 | 8 | |
1c118933 | 9 | Eye(String color) { |
97491195 JB |
10 | setColor(color); |
11 | } | |
12 | ||
13 | public Color getColor() { | |
14 | return color; | |
15 | } | |
16 | ||
1c118933 | 17 | public void setColor(String color) { |
6b48f570 | 18 | if (validateColor(color)) { |
6977e614 | 19 | if (color.equals("brown")) |
6b48f570 | 20 | this.color = new Color(88, 41, 0); |
6977e614 JB |
21 | else |
22 | this.color = Color.getColor(color); | |
23 | ||
6b48f570 JB |
24 | } else { |
25 | throw new IllegalArgumentException("Color must be " + colorList.toString()); | |
26 | } | |
27 | } | |
28 | ||
29 | private boolean validateColor(String color) { | |
30 | return colorList.contains(color); | |
1c118933 JB |
31 | } |
32 | ||
33 | @Override | |
34 | public int compareTo(Eye eye) { | |
6b48f570 JB |
35 | double r_diff = this.getColor().getRed() - eye.getColor().getRed(); |
36 | double g_diff = this.getColor().getGreen() - eye.getColor().getGreen(); | |
37 | double b_diff = this.getColor().getBlue() - eye.getColor().getBlue(); | |
38 | // See https://en.wikipedia.org/wiki/Color_difference | |
39 | Double distance = Math.sqrt(2 * Math.pow(r_diff, 2) + 4 * Math.pow(g_diff, 2) + 3 * Math.pow(b_diff, 2)); | |
40 | return distance.intValue(); | |
97491195 JB |
41 | } |
42 | } |