Last active
August 1, 2025 11:57
-
-
Save Maxiviper117/095c754e5abefe3d82cbbe6e5a9f8dfe to your computer and use it in GitHub Desktop.
SCSS Color Shades Generator: Dynamic CSS Variable Creation for Theming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Import the Sass math module for division and other mathematical operations | |
| @use "sass:math"; | |
| // Function to generate a series of color shades from a base color | |
| @function generate-shades($base-color) { | |
| // Initialize an empty map to store the shades | |
| $shades: (); | |
| // Define the scale for the shades | |
| $scale: (50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950); | |
| // Iterate over each value in the scale to generate shades | |
| @each $value in $scale { | |
| // Determine if the shade should be lighter or darker than the base color | |
| $is-lighter: $value < 500; | |
| // Calculate the percentage to mix with white or black | |
| $percentage: if($is-lighter, 1 - math.div($value, 1000), math.div($value - 500, 500)); | |
| // Choose white for lighter shades, black for darker | |
| $mix-color: if($is-lighter, #ffffff, #000000); | |
| // Generate the shade by mixing the base color with white or black | |
| $shade: mix($mix-color, $base-color, $percentage * 100%); | |
| // Add the generated shade to the map | |
| $shades: map-merge( | |
| $shades, | |
| ( | |
| $value: $shade | |
| ) | |
| ); | |
| } | |
| // Return the map of generated shades | |
| @return $shades; | |
| } | |
| // Mixin to create CSS variables for the color shades | |
| @mixin generate-color-vars($base-name, $base-color) { | |
| // Generate shades using the base color | |
| $shades: generate-shades($base-color); | |
| // Define CSS variables within the :root selector for global access | |
| :root { | |
| // Iterate over each shade to create a CSS variable | |
| @each $shade, $color in $shades { | |
| // The variable name includes the base name and the shade value | |
| --#{$base-name}-#{$shade}: #{$color}; | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SCSS Color Shades Generator: Dynamic CSS Variable Creation for Theming
The SCSS Color Shades Generator is a powerful tool designed to help developers generate a comprehensive set of color shades from a base color. These shades are then converted into CSS variables, making them easily accessible throughout your project for consistent theming.
Features
How to Use
Step 1: Setup
Ensure you have SCSS set up in your project. This tool requires the use of the
sass:mathmodule for calculations.Step 2: Include the Functions and Mixins
Add the following SCSS code to a file in your project, for example,
functions.scss. This file contains the necessary function and mixin for generating color shades and corresponding CSS variables.Step 3: Generating Your Color Shades
To use the color shades generator, you'll need to include the
functions.scssin your main SCSS file and call the mixin with your desired base color and name.Example usage in your
global.scss:This will generate CSS variables for color shades named
--primary-50,--primary-100, ...,--primary-950, which you can use anywhere in your CSS.Step 4: Apply the Colors
Now that your color shades are defined as CSS variables, you can use them in your styles like so:
Customization
You can generate multiple color palettes by calling the mixin with different base colors and names. This allows for extensive customization and theming options across your application.
By following these steps, you can integrate the SCSS Color Shades Generator into your project, enhancing your theming capabilities with dynamically generated color shades.
This Markdown guide provides a comprehensive overview of how to integrate and use the SCSS Color Shades Generator in a project, from setup to application, including examples and customization options.