Skip to content

Commit

Permalink
introduce CssColors
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jan 15, 2024
1 parent 68956e1 commit 16c365a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 47 deletions.
74 changes: 74 additions & 0 deletions src/main/java/org/htmlunit/css/CssColors.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -128,6 +127,7 @@
import org.htmlunit.corejs.javascript.Undefined;
import org.htmlunit.css.AbstractCssStyleDeclaration;
import org.htmlunit.css.ComputedCssStyleDeclaration;
import org.htmlunit.css.CssColors;
import org.htmlunit.css.CssPixelValueConverter;
import org.htmlunit.css.ElementCssStyleDeclaration;
import org.htmlunit.css.StyleAttributes;
Expand Down Expand Up @@ -213,30 +213,10 @@ public class CSSStyleDeclaration extends HtmlUnitScriptable {
"inherit", "initial", "revert", "unset"};

// private static final Log LOG = LogFactory.getLog(CSSStyleDeclaration.class);
private static final Map<String, String> CSSColors_ = new HashMap<>();

/** The wrapped CSSStyleDeclaration */
private AbstractCssStyleDeclaration styleDeclaration_;

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)");
}

/**
* Creates an instance.
*/
Expand Down Expand Up @@ -2594,7 +2574,7 @@ private static String findColor(final String text) {

final String[] tokens = org.htmlunit.util.StringUtils.splitAtBlank(text);
for (final String token : tokens) {
if (isColorKeyword(token)) {
if (CssColors.isColorKeyword(token)) {
return token;
}

Expand Down Expand Up @@ -2704,29 +2684,6 @@ private static String findBorderWidth(final String text) {
return null;
}

/**
* 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
*/
private 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;
}

/**
* Returns if the specified token is a border style.
* @param token the token to check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.htmlunit.WebWindow;
import org.htmlunit.corejs.javascript.ES6Iterator;
import org.htmlunit.css.ComputedCssStyleDeclaration;
import org.htmlunit.css.CssColors;
import org.htmlunit.css.CssPixelValueConverter;
import org.htmlunit.css.CssPixelValueConverter.CssValue;
import org.htmlunit.css.StyleAttributes;
Expand Down Expand Up @@ -245,7 +246,7 @@ public String getBackgroundColor() {
if (StringUtils.isEmpty(value)) {
return BACKGROUND_COLOR.getDefaultComputedValue(getBrowserVersion());
}
return toRGBColor(value);
return CssColors.toRGBColor(value);
}

/**
Expand Down Expand Up @@ -383,7 +384,7 @@ public String getBottom() {
@Override
public String getColor() {
final String value = defaultIfEmpty(super.getColor(), "rgb(0, 0, 0)", null);
return toRGBColor(value);
return CssColors.toRGBColor(value);
}

/**
Expand Down

0 comments on commit 16c365a

Please sign in to comment.