Properly add all view JComponents to the JPanel in the main JFrame.
[Persons_Comparator.git] / src / Eye.java
1 import java.awt.Color;
2 import java.util.List;
3 import java.util.Arrays;
4
5 public class Eye implements Comparable<Eye> {
6 private Color color;
7 private List<String> colorList = Arrays.asList("black", "green", "blue", "brown");
8
9 /**
10 * @param color
11 */
12 Eye(String color) {
13 setColor(color);
14 }
15
16 /**
17 * @return
18 */
19 public Color getColor() {
20 return color;
21 }
22
23 /**
24 * @param color
25 */
26 public void setColor(String color) {
27 if (validateColor(color)) {
28 if (color.equals("brown"))
29 this.color = new Color(88, 41, 0);
30 else
31 this.color = Color.getColor(color);
32
33 } else {
34 throw new IllegalArgumentException("Color must be " + colorList.toString());
35 }
36 }
37
38 /**
39 * @param color
40 * @return
41 */
42 private boolean validateColor(String color) {
43 return colorList.contains(color);
44 }
45
46 /**
47 * @param eye
48 * @return
49 */
50 @Override
51 public int compareTo(Eye eye) {
52 double r_diff = this.getColor().getRed() - eye.getColor().getRed();
53 double g_diff = this.getColor().getGreen() - eye.getColor().getGreen();
54 double b_diff = this.getColor().getBlue() - eye.getColor().getBlue();
55 // See https://en.wikipedia.org/wiki/Color_difference
56 Double distance = Math.sqrt(2 * Math.pow(r_diff, 2) + 4 * Math.pow(g_diff, 2) + 3 * Math.pow(b_diff, 2));
57 return distance.intValue();
58 }
59 }