A lib directory to the project with gson library.
[Persons_Comparator.git] / src / Eye.java
... / ...
CommitLineData
1import java.awt.Color;
2
3public class Eye implements Comparable<Eye> {
4 private Color color;
5 private String[] colorsList = {"black", "green", "blue", "brown"};
6
7 Eye() {
8 }
9
10 /**
11 * @param color
12 */
13 Eye(String color) {
14 setColor(color);
15 }
16
17 public String[] getColorsList() {
18 return colorsList;
19 }
20
21 /**
22 * @return
23 */
24 public Color getColor() {
25 return color;
26 }
27
28 /**
29 * @param color
30 */
31 public void setColor(String color) {
32 if (validateColor(color)) {
33 if (color.equals("brown"))
34 this.color = new Color(88, 41, 0);
35 else
36 this.color = Color.getColor(color);
37
38 } else {
39 throw new IllegalArgumentException("Color must be " + colorsList);
40 }
41 }
42
43 /**
44 * @param color
45 * @return
46 */
47 private boolean validateColor(String color) {
48 for (String c : colorsList) {
49 if (color.equals(c)) {
50 return true;
51 }
52 }
53 return false;
54 }
55
56 /**
57 * @param eye
58 * @return
59 */
60 @Override
61 public int compareTo(Eye eye) {
62 double r_diff = this.getColor().getRed() - eye.getColor().getRed();
63 double g_diff = this.getColor().getGreen() - eye.getColor().getGreen();
64 double b_diff = this.getColor().getBlue() - eye.getColor().getBlue();
65 //See https://en.wikipedia.org/wiki/Color_difference
66 Double distance = Math.sqrt(2 * Math.pow(r_diff, 2) + 4 * Math.pow(g_diff, 2) + 3 * Math.pow(b_diff, 2));
67 return distance.intValue();
68 }
69}