Last active
May 16, 2022 13:05
-
-
Save LeaVerou/9199277 to your computer and use it in GitHub Desktop.
Polyfill gray() from CSS Color Level 4 with SASS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@function gray($intensity, $alpha: 1) { | |
@return rgba($intensity, $intensity, $intensity, $alpha); | |
} | |
/* Thanks Chris Eppstein for simplifying my code! */ | |
/* Testing our new function */ | |
body { | |
background: gray(50%); | |
background: gray(255, .2); | |
} | |
/* Test should output something like: */ | |
body { | |
background: #7f7f7f; | |
background: rgba(255, 255, 255, 0.2); } |
This can be simplified to:
@function gray($intensity, $alpha: 1) {
@return rgba($intensity, $intensity, $intensity, $alpha);
}
Which will return a Sass color resulting in:
body {
background: #7f7f7f;
background: rgba(255, 255, 255, 0.2); }
Thanks Chris!! Gist updated!
Like prisma color markers! I'll take it.
Man I've been defining a grey
function in all my projects for three years. Now that it's gray
I'm screwed :D
The Stylus version of this is almost identical:
gray(intensity, alpha = 1)
rgba(intensity, intensity, intensity, alpha)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like the more mentally pleasing way of calling out gray in percentages. Fancy. :D