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

[Global functions] add hex functions to convert RGB / RGBA to #RRGGBB / #RRGGBBAA #2987

Closed
axdyng opened this issue Jan 6, 2021 · 5 comments

Comments

@axdyng
Copy link

axdyng commented Jan 6, 2021

Currently, Sass has HSL and RGB helper functions. However, it is not possible to convert vice versa? (Or is it, but not documented?)

I think it would be really helpful to developers for this to come out of the box. It's a pain to switch from a #hex notation to rgba() notation just for adding opacity to a color.

@axdyng axdyng changed the title [Global functions] hex function to convert RGB / RGBA to #RRGGBB / #RRGGBBAA [Global functions] add hex functions to convert RGB / RGBA to #RRGGBB / #RRGGBBAA Jan 6, 2021
@stof
Copy link
Contributor

stof commented Jan 6, 2021

The sass rgb/rgba functions supports a signature with 2 arguments: rgb($color, $alpha) which already solves this need IMO (as you can use your existing hex color for the first argument): https://sass-lang.com/documentation/modules#rgb

@axdyng
Copy link
Author

axdyng commented Jan 6, 2021

What I meant is to convert rgb to hexadecimal format

For example:

  • hex(rgb(255, 255, 255)) compiles to #FFFFFF / white
  • hex(rgba(0, 0, 0, 0.5) compiles to #000000CC (#RRGGBBAA format)

@stof
Copy link
Contributor

stof commented Jan 6, 2021

The rendering of sass values in the output is not configurable. the implementation deals with it on its own (as they are equivalent in CSS anyway):

  • Sass never uses the #RRGGBBAA format (as it has lower browser support).
  • In compressed style, Sass automatically chooses the shorter format for opaque colors in the output (considering the #RGB format, the #RRGGBB format and named colors)
  • if the color has an original form (i.e. it was parsed from the scss source rather than being the result of some computation), it is rendered this way
  • in expanded style, Sass will use named color if possible or the #RRGGBB format for opaque colors
  • Semi-transparent colors are rendered as rgba(...)

Note that this corresponds to the rendering behavior of dart-sass. libsass might have some differences.

@stof
Copy link
Contributor

stof commented Jan 6, 2021

Btw, to implement a hex function which could force the behavior you describe, it would have to be a function which takes a color as argument and return an unquoted string (if it returns a sass color, it cannot control its formatting and would be a no-op). Such function would be confusing in the Sass language IMO.

@nex3
Copy link
Contributor

nex3 commented Jan 6, 2021

I'll cosign @stof's answer. If you want your CSS output to use specific color formats, I recommend using a post-processor like PostCSS to convert all colors to that format after Sass has run.

@nex3 nex3 closed this as completed Jan 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants