-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2002-2024 Gargoyle Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.htmlunit.css; | ||
|
||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
/** | ||
* Helper to work with colors. | ||
* | ||
* @author Ronald Brill | ||
*/ | ||
public final class CssColors { | ||
|
||
private static final Map<String, String> CSSColors_ = new HashMap<>(); | ||
|
||
static { | ||
CSSColors_.put("aqua", "rgb(0, 255, 255)"); | ||
CSSColors_.put("black", "rgb(0, 0, 0)"); | ||
CSSColors_.put("blue", "rgb(0, 0, 255)"); | ||
CSSColors_.put("fuchsia", "rgb(255, 0, 255)"); | ||
CSSColors_.put("gray", "rgb(128, 128, 128)"); | ||
CSSColors_.put("green", "rgb(0, 128, 0)"); | ||
CSSColors_.put("lime", "rgb(0, 255, 0)"); | ||
CSSColors_.put("maroon", "rgb(128, 0, 0)"); | ||
CSSColors_.put("navy", "rgb(0, 0, 128)"); | ||
CSSColors_.put("olive", "rgb(128, 128, 0)"); | ||
CSSColors_.put("purple", "rgb(128, 0, 128)"); | ||
CSSColors_.put("red", "rgb(255, 0, 0)"); | ||
CSSColors_.put("silver", "rgb(192, 192, 192)"); | ||
CSSColors_.put("teal", "rgb(0, 128, 128)"); | ||
CSSColors_.put("white", "rgb(255, 255, 255)"); | ||
CSSColors_.put("yellow", "rgb(255, 255, 0)"); | ||
} | ||
|
||
private CssColors() { | ||
} | ||
|
||
/** | ||
* Returns if the specified token is a reserved color keyword. | ||
* @param token the token to check | ||
* @return whether the token is a reserved color keyword or not | ||
*/ | ||
public static boolean isColorKeyword(final String token) { | ||
return CSSColors_.containsKey(token.toLowerCase(Locale.ROOT)); | ||
} | ||
|
||
/** | ||
* Gets the RGB equivalent of a CSS color if the provided color is recognized. | ||
* @param color the color | ||
* @return the provided color if this is not a recognized color keyword, the RGB value | ||
* in the form "rgb(x, y, z)" otherwise | ||
*/ | ||
public static String toRGBColor(final String color) { | ||
final String rgbValue = CSSColors_.get(color.toLowerCase(Locale.ROOT)); | ||
if (rgbValue != null) { | ||
return rgbValue; | ||
} | ||
return color; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters