01 package de.java2html.util;
02
03 /**
04 * A color representation similar to {@link java.awt.Color}, but more lightweight, since it does not rquire GUI
05 * libraries.
06 * @author Markus Gebhard
07 */
08 public class RGB {
09 public static final RGB MAGENTA = new RGB(255, 0, 255);
10 public static final RGB GREEN = new RGB(0, 255, 0);
11 public static final RGB BLACK = new RGB(0, 0, 0);
12 public static final RGB RED = new RGB(255, 0, 0);
13 public static final RGB WHITE = new RGB(255, 255, 255);
14 public static final RGB ORANGE = new RGB(255, 200, 0);
15 public final static RGB CYAN = new RGB(0, 255, 255);
16 public final static RGB BLUE = new RGB(0, 0, 255);
17 public final static RGB LIGHT_GRAY = new RGB(192, 192, 192);
18 public final static RGB GRAY = new RGB(128, 128, 128);
19 public final static RGB DARK_GRAY = new RGB(64, 64, 64);
20 public final static RGB YELLOW = new RGB(255, 255, 0);
21 public final static RGB PINK = new RGB(255, 175, 175);
22
23 private int red;
24 private int green;
25 private int blue;
26
27 public RGB(int red, int green, int blue) {
28 assertColorValueRange(red, green, blue);
29 this.red = red;
30 this.green = green;
31 this.blue = blue;
32 }
33
34 private static void assertColorValueRange(int red, int green, int blue) {
35 boolean rangeError = false;
36 String badComponentString = ""; //$NON-NLS-1$
37 if (red < 0 || red > 255) {
38 rangeError = true;
39 badComponentString = badComponentString + " Red"; //$NON-NLS-1$
40 }
41 if (green < 0 || green > 255) {
42 rangeError = true;
43 badComponentString = badComponentString + " Green"; //$NON-NLS-1$
44 }
45 if (blue < 0 || blue > 255) {
46 rangeError = true;
47 badComponentString = badComponentString + " Blue"; //$NON-NLS-1$
48 }
49 if (rangeError == true) {
50 throw new IllegalArgumentException("Color parameter outside of expected range:" //$NON-NLS-1$
51 + badComponentString);
52 }
53 }
54
55 public int getRed() {
56 return red;
57 }
58
59 public int getGreen() {
60 return green;
61 }
62
63 public int getBlue() {
64 return blue;
65 }
66
67 @Override
68 public boolean equals(Object obj) {
69 if (!(obj instanceof RGB)) {
70 return false;
71 }
72 final RGB other = (RGB) obj;
73 return other.getRed() == getRed()
74 && other.getGreen() == getGreen()
75 && other.getBlue() == getBlue();
76 }
77
78 @Override
79 public int hashCode() {
80 return (red & 0xFF) << 16 | (green & 0xFF) << 8 | blue & 0xFF;
81 }
82
83 /**
84 * Returns a string containing a concise, human-readable
85 * description of the receiver.
86 *
87 * @return a string representation of the <code>RGB</code>
88 */
89 @Override
90 public String toString () {
91 return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
92 }
93 }
|