TExcelTAD
Home › Sass › Sass Colors

Sass Colors

Sass understands colours as real values, not just text. It can lighten, darken, mix, fade and inspect them at compile time, so a whole palette can grow from one or two brand colours. The modern tools live in the sass:color module.

Three core functions

Function How it changes the colour Example
color.adjust($c, ...) Adds or subtracts a fixed amount $lightness: 10% → +10 points
color.scale($c, ...) Moves a percentage of the way to the limit $lightness: 50% → halfway to white
color.change($c, ...) Sets a channel to a value $alpha: 0.5

Each one takes keyword arguments for the channels: $red, $green, $blue, $hue, $saturation, $lightness, $whiteness, $blackness and $alpha.

scss · try it yourself
@use "sass:color";

$brand: #cc6699;

.swatches {
  --base: #{$brand};
  --adjust-dark: #{color.adjust($brand, $lightness: -15%)};
  --scale-light: #{color.scale($brand, $lightness: 60%)};
  --faded: #{color.change($brand, $alpha: 0.4)};
  --rotated: #{color.adjust($brand, $hue: 120deg)};
}
.swatches {
  --base: #cc6699;
  --adjust-dark: rgb(67.5%, 22.5%, 45%);
  --scale-light: rgb(92%, 76%, 84%);
  --faded: rgba(204, 102, 153, 0.4);
  --rotated: #99cc66;
}

Note: When a computed colour has fractional channel values, Dart Sass writes it with percentages, for example rgb(90%, 70%, 80%), so no precision is lost. Browsers read it like any other rgb() colour.

adjust vs scale

color.adjust adds the same amount no matter where you start, so a colour that's already light can be pushed past 100% lightness. color.scale moves proportionally, so it never overshoots — which is why it's usually the better choice for tints and shades:

scss · try it yourself
@use "sass:color";

$light: #f8bbd0;

.compare {
  border-top-color: color.adjust($light, $lightness: 20%);    // +20 points: overshoots
  border-bottom-color: color.scale($light, $lightness: 20%);  // 20% of the remaining distance
}
.compare {
  border-top-color: hsl(339.3442622951, 81.3333333333%, 105.2941176471%);
  border-bottom-color: rgb(97.8039215686%, 78.6666666667%, 85.2549019608%);
}

Building a palette

scss · try it yourself
@use "sass:color";

$brand: #cc6699;
$steps: (100: 80%, 300: 40%, 500: 0%, 700: -30%, 900: -60%);

@each $step, $amount in $steps {
  .bg-brand-#{$step} {
    background: color.scale($brand, $lightness: $amount);
  }
}
.bg-brand-100 {
  background: rgb(96%, 88%, 92%);
}

.bg-brand-300 {
  background: rgb(88%, 64%, 76%);
}

.bg-brand-500 {
  background: #cc6699;
}

.bg-brand-700 {
  background: rgb(63%, 21%, 42%);
}

.bg-brand-900 {
  background: rgb(36%, 12%, 24%);
}

Mixing and other helpers

scss · try it yourself
@use "sass:color";

$brand: #cc6699;

.helpers {
  --mix: #{color.mix(#3366cc, #ffcc00, 50%)};
  --complement: #{color.complement($brand)};
  --gray: #{color.grayscale($brand)};
  --red-channel: #{color.channel($brand, "red", $space: rgb)};
  --hue: #{color.channel($brand, "hue", $space: hsl)};
}
.helpers {
  --mix: #999966;
  --complement: #66cc99;
  --gray: #999999;
  --red-channel: 204;
  --hue: 330deg;
}

Goodbye darken() and lighten()

Older tutorials use global functions like darken($c, 10%), lighten(), saturate() and fade-out(). These still compile, but they're deprecated — both because global built-in functions are being removed in favour of modules, and because they behave unintuitively. Watch the warnings:

scss · try it yourself
$brand: #cc6699;

.old-way {
  background: darken($brand, 10%);
}
.old-way {
  background: rgb(75%, 25%, 50%);
}

Deprecation warning (line 4): Global built-in functions are deprecated and will be removed in Dart Sass 3.0.0.

Deprecation warning (line 4): darken() is deprecated. Suggestions:
Deprecated Modern replacement
darken($c, 10%) color.adjust($c, $lightness: -10%) or color.scale($c, $lightness: -15%)
lighten($c, 10%) color.adjust($c, $lightness: 10%)
transparentize($c, 0.3) / fade-out color.adjust($c, $alpha: -0.3)
mix($a, $b) color.mix($a, $b)
red($c) color.channel($c, "red", $space: rgb)

Tip: For colours that must change at runtime (themes, dark mode), output them as CSS custom properties, or let the browser compute them with CSS color-mix(in srgb, var(--brand) 80%, black).

Test yourself

Which function moves a colour a percentage of the way toward white without overshooting?

  1. color.adjust($c, $lightness: 20%)
  2. color.scale($c, $lightness: 20%)
  3. color.change($c, $lightness: 20%)
  4. lighten($c, 20%)

Which call makes a colour 50% transparent regardless of its current alpha?

  1. color.adjust($c, $alpha: -0.5)
  2. color.scale($c, $alpha: 50%)
  3. color.change($c, $alpha: 0.5)
  4. color.mix($c, transparent)

What's the modern replacement for darken($c, 10%)?

  1. color.darken($c, 10%)
  2. color.adjust($c, $lightness: -10%)
  3. math.darken($c, 10%)
  4. $c - 10%