I'm trying to make a horizontal rainbow stripe background gradient mixin, but I feel like this is way too verbose. How can it be better?
Goals:
- [check] Use variables for colors so they can be swapped out for different colors.
- [check] The widths are hard coded for 8 colors. Can it be done smarter where it adjusts to the number of colors you add? Or is that asking too much?
- [check] The colors are defined twice for the color starts and stops. Can this be done better?
- [see below] Right now I define the colors as variables at the top level, then pass them in the mixin. Should they instead be created inside the mixin and then colors are brought in as arguments? Or does that matter?
// You could set individual variables for each color as well.
// You would still pass them all as a single argument,
// or join them into a single variable before passing, as you see fit.
$mycolors: red orange yellow green blue indigo violet;
// Returns a striped gradient for use anywhere gradients are accepted.
// - $position: the starting position or angle of the gradient.
// - $colors: a list of all the colors to be used.
@function rainbow($position, $colors) {
$colors: if(type-of($colors) != 'list', compact($colors), $colors);
$gradient: compact();
$width: 100% / length($colors);
@for $i from 1 through length($colors) {
$pop: nth($colors,$i);
$new: $pop ($width * ($i - 1)), $pop ($width * $i);
$gradient: join($gradient, $new, comma);
}
@return linear-gradient($position, $gradient);
}
.rainbow {
@include background-image(rainbow(left, $colors));
}
Apologies, I don't mean to turn this into a support thread. But I was hoping you (or someone reading along) could help me. :)
Firstly, is this Sass 3.2 specific? I'm using Middleman (just to rapidly prototype) and last I checked, it doesn't use 3.2 yet (correct me if I'm wrong), so I'm wondering if that's the issue. I might just need to go ahead and get this site moved out into a regular Rails app.
If that's not the issue, any guidance toward what I did wrong would be greatly appreciated. :-) The only change I made was changing "rainbow" to "pop-stripe" — which is what we're calling our stripe thingy), switching to the original Sass syntax (which is preferred at work), and British spellings (another preference with the guys I work with). :-)
Variable
Function
Usage
The Error I'm Getting
I tried adding parentheses around it, but that didn't seem to work. I had a Ruby dev here with me look at it, too, and that didn't help either. :-P Does anyone have any ideas why I'm getting argument errors?