Last active
July 25, 2024 03:37
-
-
Save certainlyakey/e9c0d8f5c87ff47e3d5b to your computer and use it in GitHub Desktop.
URL-encode color SASS function / convert color to hex SASS function
This file contains 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
//does not work with colors containing alpha | |
@function encodecolor($string) { | |
@if type-of($string) == 'color' { | |
$hex: str-slice(ie-hex-str($string), 4); | |
$string:unquote("#{$hex}"); | |
} | |
$string: '%23' + $string; | |
@return $string; | |
} |
I had the same problem as jacobdubail. Thanks!
This wasn't working properly for me. I have forked it and added a couple of fixes.
Exactly what I was looking for, thanks!
Awesome. Thank you.
Thanks so much! I also had the same problem as jacobdubail.
👍
Thanks, that's very useful 👍
does not work with colors containing alpha
Here's a little improvement, accounting just for that — makes it more convenient to use variables:
@function encodecolor($string) {
@if type-of($string) == 'color' and str-index(#{$string}, '#') == 1 {
$hex: str-slice(ie-hex-str($string), 4);
$string: unquote('#{$hex}');
@return '%23' + $string;
}
@return $string;
}
// TESTS
$test-key-color: cyan;
$test-hex-color: #1ef;
$test-hsl-color: hsl(184, 100%, 53%);
$test-rgb-color: #11eeff;
$test-rgba-color: rgba(17, 238, 255, 0.69);
@debug encodecolor($test-key-color); // cyan
@debug encodecolor($test-hex-color); // %2311EEFF
@debug encodecolor($test-hsl-color); // %230FEFFF
@debug encodecolor($test-rgb-color); // %2311EEFF
@debug encodecolor($test-rgba-color); // rgba(17, 238, 255, 0.69)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just saved me so much work. fixed an issue in firefox where the svg wouldn't appear as a background.