The speed is almost the same, but it’s cleaner and works for colors with alpha like #ffffffff
But using a map in the conversion improved the performance.
Here is the updated version and thanks for the suggestion!
Yes, the function hexDecode would help! So +1 for that.
But I’m wondering if a more specialized function, related to colors - would also be interesting.
My use case is very specific, I’m not sure if this would create unnecessary noise.
Let me explain my use case:
I’m using Bootstrap in my theme. An editor can change the Bootstrap primary color variable in a Data file (theme.yaml).
Some sections of my website use the primary or secondary color as background. I want to know if it’s safe to use a btn-primary, btn-secondary or btn-danger in a specific section. If not, I want to decide which class would be better to use as a fallback, btn-light or btn-dark.
The logic is the same of this Bootstrap SCSS function:
Adding color-specific template functions is a bit of a stretch. Let’s stick with hex encoding enhancements for now.
I have a working implementation in a dev branch, and I’m seeing about a 10x improvement in speed versus your partial above. I’ll try to get a PR submitted in the few days.
PS - Your partial has a bug. Use (eq $mod 0) instead of (eq $mod 1).