1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.eclipse.common.util;
17
18 import org.eclipse.jface.resource.ColorDescriptor;
19 import org.eclipse.jface.resource.ColorRegistry;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.RGB;
22
23
24
25
26
27
28 public class ColorManager {
29 private static ColorRegistry registry = new ColorRegistry();
30
31 private static final String SHARP = "#";
32
33 private static final String ZERO = "0";
34
35 private ColorManager() {
36 }
37
38
39
40
41
42
43
44
45
46
47 public static void putColor(final String symbolicName, final RGB colorData) {
48 registry.put(symbolicName, colorData);
49 }
50
51
52
53
54
55
56
57
58
59 public static void putColor(final RGB colorData) {
60 StringBuffer symbolicName = new StringBuffer(7);
61 symbolicName.append(SHARP).append(toFixedHexString(colorData.red))
62 .append(toFixedHexString(colorData.green)).append(
63 toFixedHexString(colorData.blue));
64 putColor(symbolicName.toString(), colorData);
65 }
66
67 private static String toFixedHexString(final int value) {
68 return (value < 0x10) ? ZERO + Integer.toHexString(value) : Integer
69 .toHexString(value);
70 }
71
72
73
74
75
76
77
78
79
80 public static Color getColor(final String symbolicName) {
81 Color color = registry.get(symbolicName);
82 if (color == null) {
83 String symbol = putColorByColorText(symbolicName);
84 if (symbol != null) {
85 color = registry.get(symbol);
86 }
87 }
88 return color;
89 }
90
91
92
93
94
95
96
97
98
99 public static ColorDescriptor getColorDescriptor(final String symbolicName) {
100 ColorDescriptor color = registry.getColorDescriptor(symbolicName);
101 if (color == null) {
102 String symbol = putColorByColorText(symbolicName);
103 if (symbol != null) {
104 color = registry.getColorDescriptor(symbol);
105 }
106 }
107 return color;
108 }
109
110
111
112
113
114
115
116
117
118
119 public static String putColorByColorText(final String colorText) {
120 if (colorText.startsWith(SHARP) && colorText.length() == 7) {
121 int red = Integer.parseInt(colorText.substring(1, 3), 16);
122 int green = Integer.parseInt(colorText.substring(3, 5), 16);
123 int blue = Integer.parseInt(colorText.substring(5, 7), 16);
124 RGB rgb = new RGB(red, green, blue);
125 String newName = colorText.toUpperCase();
126 putColor(newName, rgb);
127
128 return newName;
129 }
130 return null;
131 }
132 }