Add cut/copy/paste to the main windows menu.
[Persons_Comparator.git] / src / Eye.java
CommitLineData
97491195 1import java.awt.Color;
d75d0a00 2import java.util.Arrays;
97491195 3
1c118933 4public 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 {
30 throw new IllegalArgumentException("Color must be" + new Eye().colorListToString());
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);
54
6b48f570 55 } else {
d75d0a00 56 throw new IllegalArgumentException("Color must be" + new Eye().colorListToString());
6b48f570
JB
57 }
58 }
59
ac6c3ea2 60 /**
ac6c3ea2
JB
61 * @param color
62 * @return
63 */
6b48f570 64 private boolean validateColor(String color) {
883508ca
JB
65 for (String c : colorsList) {
66 if (color.equals(c)) {
67 return true;
68 }
69 }
70 return false;
1c118933
JB
71 }
72
d75d0a00
JB
73 private void sortColorList() {
74 Arrays.sort(this.colorsList);
75 }
76
ac6c3ea2 77 /**
ac6c3ea2
JB
78 * @param eye
79 * @return
80 */
1c118933
JB
81 @Override
82 public int compareTo(Eye eye) {
6b48f570
JB
83 double r_diff = this.getColor().getRed() - eye.getColor().getRed();
84 double g_diff = this.getColor().getGreen() - eye.getColor().getGreen();
85 double b_diff = this.getColor().getBlue() - eye.getColor().getBlue();
f1e9d6d2 86 //See https://en.wikipedia.org/wiki/Color_difference
6b48f570
JB
87 Double distance = Math.sqrt(2 * Math.pow(r_diff, 2) + 4 * Math.pow(g_diff, 2) + 3 * Math.pow(b_diff, 2));
88 return distance.intValue();
97491195 89 }
b974e749 90
d75d0a00 91 private String colorListToString() {
b974e749
JB
92 StringBuilder stringBuilder = new StringBuilder();
93 for (String c : colorsList) {
94 stringBuilder.append(" ");
95 stringBuilder.append(c);
96 }
97 return stringBuilder.toString();
98 }
97491195 99}