Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass: $color-darken with HSL color codes

Tags:

css

sass

colors

I know how to use built-in functions for determining a darker color from a hex code. But how to do this with hsl? I have three colors, one primary, one darker and a lighter one. I need to write a function to calculate the difference between them and get a lighter and darker shade for them. So when I add another color code, it'll give me same percentage for lighter and darker shade.

As I understand from Sass documentation, I first need a function to get hue, saturation, and lightness of my base color, right? But what then?

These are my colors in hex:

$base: #7760BF;
$base-darker: #483584;
$base-lighter: #b5a9dc;

And here they are in hsl, just in case:

$base: hsl(255, 43%, 56%);
$base-darker: hsl(254, 43%, 36%);
$base-lighter: hsl(254, 42%, 76%);

Can someone help me out?

like image 847
hemoglobin Avatar asked Jun 22 '17 15:06

hemoglobin


People also ask

What is the HSL color code?

HSL stands for hue, saturation, and lightness. It's based on the RGB color wheel. Each color has an angle and a percentage value for the saturation and lightness values.

How do you make a color darker in CSS?

The brightness() CSS function applies a linear multiplier to the input image, making it appear brighter or darker.

How do I add color to opacity in SCSS?

Add an Alpha Value to a Color Variable With Sass, just wrap your color with rgba() and the opacity value as you would with normal CSS. Sass will take care of breaking down the color value into the correct RGB values.

How do I change colors in HSL?

Use the Paint Brush Tool and a brush coloured white to paint the HSL adjustment back into the image, where you've previously erased it. Invert the HSL Adjustment to paint the colour changes into the image, instead of erasing.


1 Answers

In Sass, types are converted automatically. Color functions from the Sass standard library will accept any color, rgb or hsl.

At first to generate darker and lighter colors, you can use darken and lighten. I made an example here.

enter image description here

Then to know the lightness difference between colors to generate new colors with the same difference, you can use lightness.

@function get-lightness-diff($base, $color) {
    @return lightness($base) - lightness($color);
}

@function get-variant($color, $diff) {
    @if ($diff < 0) {
        @return darken($color, -$diff);
    } @else {
        @return lighten($color, $diff);
    }
}

@function get-variants($color, $diff) {
    $ret: ();
    $ret: map-merge($ret, ( darker: get-variant($color, -$diff) ));
    $ret: map-merge($ret, ( lighter: get-variant($color, $diff) ));
    @return $ret;
}

@function get-variants-from-set($color, $darker, $base, $lighter) {
    $darker-diff: get-lightness-diff($base, $darker);
    $lighter-diff: get-lightness-diff($base, $lighter);

    $ret: ();
    $ret: map-merge($ret, ( darker: get-variant($color, $darker-diff) ));
    $ret: map-merge($ret, ( lighter: get-variant($color, $lighter-diff) ));
    @return $ret;
}

$base: hsl(255, 43%, 56%);
$base-lighter: hsl(255, 43%, 66%);
$base-darker: hsl(255, 43%, 46%);

// Get a lightness diff, from the light color for example
$diff: get-lightness-diff($base, $base-lighter);

// Variants: contains a map with 'darker' and 'lighter' colors.
$variants: get-variants($base, $diff);
// Or create your lighter/darker variants manually:
$darker: get-variant($base, -10%);
$lighter: get-variant($base, 10%);

// Or all-in-one: create your variants from an other set of colors:
$other-base: hsl(255, 33%, 33%);
$other-variants: get-variants-from-set($other-base, $base-darker, $base, $base-lighter);

I made an other example here, but you may have to adapt it to make it fit your needs.

Variants


Edit (11/07/17): Since I written this post, I improved the utilities we can use to generate colors variants. You can see an example at: https://codepen.io/ncoden/pen/yXQqpz?editors=1100.

like image 67
ncoden Avatar answered Oct 02 '22 06:10

ncoden