|
| 1 | +package me.lcw.jil; |
| 2 | + |
| 3 | +/** |
| 4 | + * This class is how colors are passed around to/from the Image Object. |
| 5 | + * |
| 6 | + * Colors are technically *read-only*. There are methods to make a duplicate |
| 7 | + * color with a changed color channel. |
| 8 | + * |
| 9 | + */ |
| 10 | +public class Color implements Comparable<Color> { |
| 11 | + public static final byte MAX_BYTE = (byte)255; |
| 12 | + public static final byte EMPTY_BYTE = (byte)0; |
| 13 | + |
| 14 | + public static final Color BLACK = new Color(EMPTY_BYTE); |
| 15 | + public static final Color WHITE = new Color(MAX_BYTE); |
| 16 | + public static final Color GREY = new Color((byte)127); |
| 17 | + public static final Color RED = new Color(MAX_BYTE, EMPTY_BYTE, EMPTY_BYTE); |
| 18 | + public static final Color GREEN = new Color(EMPTY_BYTE, MAX_BYTE, EMPTY_BYTE); |
| 19 | + public static final Color BLUE = new Color(EMPTY_BYTE, EMPTY_BYTE, MAX_BYTE); |
| 20 | + public static final Color ALPHA = new Color(EMPTY_BYTE, EMPTY_BYTE, EMPTY_BYTE, EMPTY_BYTE); |
| 21 | + |
| 22 | + private final byte green; |
| 23 | + private final byte red; |
| 24 | + private final byte blue; |
| 25 | + private final byte grey; |
| 26 | + private final byte alpha; |
| 27 | + |
| 28 | + |
| 29 | + /** |
| 30 | + * Construct a Grey color. Alpha is set to max value. |
| 31 | + * |
| 32 | + * @param grey grey color level. |
| 33 | + */ |
| 34 | + public Color(byte grey) { |
| 35 | + this.red = grey; |
| 36 | + this.green = grey; |
| 37 | + this.blue = grey; |
| 38 | + this.alpha = MAX_BYTE; |
| 39 | + this.grey = grey; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Construct the color with RGB values set. Alpha will be max value. |
| 44 | + * |
| 45 | + * @param red red color level |
| 46 | + * @param green green color level |
| 47 | + * @param blue blue color level |
| 48 | + */ |
| 49 | + public Color(byte red, byte green, byte blue) { |
| 50 | + this(red, green, blue, MAX_BYTE); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Construct the color with RGBA values set. |
| 55 | + * |
| 56 | + * @param red red color level |
| 57 | + * @param green green color level |
| 58 | + * @param blue blue color level |
| 59 | + * @param alpha alpha level |
| 60 | + */ |
| 61 | + public Color(byte red, byte green, byte blue, byte alpha) { |
| 62 | + this.red = red; |
| 63 | + this.green = green; |
| 64 | + this.blue = blue; |
| 65 | + this.alpha = alpha; |
| 66 | + this.grey = Utils.colorsToGrey(red, green, blue); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Creates a duplicate of this color with the BlueChannel changed. |
| 71 | + * |
| 72 | + * @param lblue value to set the blue color of the new color too. |
| 73 | + */ |
| 74 | + public Color changeBlue(byte lblue) { |
| 75 | + return new Color(red, green, lblue, alpha); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * |
| 80 | + * @return the value of the blue channel for this color. |
| 81 | + */ |
| 82 | + public byte getBlue() { |
| 83 | + return blue; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Creates a duplicate of this color with the RedChannel changed. |
| 88 | + * |
| 89 | + * @param lred value to set the blue color of the new color too. |
| 90 | + */ |
| 91 | + public Color changeRed(byte lred) { |
| 92 | + return new Color(lred, green, blue, alpha); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * |
| 97 | + * @return the value of the red channel for this color. |
| 98 | + */ |
| 99 | + public byte getRed() { |
| 100 | + return red; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Creates a duplicate of this color with the GreenChannel changed. |
| 105 | + * |
| 106 | + * @param lgreen value to set the blue color of the new color too. |
| 107 | + */ |
| 108 | + public Color changeGreen(byte lgreen) { |
| 109 | + return new Color(red, lgreen, blue, alpha); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * |
| 114 | + * @return the value of the green channel for this color. |
| 115 | + */ |
| 116 | + public byte getGreen() { |
| 117 | + return green; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Creates a duplicate of this color with the AlphaChannel changed. |
| 122 | + * |
| 123 | + * @param lalpha value to set the alpha color of the new color too. |
| 124 | + */ |
| 125 | + public Color changeAlpha(byte lalpha) { |
| 126 | + return new Color(red, green, blue, lalpha); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * |
| 131 | + * @return the value of the Alpha channel for this color. |
| 132 | + */ |
| 133 | + public byte getAlpha() { |
| 134 | + return alpha; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Creates a duplicate of this color with the GreyChannel changed. |
| 139 | + * |
| 140 | + * @param lgrey value to set the grey color of the new color too. |
| 141 | + */ |
| 142 | + public Color changeGrey(byte lgrey) { |
| 143 | + return new Color(lgrey); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * |
| 148 | + * @return the value of the Grey channel for this color. |
| 149 | + */ |
| 150 | + public byte getGrey() { |
| 151 | + return grey; |
| 152 | + } |
| 153 | + |
| 154 | + public int getARGB() { |
| 155 | + return alpha<<24 & 0xff000000 | red<<16 & 0xff0000 | green<<8 & 0xff00 | blue & 0xff; |
| 156 | + } |
| 157 | + |
| 158 | + public int getRGBA() { |
| 159 | + return red<<24 & 0xff000000 | green<<16 & 0xff0000 | blue<<8 & 0xff00 |alpha & 0xff; |
| 160 | + } |
| 161 | + |
| 162 | + public int getRGB() { |
| 163 | + return red<<16 & 0xff0000 | green<<8 & 0xff00 | blue & 0xff; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public String toString() { |
| 168 | + return "Colors: R:"+red+" G:"+green+" B:"+blue+" Alpha:"+alpha+" grey:"+grey; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public int hashCode() { |
| 173 | + return getARGB(); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public boolean equals(Object o) { |
| 178 | + if(!(o instanceof Color)) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + Color c = (Color) o; |
| 183 | + long t = getARGB() & 0xffffffffL; |
| 184 | + long ot = c.getARGB() & 0xffffffffL; |
| 185 | + if (t == ot){ |
| 186 | + return true; |
| 187 | + } |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + public boolean equalsNoAlpha(Object o) { |
| 192 | + if(!(o instanceof Color)) { |
| 193 | + return false; |
| 194 | + } |
| 195 | + Color c = (Color) o; |
| 196 | + long t = getRGB() & 0xffffffffL; |
| 197 | + long ot = c.getRGB() & 0xffffffffL; |
| 198 | + if (t == ot){ |
| 199 | + return true; |
| 200 | + } |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public int compareTo(Color o) { |
| 206 | + long t = getARGB() & (long)0xffffffffL; |
| 207 | + long ot = o.getARGB() & (long)0xffffffffL; |
| 208 | + if(t > ot) { |
| 209 | + return 1; |
| 210 | + } else if (t < ot) { |
| 211 | + return -1; |
| 212 | + } |
| 213 | + return 0; |
| 214 | + } |
| 215 | + |
| 216 | +} |
| 217 | + |
| 218 | + |
0 commit comments