Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 39 ยท Mixins and Functions

Sass Color Functions: adjust, scale, mix and More

Generate palettes with the sass:color module: adjust, scale, change, mix, complement and channel, plus how the old darken() and lighten() functions map to them.

Hover states, disabled states, light backgrounds and borders are usually variations of a base color. Instead of picking each shade by hand in a design tool and pasting the hex codes, Sass lets you compute them from one brand color. The functions live in the built-in sass:color module. Change the base color and the whole palette follows.

SCSS
@use "sass:color";

After that line, functions are called with the color. prefix: color.adjust(...), color.mix(...).

Start with HSL

Colors can be written as hex, rgb(), hsl(), named colors, and newer spaces such as oklch(). For palettes, HSL (hue, saturation, lightness) is easiest to reason about: change lightness to get a lighter or darker shade of the same color. It also gives the neatest compiled output. When Sass changes a color it prints it in the format the color started in, and an hsl input stays as hsl(262, 83%, 48%), while a hex input that is adjusted prints as rgb() with percentages.

color.adjust: add or subtract

color.adjust($color, $channel: amount) changes a channel by a fixed amount.

SCSS
$brand: hsl(262 83% 58%);

.button {
  background: $brand;
  &:hover  { background: color.adjust($brand, $lightness: -10%); }   // darker
  &:active { background: color.adjust($brand, $lightness: -18%); }
}

You can adjust $hue (degrees), $saturation, $lightness, $whiteness, $blackness, $red, $green, $blue and $alpha. Different spaces can be mixed only if you name one with $space.

color.scale: move a percentage of the way

color.scale moves a channel a fraction of the remaining distance toward its maximum (positive percentage) or minimum (negative percentage). It never overshoots, so it works consistently on both pale and vivid colors.

SCSS
$light-tint: color.scale($brand, $lightness: 85%);   // 85% of the way to white
$deep-shade: color.scale($brand, $lightness: -40%);  // 40% of the way to black

Choose adjust when you want exact steps and scale when you want predictable proportional results.

color.change: set a channel directly

color.change replaces a channel with a given value. It is the way to set transparency:

SCSS
$overlay: color.change(#000, $alpha: 0.6);      // rgba(0, 0, 0, 0.6)
$accent-bg: color.change($brand, $lightness: 95%);

color.mix: blend two colors

color.mix($c1, $c2, $weight) blends two colors. The weight is how much of the first color to use (default 50%).

SCSS
$tint: color.mix(white, $brand, 20%);    // 20% white, 80% brand
$shade: color.mix(black, $brand, 20%);

Mixing with white or black is the classic way to make tints and shades, popularized by frameworks such as Bootstrap.

Build a palette from one color

Reading a channel: color.channel

color.channel($color, "name", $space: ...) returns a single channel value. It replaces the older color.red(), color.hue(), color.lightness() and friends, which are deprecated:

SCSS
color.channel(#6d28d9, "red", $space: rgb);          // 109
color.channel(hsl(262 83% 58%), "hue", $space: hsl);  // 262deg
color.channel($brand, "lightness", $space: hsl);      // 58%

Reading channels is how you build logic such as the contrast picker from Custom functions.

Other handy functions

Function What it does
color.complement($c) Hue rotated 180 degrees
color.invert($c) Inverts the color
color.grayscale($c) Removes saturation
color.to-space($c, hsl) Converts to another color space
color.is-legacy($c) Checks whether it is an rgb, hsl or hwb color
color.same($a, $b) Compares colors across spaces

Old functions you will still see

Older tutorials and codebases use global functions with no module prefix:

Legacy (deprecated) Modern replacement
darken($c, 10%) color.adjust($c, $lightness: -10%) (or color.scale)
lighten($c, 10%) color.adjust($c, $lightness: 10%)
saturate($c, 10%) color.adjust($c, $saturation: 10%)
desaturate($c, 10%) color.adjust($c, $saturation: -10%)
adjust-hue($c, 30deg) color.adjust($c, $hue: 30deg)
opacify, transparentize color.adjust($c, $alpha: ยฑn)
red($c), hue($c), lightness($c) color.channel($c, "red", $space: rgb) and so on
mix($a, $b, 50%) color.mix($a, $b, 50%)

They still compile today but emit deprecation warnings. Migrate them with the sass-migrator module tool.

Results print in the color's starting format. A hex color adjusted by lightness therefore compiles to rgb() with long percentages, which browsers read exactly; write the base in hsl() when you want readable CSS.

Tints and shades with @each

A loop makes a complete scale in a few lines (loops are explained in Each, for and while):

A tint scale with @each

Note the output format: mixed colors print as rgb() with percentages, which browsers handle perfectly. If you need hex for a legacy reason, run a tool such as PostCSS or Lightning CSS after Sass.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which call darkens $brand by exactly 10 lightness points?

    color.adjust changes a channel by a fixed amount. darken() is the deprecated global equivalent.
  2. 2. What does color.scale($c, $lightness: 50%) do?

    scale uses a percentage of the remaining distance to the channel maximum (or minimum if negative).
  3. 3. How do you make a 40% transparent black?

    color.change sets a channel to an explicit value, here the alpha channel.
  4. 4. Which function replaces the deprecated lightness($c)?

    color.channel reads any channel in any color space.

Summary

  • Load the module with @use "sass:color" and call functions as color.adjust() etc.
  • adjust changes a channel by a fixed amount, scale by a proportion, change sets it, mix blends two colors.
  • color.channel reads a channel; complement, invert and grayscale handle simple transforms.
  • Define base colors in hsl() for readable output; Sass prints results in the starting format.
  • Legacy functions (darken, lighten, red...) are deprecated; use the table above to migrate.

Next: Built-in math, string and list functions