Do not create a new instance recursively for nothing to display the list of valid...
[Persons_Comparator.git] / src / Eye.java
index abbbfb70fabbc21f35cd3aca57a2d5f7397326f8..59612440812c342dd6dd929b91936de79363d337 100644 (file)
@@ -1,24 +1,98 @@
 import java.awt.Color;
-import java.util.List;
+import java.util.Arrays;
 
 public class Eye implements Comparable<Eye> {
+    private String strColor;
     private Color color;
-    private List<Color> colorList;
+    private String[] colorsList = {"black", "blue", "brown", "green"};
 
+    Eye() {
+        sortColorList();
+    }
+
+    /**
+     * @param color
+     */
     Eye(String color) {
+        setStrColor(color);
         setColor(color);
+        sortColorList();
+    }
+
+    public String getStrColor() {
+        return strColor;
+    }
+
+    public void setStrColor(String strColor) {
+        if (validateColor(strColor)) {
+            this.strColor = strColor;
+        } else {
+            throw new IllegalArgumentException("Color must be" + this.colorListToString());
+        }
+    }
+
+    public String[] getColorsList() {
+        return colorsList;
     }
 
+    /**
+     * @return
+     */
     public Color getColor() {
         return color;
     }
 
+    /**
+     * @param color
+     */
     public void setColor(String color) {
-        this.color = Color.getColor(color);
+        if (validateColor(color)) {
+            if (color.equals("brown"))
+                this.color = new Color(88, 41, 0);
+            else
+                this.color = Color.getColor(color);
+        } else {
+            throw new IllegalArgumentException("Color must be" + this.colorListToString());
+        }
+    }
+
+    /**
+     * @param color
+     * @return
+     */
+    private boolean validateColor(String color) {
+        for (String c : colorsList) {
+            if (color.equals(c)) {
+                return true;
+            }
+        }
+        return false;
     }
 
+    private void sortColorList() {
+        Arrays.sort(this.colorsList);
+    }
+
+    /**
+     * @param eye
+     * @return
+     */
     @Override
     public int compareTo(Eye eye) {
-        return 0;
+        double r_diff = this.getColor().getRed() - eye.getColor().getRed();
+        double g_diff = this.getColor().getGreen() - eye.getColor().getGreen();
+        double b_diff = this.getColor().getBlue() - eye.getColor().getBlue();
+        //See https://en.wikipedia.org/wiki/Color_difference
+        Double distance = Math.sqrt(2 * Math.pow(r_diff, 2) + 4 * Math.pow(g_diff, 2) + 3 * Math.pow(b_diff, 2));
+        return distance.intValue();
+    }
+
+    private String colorListToString() {
+        StringBuilder stringBuilder = new StringBuilder();
+        for (String c : colorsList) {
+            stringBuilder.append(" ");
+            stringBuilder.append(c);
+        }
+        return stringBuilder.toString();
     }
 }