-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Color issue #12
Comments
Yes correct, will try to fix that by making the color handling more spec conform https://developer.mozilla.org/en-US/docs/Web/CSS/color_value |
Any progress on this issue? I am new to JavaCC and do not know yet how to add the support for the "/" token, but could
I have ported the original library (from SourceForge) to C# before I found your improvements/simplifications, but I did further simplifications by turning |
Had already a look at this but had to put it aside because of others.
This method is called with the various parameters (and separators) chained as lexical units. Will be great if you can make a PR for this (including more unit tests) and i will try to merge it as soon as possible. |
Partially yes. Together with the protected LexicalUnit functionInternal(final LexicalUnit prev, final String funct,
final LexicalUnit params) {
if ("counter(".equalsIgnoreCase(funct)) {
return LexicalUnitImpl.createCounter(prev, params);
}
else if ("counters(".equalsIgnoreCase(funct)) {
return LexicalUnitImpl.createCounters(prev, params);
}
else if ("attr(".equalsIgnoreCase(funct)) {
return LexicalUnitImpl.createAttr(prev, params.getStringValue());
}
else if ("rect(".equalsIgnoreCase(funct)) {
return LexicalUnitImpl.createRect(prev, params);
}
else if ("rgb(".equalsIgnoreCase(funct)) {
return LexicalUnitImpl.createRgbColor(prev, params);
}
else if ("calc(".equalsIgnoreCase(funct)) {
return LexicalUnitImpl.createCalc(prev, params);
}
return LexicalUnitImpl.createFunction(
prev,
funct.substring(0, funct.length() - 1),
params);
}
I have not tested your modified codes, but the original throws exception for the rgba(120 75% 50% / 80%) because of the "/" token. Is this fixed in your case?
I could easily contribute that, since I have already handled such cases in my C# codes. The problem is the unsupported token "/", which is used as a separator of the alpha-component of the color. |
ok, will improve the parser tomorrow |
made some fixes here and changed the parser @paulushub |
@rbri That is a lot of work, thanks for the time. Will start testing immediately. |
Another round of fixes, hsl should now work also |
By the new draft Specs: Also for legacy reasons, an rgba() function also exists, with an identical grammar and behavior to rgb(). So, the public RGBColorImpl(final String function, final LexicalUnit lu) throws DOMException
public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMException Currently, the test below fails /**
* @throws Exception if any error occurs
*/
@Test
public void getCssText2() throws Exception {
final LexicalUnit rgbLu = LexicalUnitImpl.createNumber(null, 10);
LexicalUnit lu = LexicalUnitImpl.createComma(rgbLu);
lu = LexicalUnitImpl.createNumber(lu, 20);
lu = LexicalUnitImpl.createComma(lu);
lu = LexicalUnitImpl.createNumber(lu, 30);
lu = LexicalUnitImpl.createComma(lu);
LexicalUnitImpl.createNumber(lu, 0);
//NOTE: Here, I tried empty function name. null will throw exception.
final RGBColorImpl rgb = new RGBColorImpl("", rgbLu);
assertEquals("rgba(10, 20, 30, 0)", rgb.toString());
// Or: assertEquals("rgb(10, 20, 30, 0)", rgb.toString());
} |
There is no set method for the commaSeparated_ member: private boolean commaSeparated_ |
In the protected LexicalUnit hexColorInternal(final LexicalUnit prev, final Token t) {
// Step past the hash at the beginning
final int i = 1;
int r = 0;
int g = 0;
int b = 0;
int a = -1;
final int len = t.image.length() - 1;
try {
if (len == 3 || len == 4) {
r = Integer.parseInt(t.image.substring(i + 0, i + 1), 16);
g = Integer.parseInt(t.image.substring(i + 1, i + 2), 16);
b = Integer.parseInt(t.image.substring(i + 2, i + 3), 16);
r = (r << 4) | r;
g = (g << 4) | g;
b = (b << 4) | b;
if (len == 4) {
a = Integer.parseInt(t.image.substring(i + 3, i + 4), 16);
a = (a << 4) | a;
}
}
else if (len == 6 || len == 8) {
r = Integer.parseInt(t.image.substring(i + 0, i + 2), 16);
g = Integer.parseInt(t.image.substring(i + 2, i + 4), 16);
b = Integer.parseInt(t.image.substring(i + 4, i + 6), 16);
if (len == 8) {
a = Integer.parseInt(t.image.substring(i + 6, i + 8), 16);
}
}
else {
final String pattern = getParserMessage("invalidColor");
throw new CSSParseException(MessageFormat.format(
pattern, new Object[] {t}),
getInputSource().getURI(), t.beginLine,
t.beginColumn);
}
// Turn into an "rgb()"
final LexicalUnit lr = LexicalUnitImpl.createNumber(null, r);
final LexicalUnit lg = LexicalUnitImpl.createNumber(LexicalUnitImpl.createComma(lr), g);
final LexicalUnit lb = LexicalUnitImpl.createNumber(LexicalUnitImpl.createComma(lg), b);
if (a != -1) {
LexicalUnitImpl.createNumber(LexicalUnitImpl.createComma(lb), a);
return LexicalUnitImpl.createRgbColor(prev, "rgba", lr);
}
return LexicalUnitImpl.createRgbColor(prev, "rgb", lr);
}
catch (final NumberFormatException ex) {
final String pattern = getParserMessage("invalidColor");
throw new CSSParseException(MessageFormat.format(
pattern, new Object[] {t}),
getInputSource().getURI(), t.beginLine,
t.beginColumn, ex);
}
} |
hexColorInternal in the hex notation the value is between 0 and 255. We need to divide this by 255 to get the alpha value. |
commaSeparated_ is determined at construction time, there is no need to change afterwards |
npe inside the ctor is fixed |
I think that conversion should be left to the user. Java Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
int getAlpha()
Returns the alpha component in the range 0-255.
I think your simplification went a bit far with elimination of |
@oswetto As the original poster of the issue and with application different from HtmlUnit, please provide a feedback. |
@rbri @paulushub thanks for me works! |
@rbri I missed this, please fix the /**
* @return the red part.
*/
public CSSValueImpl getRed() {
return hue_;
}
/**
* Sets the red part to a new value.
* @param red the new CSSPrimitiveValue
*/
public void setRed(final CSSValueImpl red) {
hue_ = red;
}
/**
* @return the green part.
*/
public CSSValueImpl getGreen() {
return saturation_;
}
/**
* Sets the green part to a new value.
* @param green the new CSSPrimitiveValue
*/
public void setGreen(final CSSValueImpl green) {
saturation_ = green;
}
/**
* @return the blue part.
*/
public CSSValueImpl getBlue() {
return lightness_;
}
/**
* Sets the blue part to a new value.
* @param blue the new CSSPrimitiveValue
*/
public void setBlue(final CSSValueImpl blue) {
lightness_ = blue;
} |
getter/setters are fixed |
i think not :-) There is no chance for the user to figure out if the color was created from a hex notation or an rgb one. If you define one color in both ways the result should be consistent. |
The original idea was to trim the parser to the stuff i need to support my cases in HtmlUnit and be able to move forward (and do not have to take care of sac). This was the reason why all the other stuff is removed. |
I tried to reproduce the orginal value format nothing more |
Sorry, there is more. In the constructors, some of the exception statements are without the throw keyword.
The user does not have to, the primitive type will indicate an number/integer type like the rest of the RGB components, so for color systems using 0-255, no conversion will be needed.
I do understand the requirements of HtmlUnit as a testing platform. |
@rbri Check the b in the exception statement "b requires at least three values."
The statement:
Attached is a test file ( |
Looks like i forgot to close this. Thanks @paulushub for all the work. |
Hi @rbri in the color the char "/" is not supported
color: hsl(120 75% 50% / 80%);
Error in expression. Invalid token "/"
The text was updated successfully, but these errors were encountered: