Implement eye color compareTo() method.
[Persons_Comparator.git] / src / Eye.java
index abbbfb70fabbc21f35cd3aca57a2d5f7397326f8..595d3d3990da3e71fa5cbf403ffde791e81704f5 100644 (file)
@@ -1,9 +1,10 @@
 import java.awt.Color;
 import java.util.List;
+import java.util.Arrays;
 
 public class Eye implements Comparable<Eye> {
     private Color color;
-    private List<Color> colorList;
+    private List<String> colorList = Arrays.asList("black", "green", "blue", "brown");
 
     Eye(String color) {
         setColor(color);
@@ -14,11 +15,27 @@ public class Eye implements Comparable<Eye> {
     }
 
     public void setColor(String color) {
-        this.color = Color.getColor(color);
+        if (validateColor(color)) {
+            if (color != "brown")
+                this.color = Color.getColor(color);
+            else
+                this.color = new Color(88, 41, 0);
+        } else {
+            throw new IllegalArgumentException("Color must be " + colorList.toString());
+        }
+    }
+
+    private boolean validateColor(String color) {
+        return colorList.contains(color);
     }
 
     @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();
     }
 }