1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
1: import java.awt.*;
2:
3: /**
4: * Colors is an enumeration class that makes it easier to work with colors. Methods are provided for
5: * conversion to hex strings, and for getting alpha channel colors.
6: *
7: * @author Nazmul Idris
8: * @version 1.0
9: * @since Apr 21, 2007, 12:55:24 PM
10: */
11: public enum Colors {
12:
13: //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
14: // various colors in the pallete
15: //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
16: Pink(255, 175, 175),
17: Green(159, 205, 20),
18: Orange(213, 113, 13),
19: Yellow(Color.yellow),
20: Red(189, 67, 67),
21: LightBlue(208, 223, 245),
22: Blue(Color.blue),
23: Black(0, 0, 0),
24: White(255, 255, 255),
25: Gray(Color.gray.getRed(), Color.gray.getGreen(), Color.gray.getBlue());
26:
27: //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
28: // constructors
29: //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
30:
31: Colors(Color c) {
32: _myColor = c;
33: }
34:
35: Colors(int r, int g, int b) {
36: _myColor = new Color(r, g, b);
37: }
38:
39: Colors(int r, int g, int b, int alpha) {
40: _myColor = new Color(r, g, b, alpha);
41: }
42:
43: Colors(float r, float g, float b, float alpha) {
44: _myColor = new Color(r, g, b, alpha);
45: }
46:
47: //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
48: // data
49: //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
50:
51: private Color _myColor;
52:
53: //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
54: // methods
55: //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
56:
57: public Color alpha(float t) {
58: return new Color(_myColor.getRed(), _myColor.getGreen(), _myColor.getBlue(), (int) (t * 255f));
59: }
60:
61: public static Color alpha(Color c, float t) {
62: return new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (t * 255f));
63: }
64:
65: public Color color() { return _myColor; }
66:
67: public Color color(float f) {
68: return alpha(f);
69: }
70:
71: public String toString() {
72: StringBuilder sb = new StringBuilder();
73: sb.append("r=")
74: .append(_myColor.getRed())
75: .append(", g=")
76: .append(_myColor.getGreen())
77: .append(", b=")
78: .append(_myColor.getBlue())
79: .append("\n");
80: return sb.toString();
81: }
82:
83: public String toHexString() {
84: Color c = _myColor;
85: StringBuilder sb = new StringBuilder();
86: sb.append("#");
87: sb.append(Integer.toHexString(_myColor.getRed()));
88: sb.append(Integer.toHexString(_myColor.getGreen()));
89: sb.append(Integer.toHexString(_myColor.getBlue()));
90: return sb.toString();
91: }
92:
93: }//end enum Colors |
Partager