ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

SCSS Color Palette Automation with HSL

SCSS

Goal -- WPM

Ready
Exercise Algorithm Area
1/*
2* Generates a color palette from a base HSL color.
3* @param {Number} $hue - The hue value (0-360).
4* @param {Number} $saturation - The base saturation value (0-100%).
5* @param {Number} $lightness - The base lightness value (0-100%).
6* @param {Number} $steps - The number of shades/tints to generate.
7*/
8@mixin generate-hsl-palette($hue, $saturation, $lightness, $steps: 5) {
9@if not (0 <= $hue <= 360) {
10@warn "Hue must be between 0 and 360.";
11@return;
12}
13@if not (0 <= $saturation <= 100) {
14@warn "Saturation must be between 0 and 100%.";
15@return;
16}
17@if not (0 <= $lightness <= 100) {
18@warn "Lightness must be between 0 and 100%.";
19@return;
20}
21@if $steps < 1 {
22@warn "Steps must be at least 1.";
23@return;
24}
25
26$lightness-step: 100% / ($steps - 1);
27$saturation-step: 50% / ($steps - 1); // Adjust saturation more moderately
28
29@for $i from 0 to ($steps - 1) {
30$current-lightness: $lightness + ($i * $lightness-step) - (($steps - 1) / 2 * $lightness-step);
31$current-saturation: $saturation + ($i * $saturation-step) - (($steps - 1) / 2 * $saturation-step);
32
33// Clamp values to valid HSL ranges
34$current-lightness: max(0%, min(100%, $current-lightness));
35$current-saturation: max(0%, min(100%, $current-saturation));
36
37$color: hsl($hue, $current-saturation, $current-lightness);
38$variable-name: "palette-color-#{$i+1}";
39#{'#' + $variable-name}: $color;
40}
41}
42
43// Example usage:
44@include generate-hsl-palette(210, 70%, 50%, 7);
Algorithm description viewbox

SCSS Color Palette Automation with HSL

Algorithm description:

This SCSS mixin automates the creation of a color palette using HSL values. It takes a base hue, saturation, and lightness, and generates a specified number of steps by adjusting lightness and saturation. This allows for the creation of harmonious color schemes, including shades and tints, with built-in validation for HSL ranges and step counts. It's perfect for generating consistent color systems for UI elements.

Algorithm explanation:

The `generate-hsl-palette` mixin first performs input validation to ensure hue, saturation, and lightness are within their valid ranges (0-360 for hue, 0-100% for saturation/lightness), and that the number of steps is at least 1. It then calculates `lightness-step` and `saturation-step` to distribute variations across the generated colors. The `@for` loop iterates to create each color. It calculates `$current-lightness` and `$current-saturation` by adjusting from the base values, centering the distribution around the base lightness. Values are clamped to ensure they remain within valid HSL ranges (0-100%). Finally, it generates SCSS variables like `$palette-color-1`, `$palette-color-2`, etc. The time complexity is O(N) where N is the number of steps. Space complexity is O(N) for the generated variables.

Pseudocode:

mixin generate_hsl_palette(hue, saturation, lightness, steps = 5):
  if hue is not between 0 and 360:
    warn "Hue must be between 0 and 360."
    return
  if saturation is not between 0% and 100%:
    warn "Saturation must be between 0 and 100%."
    return
  if lightness is not between 0% and 100%:
    warn "Lightness must be between 0 and 100%."
    return
  if steps is less than 1:
    warn "Steps must be at least 1."
    return

  lightness_step = 100% / (steps - 1)
  saturation_step = 50% / (steps - 1)

  for i from 0 to steps - 1:
    current_lightness = lightness + (i * lightness_step) - ((steps - 1) / 2 * lightness_step)
    current_saturation = saturation + (i * saturation_step) - ((steps - 1) / 2 * saturation_step)

    current_lightness = clamp(0%, 100%, current_lightness)
    current_saturation = clamp(0%, 100%, current_saturation)

    color = hsl(hue, current_saturation, current_lightness)
    variable_name = "palette-color-" + (i + 1)
    set variable variable_name to color