Skip to content
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

Add format option to convert all colors to 6 character hex codes #728

Open
hosef opened this issue Sep 28, 2024 · 8 comments
Open

Add format option to convert all colors to 6 character hex codes #728

hosef opened this issue Sep 28, 2024 · 8 comments

Comments

@hosef
Copy link

hosef commented Sep 28, 2024

In the HTML 5 spec for color input elements, it says that values should always be a 6character hex code. If it does not match that format, then the browser will use #000000 as the default value for your fields.

It would be really handy to have the option to convert all of the colors to that format so that they can be used in forms without needing to do further processing of them.

@oliverklee
Copy link
Contributor

Hi @hosef, thanks for suggesting this!

Do you have a link to the part of the reference that says that color values need to be six-digit hex codes?

According to https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color, there are multiple formats, ranging from 3- to 8-value formats.

Also, I'm not sure normalizing the color format should be a responsibility of the CSS parser. IMHO, a parser should return the CSS as it is and not transform ist. (That's one of the reasons we're going to drop the shorthand notation expansion in #511.)

@sabberworm @JakeQZ What do you think?

@hosef
Copy link
Author

hosef commented Sep 28, 2024

It is from the HTML5 spec, not the CSS spec. I am not sure why, but HTML color inputs require exactly that format to use them.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color#value

@oliverklee
Copy link
Contributor

Thanks! I wasn't aware of that.

Still, I tend to think this is outside of the scope of parsing CSS, but should be done by the PHP that uses the parsed CSS. I'd like to hear the thoughts of the other project maintainers on this.

@sabberworm
Copy link
Contributor

Also, I'm not sure normalizing the color format should be a responsibility of the CSS parser. IMHO, a parser should return the CSS as it is and not transform it. (That's one of the reasons we're going to drop the shorthand notation expansion in #511.)

I agree it’s outside the scope of parsing CSS. However, I don’t think it’s outside the scope of stringifying to CSS, which this library also does. As a matter of fact, I think OutputFormat already has an option bRGBHashNotation that does exactly this, if I’m not mistaken.

@oliverklee
Copy link
Contributor

It indeed has that option! https://github.com/MyIntervals/PHP-CSS-Parser/blob/main/src/OutputFormat.php#L27

@hosef Does this option solve your request?

@hosef
Copy link
Author

hosef commented Oct 4, 2024

@oliverklee I did see that. It is better than not using it, but unfortunately it still doesn't work because it tries to convert the color into a 3 digit color code when that is an option.

return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5])

@hosef
Copy link
Author

hosef commented Oct 4, 2024

Is the plan to move the code for manipulating the css into another project so this one will only parse the files?

@oliverklee
Copy link
Contributor

@hosef No, at the moment we have no plans in that direction. The only similar thing that was decided was deprecating and removing the expansion of shorthand properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants