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

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

Sass Theme Color Palette Generator

SASS

Goal -- WPM

Ready
Exercise Algorithm Area
1// Generates a color palette from a base color.
2// Uses Sass's built-in color functions for manipulation.
3
4@mixin generate-palette($base-color, $shades: 5) {
5// Validate the base color input.
6@if type-of($base-color) != color {
7@warn "Invalid base color provided to generate-palette. Expected a color.";
8@return;
9}
10
11// Ensure the number of shades is a positive integer.
12@if type-of($shades) != number or $shades < 1 {
13@warn "Invalid number of shades provided to generate-palette. Expected a positive integer.";
14$shades: 5; // Default to 5 shades if invalid.
15}
16
17$palette: ();
18$step: 100% / ($shades + 1);
19
20// Generate lighter shades.
21@for $i from 1 through $shades {
22$lightness: 100% - ($step * $i);
23$color: lighten($base-color, $lightness);
24$palette: append($palette, $color, "comma");
25}
26
27// Add the base color itself.
28$palette: append($palette, $base-color, "comma");
29
30// Generate darker shades.
31@for $i from 1 through $shades {
32$darkness: $step * $i;
33$color: darken($base-color, $darkness);
34$palette: append($palette, $color, "comma");
35}
36
37// Output the palette as CSS variables.
38@each $color in $palette {
39--color-palette-item: #{$color};
40// In a real scenario, you'd assign these to specific variable names.
41// For this example, we'll just output them.
42#{inspect($color)} {
43background-color: $color;
44display: inline-block;
45width: 20px;
46height: 20px;
47margin: 2px;
48}
49}
50}
51
52// Example usage:
53.theme-primary {
54@include generate-palette(blue, 4);
55}
56
57.theme-secondary {
58@include generate-palette(#ff6347, 3);
59}
60
61.invalid-theme {
62@include generate-palette("not a color");
63}
Algorithm description viewbox

Sass Theme Color Palette Generator

Algorithm description:

This Sass mixin generates a color palette by progressively lightening and darkening a base color. It creates a series of shades that can be used for theming UI elements, such as backgrounds, borders, and text. The primary use case is to establish a consistent and harmonious color scheme across an application with minimal manual color definition.

Algorithm explanation:

The `generate-palette` mixin takes a base color and a number of shades as input. It first validates that the base color is indeed a Sass color type and that the number of shades is a positive integer. If inputs are invalid, it issues warnings and uses defaults. It then calculates a step percentage based on the desired number of shades to ensure even distribution. The mixin iterates to generate lighter shades using `lighten()` and then darker shades using `darken()`, appending each generated color to a list. Finally, it iterates through the complete list of colors and outputs them, demonstrating their usage. The time complexity is O(N) where N is the total number of shades generated, as it involves a fixed number of iterations. Space complexity is also O(N) to store the generated palette list.

Pseudocode:

DEFINE MIXIN generate-palette(base_color, shades = 5)
  IF base_color is NOT a color THEN
    WARN "Invalid base color"
    RETURN
  END IF
  IF shades is NOT a positive integer THEN
    WARN "Invalid number of shades"
    SET shades = 5
  END IF

  INITIALIZE palette LIST
  CALCULATE step = 100% / (shades + 1)

  // Generate lighter shades
  FOR i FROM 1 TO shades DO
    lightness = 100% - (step * i)
    color = lighten(base_color, lightness)
    APPEND color TO palette
  END FOR

  APPEND base_color TO palette

  // Generate darker shades
  FOR i FROM 1 TO shades DO
    darkness = step * i
    color = darken(base_color, darkness)
    APPEND color TO palette
  END FOR

  // Output colors (e.g., as CSS variables or styles)
  FOR EACH color IN palette DO
    OUTPUT color
  END FOR
END MIXIN