From 6b48f5704ad227586a57e15fdeade02e7d7332f7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 8 Jan 2019 16:10:22 +0100 Subject: [PATCH] Implement eye color compareTo() method. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Eye.java | 23 ++++++++++++++++++++--- src/Size.java | 2 -- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Eye.java b/src/Eye.java index abbbfb7..595d3d3 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -1,9 +1,10 @@ import java.awt.Color; import java.util.List; +import java.util.Arrays; public class Eye implements Comparable { private Color color; - private List colorList; + private List colorList = Arrays.asList("black", "green", "blue", "brown"); Eye(String color) { setColor(color); @@ -14,11 +15,27 @@ public class Eye implements Comparable { } 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(); } } diff --git a/src/Size.java b/src/Size.java index 20e96f6..c239207 100644 --- a/src/Size.java +++ b/src/Size.java @@ -1,5 +1,3 @@ -import java.util.Comparator; - public class Size implements Comparable { private int max = 240; private int min = 20; -- 2.34.1