1 /*
2 * Copyright (c) 2002-2025 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.Locale;
18 import java.util.Map;
19 import java.util.concurrent.ConcurrentHashMap;
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 ConcurrentHashMap<>();
29
30 static {
31 CSS_COLORS.put("aqua", "rgb(0, 255, 255)");
32 CSS_COLORS.put("black", "rgb(0, 0, 0)");
33 CSS_COLORS.put("blue", "rgb(0, 0, 255)");
34 CSS_COLORS.put("fuchsia", "rgb(255, 0, 255)");
35 CSS_COLORS.put("gray", "rgb(128, 128, 128)");
36 CSS_COLORS.put("green", "rgb(0, 128, 0)");
37 CSS_COLORS.put("lime", "rgb(0, 255, 0)");
38 CSS_COLORS.put("maroon", "rgb(128, 0, 0)");
39 CSS_COLORS.put("navy", "rgb(0, 0, 128)");
40 CSS_COLORS.put("olive", "rgb(128, 128, 0)");
41 CSS_COLORS.put("purple", "rgb(128, 0, 128)");
42 CSS_COLORS.put("red", "rgb(255, 0, 0)");
43 CSS_COLORS.put("silver", "rgb(192, 192, 192)");
44 CSS_COLORS.put("teal", "rgb(0, 128, 128)");
45 CSS_COLORS.put("white", "rgb(255, 255, 255)");
46 CSS_COLORS.put("yellow", "rgb(255, 255, 0)");
47 }
48
49 private CssColors() {
50 // util class
51 }
52
53 /**
54 * Returns if the specified token is a reserved color keyword.
55 * @param token the token to check
56 * @return whether the token is a reserved color keyword or not
57 */
58 public static boolean isColorKeyword(final String token) {
59 return CSS_COLORS.containsKey(token.toLowerCase(Locale.ROOT));
60 }
61
62 /**
63 * Gets the RGB equivalent of a CSS color if the provided color is recognized.
64 * @param color the color
65 * @return the provided color if this is not a recognized color keyword, the RGB value
66 * in the form "rgb(x, y, z)" otherwise
67 */
68 public static String toRGBColor(final String color) {
69 final String rgbValue = CSS_COLORS.get(color.toLowerCase(Locale.ROOT));
70 if (rgbValue != null) {
71 return rgbValue;
72 }
73 return color;
74 }
75 }