X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FEye.java;h=a165830f8a1f3cb67354d4c3a97fe48951798c90;hb=883508cad71557d375580ce52a4e093f131405db;hp=abbbfb70fabbc21f35cd3aca57a2d5f7397326f8;hpb=1c1189337121a1baa74961813e619c99b398a333;p=Persons_Comparator.git diff --git a/src/Eye.java b/src/Eye.java index abbbfb7..a165830 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -1,24 +1,69 @@ import java.awt.Color; -import java.util.List; public class Eye implements Comparable { private Color color; - private List colorList; + private String[] colorsList = {"black", "green", "blue", "brown"}; + Eye() { + } + + /** + * @param color + */ Eye(String color) { setColor(color); } + 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 " + colorsList); + } + } + + /** + * @param color + * @return + */ + private boolean validateColor(String color) { + for (String c : colorsList) { + if (color.equals(c)) { + return true; + } + } + return false; } + /** + * @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(); } }