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).
Not Palegen, no. But Palegen uses go-colorful to manipulate colours and that might be helpful for what you are trying to do. But I have no idea how you would call an external library.
Sass (as you have) or PostCSS seem better suited to the task and are already working in Hugo.
I have no idea about the performance implications, but I find converting to rgb values quite easy in Hugo using the int function and some printf/substr fidgeting:
If this were a common need (which it isn’t) we’d probably want to implement a few functions in a colors namespace (e.g., colors.ToRGB, colors.ToRGBA, colors.IsLight, colors.IsDark, etc.).