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,
35: int blue) {
36: boolean rangeError = false;
37: String badComponentString = ""; //$NON-NLS-1$
38: if (red < 0 || red > 255) {
39: rangeError = true;
40: badComponentString = badComponentString + " Red"; //$NON-NLS-1$
41: }
42: if (green < 0 || green > 255) {
43: rangeError = true;
44: badComponentString = badComponentString + " Green"; //$NON-NLS-1$
45: }
46: if (blue < 0 || blue > 255) {
47: rangeError = true;
48: badComponentString = badComponentString + " Blue"; //$NON-NLS-1$
49: }
50: if (rangeError == true) {
51: throw new IllegalArgumentException(
52: "Color parameter outside of expected range:" //$NON-NLS-1$
53: + badComponentString);
54: }
55: }
56:
57: public int getRed() {
58: return red;
59: }
60:
61: public int getGreen() {
62: return green;
63: }
64:
65: public int getBlue() {
66: return blue;
67: }
68:
69: public boolean equals(Object obj) {
70: if (!(obj instanceof RGB)) {
71: return false;
72: }
73: RGB other = (RGB) obj;
74: return other.getRed() == getRed()
75: && other.getGreen() == getGreen()
76: && other.getBlue() == getBlue();
77: }
78:
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: public String toString() {
90: return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
91: }
92: }
|