JAL-2094 new classes ColorI, Colour added
[jalview.git] / src / jalview / schemes / Colour.java
1 package jalview.schemes;
2
3 import jalview.api.ColorI;
4
5 import java.awt.Color;
6
7 public class Colour implements ColorI
8 {
9   public static final ColorI lightGray = new Colour(Color.lightGray);
10
11   public static final ColorI gray = new Colour(Color.gray);
12
13   public static final ColorI blue = new Colour(Color.blue);
14
15   public static final ColorI yellow = new Colour(Color.yellow);
16
17   public static final ColorI red = new Colour(Color.red);
18
19   public static final ColorI black = new Colour(Color.black);
20
21   public static final ColorI white = new Colour(Color.white);
22
23   public static final ColorI pink = new Colour(Color.pink);
24
25   public static final ColorI green = new Colour(Color.green);
26
27   public static final ColorI magenta = new Colour(Color.magenta);
28
29   public static final ColorI orange = new Colour(Color.orange);
30
31   private int greenValue;
32
33   private int blueValue;
34
35   private int redValue;
36
37   public Colour(Color c)
38   {
39     this(c.getRed(), c.getGreen(), c.getBlue());
40   }
41
42   /**
43    * Constructor given RGB values in the range 0-255
44    * 
45    * @param r
46    * @param g
47    * @param b
48    */
49   public Colour(int r, int g, int b)
50   {
51     redValue = r;
52     greenValue = g;
53     blueValue = b;
54   }
55
56   /**
57    * Constructor given an rgb value
58    * 
59    * @param rgb
60    */
61   public Colour(int rgb)
62   {
63     this((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, (rgb >> 0) & 0xFF);
64   }
65
66   /**
67    * Constructor given RGB values in the range 0-1
68    * 
69    * @param r
70    * @param g
71    * @param b
72    */
73   public Colour(float r, float g, float b)
74   {
75     this((int) (r * 255 + 0.5), (int) (g * 255 + 0.5),
76             (int) (b * 255 + 0.5));
77   }
78
79   @Override
80   public int getGreen()
81   {
82     return greenValue;
83   }
84
85   @Override
86   public int getBlue()
87   {
88     return blueValue;
89   }
90
91   @Override
92   public int getRed()
93   {
94     return redValue;
95   }
96
97   /**
98    * 
99    * @see java.awt.Color#Color(int, int, int, int)
100    */
101   @Override
102   public int getRGB()
103   {
104     int value = ((255 & 0xFF) << 24) |
105             ((redValue & 0xFF) << 16) |
106             ((greenValue & 0xFF) << 8)  |
107             ((blueValue & 0xFF) << 0);  
108     return value;
109   }
110
111   @Override
112   public int hashCode()
113   {
114     return getRGB();
115   }
116
117   @Override
118   public boolean equals(Object obj)
119   {
120     if (obj instanceof Colour)
121     {
122       return getRGB() == ((Colour) obj).getRGB();
123     }
124     return false;
125   }
126
127 }