Skip to content

Instantly share code, notes, and snippets.

@nicksteffens
Created May 13, 2015 18:50
Show Gist options
  • Save nicksteffens/0e02b623d097636a7a3a to your computer and use it in GitHub Desktop.
Save nicksteffens/0e02b623d097636a7a3a to your computer and use it in GitHub Desktop.
Color mixin for name gen
// ............................................................
// .for
.for(@i, @n) {.-each(@i)}
.for(@n) when (isnumber(@n)) {.for(1, @n)}
.for(@i, @n) when not (@i = @n) {
.for((@i + (@n - @i) / abs(@n - @i)), @n);
}
// ............................................................
// .for-each
.for(@array) when (default()) {.for-impl_(length(@array))}
.for-impl_(@i) when (@i > 1) {.for-impl_((@i - 1))}
.for-impl_(@i) when (@i > 0) {.-each(extract(@array, @i))}
// mixins
@colors: "teak", "navy-leather", "natural-leather", "ebony", "cognac-leather", "spice", "blue", "bamboo", "black-leather", "olive", "royal-blue", "purple", "walnut", "cabernet", "slate", "chalk", "smoke", "crimson", "dark-teal", "violet", "navy", "raspberry", "red", "turquoise", "black";
@swatchWidth: 95px;
@swatchHeight: 95px;
@swatchMargin: 12px;
.bg-pos(@t, @l) {
background-position: @l @t;
}
.bg-position(@r, @c, @h, @w, @m) {
&when not (@r > 0) and not (@c > 0) {
.bg-pos(0, 0);
}
// get row
&when (@r > 0) and (@c > 0) {
.bg-pos( -(@r * (@h + @m)), -(@c * (@w + @m)));
}
// first row
&when not (@r > 0) and (@c > 0) {
.bg-pos(0, -(@c * (@w + @m)));
}
&when (@r > 0) and not (@c > 0) {
.bg-pos(-(@r * (@h + @m)), 0);
}
}
.colors {
.swatch {
display: block;
position: relative;
width: @swatchWidth;
height: @swatchHeight;
// sprite sheet
background: url(../images/sprite-colors.png) 0 0 no-repeat;
// sprite sheet
.for(@colors); .-each(@color) {
@name: e(@color);
@row: ceil((@i / 5) - 1);
@col: mod((@i - 1), 5);
&.@{name} {
.bg-position(@row, @col, @swatchHeight, @swatchWidth, @swatchMargin);
}
}
&:hover {
cursor: pointer;
&:after {
display: block;
}
}
&:after {
display: none;
content: " ";
position: absolute;
width: (@swatchWidth + 2);
height: (@swatchHeight + 2);
top: 50%;
left: 50%;
margin: -((@swatchWidth + 2) / 2);
background: none;
border: 8px solid @c-white;
border-radius: @swatchHeight;
}
}
}
@media screen and (max-width:480px), (max-height:780px) {
.colors {
.swatch {
width: 50px;
height: 50px;
background-size: 100%;
// individual images
.for(@colors); .-each(@color) {
@name: e(@color);
@image: "../images/colors/@{name}.png";
&.@{name} {
background-image: url("@{image}");
background-position: 0 0;
}
}
&:hover {
&:after {
display: none;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment