[Answered ]-A linear gradient with variable number of stops in LESS? (or Sass)

2👍

My own solution, using a modified file from bourbon project:

_linear-gradient.scss

@mixin linear-gradient($pos, $G1, $G2: false,
                        $G3: false, $G4: false,
                        $G5: false, $G6: false,
                        $G7: false, $G8: false,
                        $G9: false, $G10: false) {
    // Detect what type of value exists in $pos
    $pos-type: type-of(nth($pos, 1));

    // If $pos is missing from mixin, reassign vars and add default position
    @if ($pos-type == color) or (nth($pos, 1) == "transparent")    {
        $G10: $G9; $G9: $G8; $G8: $G7; $G7: $G6; $G6: $G5;
         $G5: $G4; $G4: $G3; $G3: $G2; $G2: $G1; $G1: $pos;
        $pos: top; // Default position
    }

    $usual:($G1);
    $webkit: color-stop($G1);
    @each $g in $G2, $G3, $G4, $G5, $G6, $G7, $G8, $G9, $G10 {
        @if $g != false {
            $usual: $usual + ',' + $g;
            $webkit: $webkit + ',' + color-stop($g);
        }
    }
    $usual: unquote($usual);
    $webkit: unquote($webkit);

    background-color: nth($G1, 1);
    background: deprecated-webkit-gradient(linear, $usual); // Safari <= 5.0
    background: -webkit-gradient(linear, $pos, $webkit); // Safari 5.1+, Chrome
    background: -webkit-linear-gradient($pos, $usual); // Safari 5.1+, Chrome
    background: -moz-linear-gradient($pos, $usual);
    background: -ms-linear-gradient($pos, $usual);
    background: -o-linear-gradient($pos, $usual);
    background: linear-gradient($pos, $usual);
}

usage (screen.sass):

@import linear-gradient
button.action
    +linear-gradient(top, #6db3f2 0%, #54a3ee 50%, #3690f0 51%, #1e69de 100%)

How I configured Sass with django-compressor:

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#   'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'compressor.finders.CompressorFinder',
)

INSTALLED_APPS = (
    ...
    'compressor',
)

COMPRESS = True
COMPRESS_PRECOMPILERS = (
    ('text/sass', 'sass {infile} {outfile}'),
)
COMPILER_FORMATS = {
    '.sass': {
        'binary_path': 'sass',
        'arguments': '*.sass *.css'},
}

a template:

{% load compress %}
{% compress css %}
<link href="{{ STATIC_URL }}css/screen.sass" rel="stylesheet" type="text/sass" media="screen,projection"/>
{% endcompress %}

Leave a comment