Commit | Line | Data |
---|---|---|
97491195 | 1 | import java.awt.Color; |
d75d0a00 | 2 | import java.util.Arrays; |
97491195 | 3 | |
1c118933 | 4 | public class Eye implements Comparable<Eye> { |
d75d0a00 | 5 | private String strColor; |
97491195 | 6 | private Color color; |
d75d0a00 | 7 | private String[] colorsList = {"black", "blue", "brown", "green"}; |
883508ca JB |
8 | |
9 | Eye() { | |
d75d0a00 | 10 | sortColorList(); |
883508ca | 11 | } |
97491195 | 12 | |
ac6c3ea2 | 13 | /** |
ac6c3ea2 JB |
14 | * @param color |
15 | */ | |
1c118933 | 16 | Eye(String color) { |
d75d0a00 | 17 | setStrColor(color); |
97491195 | 18 | setColor(color); |
d75d0a00 JB |
19 | sortColorList(); |
20 | } | |
21 | ||
22 | public String getStrColor() { | |
23 | return strColor; | |
24 | } | |
25 | ||
26 | public void setStrColor(String strColor) { | |
27 | if (validateColor(strColor)) { | |
28 | this.strColor = strColor; | |
29 | } else { | |
480ff9f5 | 30 | throw new IllegalArgumentException("Color must be" + this.colorListToString()); |
d75d0a00 | 31 | } |
97491195 JB |
32 | } |
33 | ||
883508ca JB |
34 | public String[] getColorsList() { |
35 | return colorsList; | |
36 | } | |
37 | ||
ac6c3ea2 | 38 | /** |
ac6c3ea2 JB |
39 | * @return |
40 | */ | |
97491195 JB |
41 | public Color getColor() { |
42 | return color; | |
43 | } | |
44 | ||
ac6c3ea2 | 45 | /** |
ac6c3ea2 JB |
46 | * @param color |
47 | */ | |
1c118933 | 48 | public void setColor(String color) { |
6b48f570 | 49 | if (validateColor(color)) { |
6977e614 | 50 | if (color.equals("brown")) |
6b48f570 | 51 | this.color = new Color(88, 41, 0); |
6977e614 JB |
52 | else |
53 | this.color = Color.getColor(color); | |
6b48f570 | 54 | } else { |
480ff9f5 | 55 | throw new IllegalArgumentException("Color must be" + this.colorListToString()); |
6b48f570 JB |
56 | } |
57 | } | |
58 | ||
ac6c3ea2 | 59 | /** |
ac6c3ea2 JB |
60 | * @param color |
61 | * @return | |
62 | */ | |
6b48f570 | 63 | private boolean validateColor(String color) { |
883508ca JB |
64 | for (String c : colorsList) { |
65 | if (color.equals(c)) { | |
66 | return true; | |
67 | } | |
68 | } | |
69 | return false; | |
1c118933 JB |
70 | } |
71 | ||
d75d0a00 JB |
72 | private void sortColorList() { |
73 | Arrays.sort(this.colorsList); | |
74 | } | |
75 | ||
ac6c3ea2 | 76 | /** |
ac6c3ea2 JB |
77 | * @param eye |
78 | * @return | |
79 | */ | |
1c118933 JB |
80 | @Override |
81 | public int compareTo(Eye eye) { | |
6b48f570 JB |
82 | double r_diff = this.getColor().getRed() - eye.getColor().getRed(); |
83 | double g_diff = this.getColor().getGreen() - eye.getColor().getGreen(); | |
84 | double b_diff = this.getColor().getBlue() - eye.getColor().getBlue(); | |
f1e9d6d2 | 85 | //See https://en.wikipedia.org/wiki/Color_difference |
6b48f570 JB |
86 | Double distance = Math.sqrt(2 * Math.pow(r_diff, 2) + 4 * Math.pow(g_diff, 2) + 3 * Math.pow(b_diff, 2)); |
87 | return distance.intValue(); | |
97491195 | 88 | } |
b974e749 | 89 | |
d75d0a00 | 90 | private String colorListToString() { |
b974e749 JB |
91 | StringBuilder stringBuilder = new StringBuilder(); |
92 | for (String c : colorsList) { | |
93 | stringBuilder.append(" "); | |
94 | stringBuilder.append(c); | |
95 | } | |
96 | return stringBuilder.toString(); | |
97 | } | |
97491195 | 98 | } |