JAL-2094 new classes ColorI, Colour added
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 5 May 2016 13:23:17 +0000 (14:23 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 5 May 2016 13:23:17 +0000 (14:23 +0100)
src/jalview/api/ColorI.java [new file with mode: 0644]
src/jalview/schemes/Colour.java [new file with mode: 0644]

diff --git a/src/jalview/api/ColorI.java b/src/jalview/api/ColorI.java
new file mode 100644 (file)
index 0000000..c46db90
--- /dev/null
@@ -0,0 +1,12 @@
+package jalview.api;
+
+public interface ColorI
+{
+  int getGreen();
+
+  int getBlue();
+
+  int getRed();
+
+  int getRGB();
+}
diff --git a/src/jalview/schemes/Colour.java b/src/jalview/schemes/Colour.java
new file mode 100644 (file)
index 0000000..0156641
--- /dev/null
@@ -0,0 +1,127 @@
+package jalview.schemes;
+
+import jalview.api.ColorI;
+
+import java.awt.Color;
+
+public class Colour implements ColorI
+{
+  public static final ColorI lightGray = new Colour(Color.lightGray);
+
+  public static final ColorI gray = new Colour(Color.gray);
+
+  public static final ColorI blue = new Colour(Color.blue);
+
+  public static final ColorI yellow = new Colour(Color.yellow);
+
+  public static final ColorI red = new Colour(Color.red);
+
+  public static final ColorI black = new Colour(Color.black);
+
+  public static final ColorI white = new Colour(Color.white);
+
+  public static final ColorI pink = new Colour(Color.pink);
+
+  public static final ColorI green = new Colour(Color.green);
+
+  public static final ColorI magenta = new Colour(Color.magenta);
+
+  public static final ColorI orange = new Colour(Color.orange);
+
+  private int greenValue;
+
+  private int blueValue;
+
+  private int redValue;
+
+  public Colour(Color c)
+  {
+    this(c.getRed(), c.getGreen(), c.getBlue());
+  }
+
+  /**
+   * Constructor given RGB values in the range 0-255
+   * 
+   * @param r
+   * @param g
+   * @param b
+   */
+  public Colour(int r, int g, int b)
+  {
+    redValue = r;
+    greenValue = g;
+    blueValue = b;
+  }
+
+  /**
+   * Constructor given an rgb value
+   * 
+   * @param rgb
+   */
+  public Colour(int rgb)
+  {
+    this((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, (rgb >> 0) & 0xFF);
+  }
+
+  /**
+   * Constructor given RGB values in the range 0-1
+   * 
+   * @param r
+   * @param g
+   * @param b
+   */
+  public Colour(float r, float g, float b)
+  {
+    this((int) (r * 255 + 0.5), (int) (g * 255 + 0.5),
+            (int) (b * 255 + 0.5));
+  }
+
+  @Override
+  public int getGreen()
+  {
+    return greenValue;
+  }
+
+  @Override
+  public int getBlue()
+  {
+    return blueValue;
+  }
+
+  @Override
+  public int getRed()
+  {
+    return redValue;
+  }
+
+  /**
+   * 
+   * @see java.awt.Color#Color(int, int, int, int)
+   */
+  @Override
+  public int getRGB()
+  {
+    int value = ((255 & 0xFF) << 24) |
+            ((redValue & 0xFF) << 16) |
+            ((greenValue & 0xFF) << 8)  |
+            ((blueValue & 0xFF) << 0);  
+    return value;
+  }
+
+  @Override
+  public int hashCode()
+  {
+    return getRGB();
+  }
+
+  @Override
+  public boolean equals(Object obj)
+  {
+    if (obj instanceof Colour)
+    {
+      return getRGB() == ((Colour) obj).getRGB();
+    }
+    return false;
+  }
+
+}