Implement eye color compareTo() method.
[Persons_Comparator.git] / src / Eye.java
CommitLineData
97491195 1import java.awt.Color;
1c118933 2import java.util.List;
6b48f570 3import java.util.Arrays;
97491195 4
1c118933 5public 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
JB
18 if (validateColor(color)) {
19 if (color != "brown")
20 this.color = Color.getColor(color);
21 else
22 this.color = new Color(88, 41, 0);
23 } else {
24 throw new IllegalArgumentException("Color must be " + colorList.toString());
25 }
26 }
27
28 private boolean validateColor(String color) {
29 return colorList.contains(color);
1c118933
JB
30 }
31
32 @Override
33 public int compareTo(Eye eye) {
6b48f570
JB
34 double r_diff = this.getColor().getRed() - eye.getColor().getRed();
35 double g_diff = this.getColor().getGreen() - eye.getColor().getGreen();
36 double b_diff = this.getColor().getBlue() - eye.getColor().getBlue();
37 // See https://en.wikipedia.org/wiki/Color_difference
38 Double distance = Math.sqrt(2 * Math.pow(r_diff, 2) + 4 * Math.pow(g_diff, 2) + 3 * Math.pow(b_diff, 2));
39 return distance.intValue();
97491195
JB
40 }
41}