KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > util > RGB


1 package de.java2html.util;
2
3 /**
4  * A color representation similar to {@link java.awt.Color}, but more lightweight, since it does not rquire GUI
5  * libraries.
6  * @author Markus Gebhard
7  */

8 public class RGB {
9   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 JavaDoc 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 JavaDoc("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   public boolean equals(Object JavaDoc obj) {
68     if (!(obj instanceof RGB)) {
69       return false;
70     }
71     RGB other = (RGB) obj;
72     return other.getRed() == getRed()
73         && other.getGreen() == getGreen()
74         && other.getBlue() == getBlue();
75   }
76
77   public int hashCode() {
78     return (red & 0xFF) << 16 | (green & 0xFF) << 8 | blue & 0xFF;
79   }
80   
81   /**
82    * Returns a string containing a concise, human-readable
83    * description of the receiver.
84    *
85    * @return a string representation of the <code>RGB</code>
86    */

87   public String JavaDoc toString () {
88     return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
89
}
90 }
Popular Tags