Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.w3c.dom.css.CSSPrimitiveValue;
import org.w3c.dom.css.RGBColor;
import org.eclipse.e4.ui.css.core.impl.dom.CssValues.CssColor;
import org.eclipse.e4.ui.css.core.impl.dom.CssValues.CssNumber;

/**
* CSS2 Color Helper.
Expand All @@ -38,20 +38,21 @@ public class CSS2ColorHelper {
private static Map<String, String> colorHexasMap = new HashMap<>();

/**
* Return w3c {@link RGBColor} from string value. Format String value is
* Return a {@link CssColor} from string value. Format String value is
* hexadecimal like #FFFFFF or color name like white.
*
* @param value string representation of color
* @return parsed color
*/
public static RGBColor getRGBColor(String value) {
public static CssColor getRGBColor(String value) {
if (value.startsWith("#") && value.length() == 7) {
// Color is like #FFFFFF
try {
int redValue = Integer.decode("0x" + value.substring(1, 3)).intValue();
int greenValue = Integer.decode("0x" + value.substring(3, 5)).intValue();
int blueValue = Integer.decode("0x" + value.substring(5)).intValue();
return new CSS2RGBColorImpl(redValue, greenValue, blueValue);
return new CssColor(new CssNumber(redValue, true), new CssNumber(greenValue, true),
new CssNumber(blueValue, true));
} catch (Exception e) {
return null;
}
Expand All @@ -66,61 +67,53 @@ public static RGBColor getRGBColor(String value) {
}

/**
* Return the hex string representation of the given w3c {@code rgbColor}.
* Return the hex string representation of the given <code>color</code>.
*
* @param rgbColor the color to get a string representation for
* @return the hex string representation of {@code rgbColor}
* @param color the color to get a string representation for
* @return the hex string representation of {@code color}
*/
public static String getColorStringValue(RGBColor rgbColor) {
return getHexaColorStringValue(rgbColor);
public static String getColorStringValue(CssColor color) {
return getHexaColorStringValue(color);
}

/**
* Return rgb (ex : rgb(0,0,0)) color string value from w3c
* <code>rgbColor</code> instance.
* Return rgb (ex : rgb(0,0,0)) color string value from the given
* <code>color</code>.
*
* @param rgbColor the color to get string representation for
* @return rgbColor as rgb(r, g, b) string
* @param color the color to get string representation for
* @return color as rgb(r, g, b) string
*/
public static String getRGBColorStringValue(RGBColor rgbColor) {
public static String getRGBColorStringValue(CssColor color) {
StringBuilder result = new StringBuilder("rgb(");
int red = (int) rgbColor.getRed().getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
result.append(red);
result.append((int) color.red().value());
result.append(",");
int green = (int) rgbColor.getGreen().getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
result.append(green);
result.append((int) color.green().value());
result.append(",");
int blue = (int) rgbColor.getBlue().getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
result.append(blue);
result.append((int) color.blue().value());
result.append(")");
return result.toString();
}

/**
* Return hexadecimal (ex : #FFFFFF) color string value from w3c
* <code>rgbColor</code> instance.
* Return hexadecimal (ex : #FFFFFF) color string value from the given
* <code>color</code>.
*
* @param rgbColor the color to get string representation for
* @return rgbColor as hexa string
* @param color the color to get string representation for
* @return color as hexa string
*/
public static String getHexaColorStringValue(RGBColor rgbColor) {
public static String getHexaColorStringValue(CssColor color) {
StringBuilder result = new StringBuilder("#");
int red = (int) rgbColor.getRed().getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
if (red < 16) {
result.append("0");
}
result.append(Integer.toHexString(red));
int green = (int) rgbColor.getGreen().getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
if (green < 16) {
result.append("0");
}
result.append(Integer.toHexString(green));
int blue = (int) rgbColor.getBlue().getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
if (blue < 16) {
appendHexPair(result, (int) color.red().value());
appendHexPair(result, (int) color.green().value());
appendHexPair(result, (int) color.blue().value());
return result.toString();
}

private static void appendHexPair(StringBuilder result, int component) {
if (component < 16) {
result.append("0");
}
result.append(Integer.toHexString(blue));
return result.toString();
result.append(Integer.toHexString(component));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
*******************************************************************************/
package org.eclipse.e4.ui.css.core.css2;

import org.w3c.dom.css.CSSPrimitiveValue;
import org.eclipse.e4.ui.css.core.impl.dom.CssValues.CssNumeric;
import org.eclipse.e4.ui.css.core.impl.dom.CssValues.CssPrimitive;
import org.eclipse.e4.ui.css.core.impl.dom.CssValues.CssText;
import org.eclipse.e4.ui.css.core.impl.dom.CssValues.CssUnit;

/**
* CSS2 Font Helper.
Expand Down Expand Up @@ -65,14 +68,11 @@ public static String getFontWeight(boolean isBold) {

/**
* Return the CSS Font Property name (font-style, font-weight, font-size,
* font-family) switch the {@link CSSPrimitiveValue} <code>value</code>.
* font-family) for the given <code>value</code>.
*/
public static String getCSSFontPropertyName(CSSPrimitiveValue value) {
short type = value.getPrimitiveType();
switch (type) {
case CSSPrimitiveValue.CSS_STRING:
case CSSPrimitiveValue.CSS_IDENT:
switch (value.getStringValue()) {
public static String getCSSFontPropertyName(CssPrimitive value) {
if (value instanceof CssText text && (text.kind() == CssText.Kind.STRING || text.kind() == CssText.Kind.IDENT)) {
switch (text.value()) {
case "italic":
case "oblique":
return "font-style";
Expand All @@ -83,9 +83,9 @@ public static String getCSSFontPropertyName(CSSPrimitiveValue value) {
default:
return "font-family";
}
case CSSPrimitiveValue.CSS_PT:
case CSSPrimitiveValue.CSS_NUMBER:
case CSSPrimitiveValue.CSS_PX:
}
if (value instanceof CssNumeric numeric
&& (numeric.unit() == CssUnit.PT || numeric.unit() == CssUnit.NUMBER || numeric.unit() == CssUnit.PX)) {
return "font-size";
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontProperties;
import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontPropertiesImpl;
import org.eclipse.e4.ui.css.core.engine.CSSElementContext;
import org.w3c.dom.css.CSSPrimitiveValue;
import org.eclipse.e4.ui.css.core.impl.dom.CssValues.CssList;
import org.eclipse.e4.ui.css.core.impl.dom.CssValues.CssPrimitive;
import org.w3c.dom.css.CSSValue;
import org.w3c.dom.css.CSSValueList;

/**
*
Expand Down Expand Up @@ -90,19 +90,15 @@ public static void updateCSSPropertyFont(CSS2FontProperties fontProperties, Stri

/**
* Update <code>fontProperties</code> instance with the {@link CSSValue}
* <code>value</code>. value can be {@link CSSPrimitiveValue} or
* {@link CSSValueList}.
* <code>value</code>. value can be a single value or a value list.
*/
public static void updateCSSPropertyFontComposite(CSS2FontProperties font, CSSValue value) {
if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
CSSValueList valueList = (CSSValueList) value;
int length = valueList.getLength();
for (int i = 0; i < length; i++) {
CSSValue value2 = valueList.item(i);
updateCSSPropertyFontComposite(font, value2);
if (value instanceof CssList list) {
for (CSSValue item : list.values()) {
updateCSSPropertyFontComposite(font, item);
}
} else if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
String property = CSS2FontHelper.getCSSFontPropertyName((CSSPrimitiveValue) value);
} else if (value instanceof CssPrimitive primitive) {
String property = CSS2FontHelper.getCSSFontPropertyName(primitive);
updateCSSPropertyFont(font, property, value);
}
}
Expand All @@ -111,35 +107,36 @@ public static void updateCSSPropertyFontComposite(CSS2FontProperties font, CSSVa
* Update CSS2FontProperties instance with font-family.
*/
public static void updateCSSPropertyFontFamily(CSS2FontProperties font, CSSValue value) {
if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
font.setFamily((CSSPrimitiveValue) value);
if (value instanceof CssPrimitive primitive) {
font.setFamily(primitive);
}
}

/**
* Update CSS2FontProperties instance with font-size.
*/
public static void updateCSSPropertyFontSize(CSS2FontProperties font, CSSValue value) {
if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
font.setSize((CSSPrimitiveValue) value);
if (value instanceof CssPrimitive primitive) {
font.setSize(primitive);
font.setSizeFromCSS(true);
}
}

/**
* Update CSS2FontProperties instance with font-style.
*/
public static void updateCSSPropertyFontStyle(CSS2FontProperties font, CSSValue value) {
if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
font.setStyle((CSSPrimitiveValue) value);
if (value instanceof CssPrimitive primitive) {
font.setStyle(primitive);
}
}

/**
* Update CSS2FontProperties instance with font-weight.
*/
public static void updateCSSPropertyFontWeight(CSS2FontProperties font, CSSValue value) {
if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
font.setWeight((CSSPrimitiveValue) value);
if (value instanceof CssPrimitive primitive) {
font.setWeight(primitive);
}
}
}

This file was deleted.

Loading
Loading