View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.css;
16  
17  import java.util.HashMap;
18  import java.util.Locale;
19  import java.util.Map;
20  
21  /**
22   * Helper to work with colors.
23   *
24   * @author Ronald Brill
25   */
26  public final class CssColors {
27  
28      private static final Map<String, String> CSS_COLORS = new HashMap<>();
29  
30      static {
31          // see https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
32  
33          // CSS Level 1
34          CSS_COLORS.put("black", "rgb(0, 0, 0)");
35          CSS_COLORS.put("silver", "rgb(192, 192, 192)");
36          CSS_COLORS.put("gray", "rgb(128, 128, 128)");
37          CSS_COLORS.put("white", "rgb(255, 255, 255)");
38          CSS_COLORS.put("maroon", "rgb(128, 0, 0)");
39          CSS_COLORS.put("red", "rgb(255, 0, 0)");
40          CSS_COLORS.put("purple", "rgb(128, 0, 128)");
41          CSS_COLORS.put("fuchsia", "rgb(255, 0, 255)");
42          CSS_COLORS.put("green", "rgb(0, 128, 0)");
43          CSS_COLORS.put("lime", "rgb(0, 255, 0)");
44          CSS_COLORS.put("olive", "rgb(128, 128, 0)");
45          CSS_COLORS.put("yellow", "rgb(255, 255, 0)");
46          CSS_COLORS.put("navy", "rgb(0, 0, 128)");
47          CSS_COLORS.put("blue", "rgb(0, 0, 255)");
48          CSS_COLORS.put("teal", "rgb(0, 128, 128)");
49          CSS_COLORS.put("aqua", "rgb(0, 255, 255)");
50  
51          // CSS Level 2 (Revision 1)
52          CSS_COLORS.put("orange", "rgb(255, 165, 0)");
53  
54          // CSS Color Module Level 3
55          CSS_COLORS.put("aliceblue", "rgb(240, 248, 255)");
56          CSS_COLORS.put("antiquewhite", "rgb(250, 235, 215)");
57          CSS_COLORS.put("aquamarine", "rgb(127, 255, 212)");
58          CSS_COLORS.put("azure", "rgb(240, 255, 255)");
59          CSS_COLORS.put("beige", "rgb(245, 245, 220)");
60          CSS_COLORS.put("bisque", "rgb(255, 228, 196)");
61          CSS_COLORS.put("blanchedalmond", "rgb(255, 235, 205)");
62          CSS_COLORS.put("blueviolet", "rgb(138, 43, 226)");
63          CSS_COLORS.put("brown", "rgb(165, 42, 42)");
64          CSS_COLORS.put("burlywood", "rgb(222, 184, 135)");
65          CSS_COLORS.put("cadetblue", "rgb(95, 158, 160)");
66          CSS_COLORS.put("chartreuse", "rgb(127, 255, 0)");
67          CSS_COLORS.put("chocolate", "rgb(210, 105, 30)");
68          CSS_COLORS.put("coral", "rgb(255, 127, 80)");
69          CSS_COLORS.put("cornflowerblue", "rgb(100, 149, 237)");
70          CSS_COLORS.put("cornsilk", "rgb(255, 248, 220)");
71          CSS_COLORS.put("crimson", "rgb(220, 20, 60)");
72          CSS_COLORS.put("cyan", "rgb(0, 255, 255)"); // synonym of aqua
73          CSS_COLORS.put("darkblue", "rgb(0, 0, 139)");
74          CSS_COLORS.put("darkcyan", "rgb(0, 139, 139)");
75          CSS_COLORS.put("darkgoldenrod", "rgb(184, 134, 11)");
76          CSS_COLORS.put("darkgray", "rgb(169, 169, 169)");
77          CSS_COLORS.put("darkgreen", "rgb(0, 100, 0)");
78          CSS_COLORS.put("darkgrey", "rgb(169, 169, 169)");
79          CSS_COLORS.put("darkkhaki", "rgb(189, 183, 107)");
80          CSS_COLORS.put("darkmagenta", "rgb(139, 0, 139)");
81          CSS_COLORS.put("darkolivegreen", "rgb(85, 107, 47)");
82          CSS_COLORS.put("darkorange", "rgb(255, 140, 0)");
83          CSS_COLORS.put("darkorchid", "rgb(153, 50, 204)");
84          CSS_COLORS.put("darkred", "rgb(139, 0, 0)");
85          CSS_COLORS.put("darksalmon", "rgb(233, 150, 122)");
86          CSS_COLORS.put("darkseagreen", "rgb(143, 188, 143)");
87          CSS_COLORS.put("darkslateblue", "rgb(72, 61, 139)");
88          CSS_COLORS.put("darkslategray", "rgb(47, 79, 79)");
89          CSS_COLORS.put("darkslategrey", "rgb(47, 79, 79)");
90          CSS_COLORS.put("darkturquoise", "rgb(0, 206, 209)");
91          CSS_COLORS.put("darkviolet", "rgb(148, 0, 211)");
92          CSS_COLORS.put("deeppink", "rgb(255, 20, 147)");
93          CSS_COLORS.put("deepskyblue", "rgb(0, 191, 255)");
94          CSS_COLORS.put("dimgray", "rgb(105, 105, 105)");
95          CSS_COLORS.put("dimgrey", "rgb(105, 105, 105)");
96          CSS_COLORS.put("dodgerblue", "rgb(30, 144, 255)");
97          CSS_COLORS.put("firebrick", "rgb(178, 34, 34)");
98          CSS_COLORS.put("floralwhite", "rgb(255, 250, 240)");
99          CSS_COLORS.put("forestgreen", "rgb(34, 139, 34)");
100         CSS_COLORS.put("gainsboro", "rgb(220, 220, 220)");
101         CSS_COLORS.put("ghostwhite", "rgb(248, 248, 255)");
102         CSS_COLORS.put("gold", "rgb(255, 215, 0)");
103         CSS_COLORS.put("goldenrod", "rgb(218, 165, 32)");
104         CSS_COLORS.put("greenyellow", "rgb(173, 255, 47)");
105         CSS_COLORS.put("grey", "rgb(128, 128, 128)");
106         CSS_COLORS.put("honeydew", "rgb(240, 255, 240)");
107         CSS_COLORS.put("hotpink", "rgb(255, 105, 180)");
108         CSS_COLORS.put("indianred", "rgb(205, 92, 92)");
109         CSS_COLORS.put("indigo", "rgb(75, 0, 130)");
110         CSS_COLORS.put("ivory", "rgb(255, 255, 240)");
111         CSS_COLORS.put("khaki", "rgb(240, 230, 140)");
112         CSS_COLORS.put("lavender", "rgb(230, 230, 250)");
113         CSS_COLORS.put("lavenderblush", "rgb(255, 240, 245)");
114         CSS_COLORS.put("lawngreen", "rgb(124, 252, 0)");
115         CSS_COLORS.put("lemonchiffon", "rgb(255, 250, 205)");
116         CSS_COLORS.put("lightblue", "rgb(173, 216, 230)");
117         CSS_COLORS.put("lightcoral", "rgb(240, 128, 128)");
118         CSS_COLORS.put("lightcyan", "rgb(224, 255, 255)");
119         CSS_COLORS.put("lightgoldenrodyellow", "rgb(250, 250, 210)");
120         CSS_COLORS.put("lightgray", "rgb(211, 211, 211)");
121         CSS_COLORS.put("lightgreen", "rgb(144, 238, 144)");
122         CSS_COLORS.put("lightgrey", "rgb(211, 211, 211)");
123         CSS_COLORS.put("lightpink", "rgb(255, 182, 193)");
124         CSS_COLORS.put("lightsalmon", "rgb(255, 160, 122)");
125         CSS_COLORS.put("lightseagreen", "rgb(32, 178, 170)");
126         CSS_COLORS.put("lightskyblue", "rgb(135, 206, 250)");
127         CSS_COLORS.put("lightslategray", "rgb(119, 136, 153)");
128         CSS_COLORS.put("lightslategrey", "rgb(119, 136, 153)");
129         CSS_COLORS.put("lightsteelblue", "rgb(176, 196, 222)");
130         CSS_COLORS.put("lightyellow", "rgb(255, 255, 224)");
131         CSS_COLORS.put("limegreen", "rgb(50, 205, 50)");
132         CSS_COLORS.put("linen", "rgb(250, 240, 230)");
133         CSS_COLORS.put("magenta", "rgb(255, 0, 255)"); // synonym of fuchsia
134         CSS_COLORS.put("mediumaquamarine", "rgb(102, 205, 170)");
135         CSS_COLORS.put("mediumblue", "rgb(0, 0, 205)");
136         CSS_COLORS.put("mediumorchid", "rgb(186, 85, 211)");
137         CSS_COLORS.put("mediumpurple", "rgb(147, 112, 219)");
138         CSS_COLORS.put("mediumseagreen", "rgb(60, 179, 113)");
139         CSS_COLORS.put("mediumslateblue", "rgb(123, 104, 238)");
140         CSS_COLORS.put("mediumspringgreen", "rgb(0, 250, 154)");
141         CSS_COLORS.put("mediumturquoise", "rgb(72, 209, 204)");
142         CSS_COLORS.put("mediumvioletred", "rgb(199, 21, 133)");
143         CSS_COLORS.put("midnightblue", "rgb(25, 25, 112)");
144         CSS_COLORS.put("mintcream", "rgb(245, 255, 250)");
145         CSS_COLORS.put("mistyrose", "rgb(255, 228, 225)");
146         CSS_COLORS.put("moccasin", "rgb(255, 228, 181)");
147         CSS_COLORS.put("navajowhite", "rgb(255, 222, 173)");
148         CSS_COLORS.put("oldlace", "rgb(253, 245, 230)");
149         CSS_COLORS.put("olivedrab", "rgb(107, 142, 35)");
150         CSS_COLORS.put("orangered", "rgb(255, 69, 0)");
151         CSS_COLORS.put("orchid", "rgb(218, 112, 214)");
152         CSS_COLORS.put("palegoldenrod", "rgb(238, 232, 170)");
153         CSS_COLORS.put("palegreen", "rgb(152, 251, 152)");
154         CSS_COLORS.put("paleturquoise", "rgb(175, 238, 238)");
155         CSS_COLORS.put("palevioletred", "rgb(219, 112, 147)");
156         CSS_COLORS.put("papayawhip", "rgb(255, 239, 213)");
157         CSS_COLORS.put("peachpuff", "rgb(255, 218, 185)");
158         CSS_COLORS.put("peru", "rgb(205, 133, 63)");
159         CSS_COLORS.put("pink", "rgb(255, 192, 203)");
160         CSS_COLORS.put("plum", "rgb(221, 160, 221)");
161         CSS_COLORS.put("powderblue", "rgb(176, 224, 230)");
162         CSS_COLORS.put("rosybrown", "rgb(188, 143, 143)");
163         CSS_COLORS.put("royalblue", "rgb(65, 105, 225)");
164         CSS_COLORS.put("saddlebrown", "rgb(139, 69, 19)");
165         CSS_COLORS.put("salmon", "rgb(250, 128, 114)");
166         CSS_COLORS.put("sandybrown", "rgb(244, 164, 96)");
167         CSS_COLORS.put("seagreen", "rgb(46, 139, 87)");
168         CSS_COLORS.put("seashell", "rgb(255, 245, 238)");
169         CSS_COLORS.put("sienna", "rgb(160, 82, 45)");
170         CSS_COLORS.put("skyblue", "rgb(135, 206, 235)");
171         CSS_COLORS.put("slateblue", "rgb(106, 90, 205)");
172         CSS_COLORS.put("slategray", "rgb(112, 128, 144)");
173         CSS_COLORS.put("slategrey", "rgb(112, 128, 144)");
174         CSS_COLORS.put("snow", "rgb(255, 250, 250)");
175         CSS_COLORS.put("springgreen", "rgb(0, 255, 127)");
176         CSS_COLORS.put("steelblue", "rgb(70, 130, 180)");
177         CSS_COLORS.put("tan", "rgb(210, 180, 140)");
178         CSS_COLORS.put("thistle", "rgb(216, 191, 216)");
179         CSS_COLORS.put("tomato", "rgb(255, 99, 71)");
180         CSS_COLORS.put("turquoise", "rgb(64, 224, 208)");
181         CSS_COLORS.put("violet", "rgb(238, 130, 238)");
182         CSS_COLORS.put("wheat", "rgb(245, 222, 179)");
183         CSS_COLORS.put("whitesmoke", "rgb(245, 245, 245)");
184         CSS_COLORS.put("yellowgreen", "rgb(154, 205, 50)");
185 
186         // CSS Color Module Level 4
187         CSS_COLORS.put("rebeccapurple", "rgb(102, 51, 153)");
188     }
189 
190     private CssColors() {
191         // util class
192     }
193 
194     /**
195      * Returns if the specified token is a reserved color keyword.
196      * @param token the token to check
197      * @return whether the token is a reserved color keyword or not
198      */
199     public static boolean isColorKeyword(final String token) {
200         return CSS_COLORS.containsKey(token.toLowerCase(Locale.ROOT));
201     }
202 
203     /**
204      * Gets the RGB equivalent of a CSS color if the provided color is recognized.
205      * @param color the color
206      * @return the provided color if this is not a recognized color keyword, the RGB value
207      *         in the form "rgb(x, y, z)" otherwise
208      */
209     public static String toRGBColor(final String color) {
210         final String rgbValue = CSS_COLORS.get(color.toLowerCase(Locale.ROOT));
211         if (rgbValue != null) {
212             return rgbValue;
213         }
214         return color;
215     }
216 }