Spectre.css
  • Introduction
  • ❤ Benefits
  • General concepts
  • Getting started
    • Skeleton
    • Installation
      • npm
    • Usage
  • Variables
    • Color scheme
    • CSS variable name
    • Using CSS variables
    • Colors
      • Settable SCSS
      • Based on settable SCSS
      • CSS variable color
      • Using CSS variable color
      • Color code
    • Other
  • CSS Color Classes
    • Background colors
    • Label colors
    • ⚠ Text colors
    • ⚠ Toast colors
  • Function
    • Color
      • alpha-var()
      • color()
      • color-name()
      • hsla-color()
      • hue-var()
      • lightness-level-var()
      • lightness-var()
      • saturation-var()
    • Var
      • css-var()
      • get-var()
      • size-var()
      • unit-var()
      • var-adjust()
      • var-name()
      • var-negative()
      • var-unit()
    • get-border()
    • get-from-list()
    • important()
    • map-get-default()
    • strip-unit()
    • typeof()
  • Mixin
    • Border
      • border()
      • border-hover()
      • border-variant()
    • Color
      • background()
      • background-color()
      • bg-color-variant()
      • color-active()
      • color-focus()
      • color-hover()
      • color-picker()
      • color-visited()
      • set-color()
      • set-var-alpha()
      • text-color-variant()
    • Label
      • label-base()
      • label-variant()
      • label-class-variant()
    • Margin
      • margin-size-variants()
      • margin-variant()
    • Padding
      • padding-size-variants()
      • padding-variant()
    • Text
      • text-ellipsis()
    • Toast
      • toast-variant()
      • ⚠ toast-class-variant()
    • Var
      • set-var()
      • set-vars()
      • ⚠ define-color()
      • ⚠ define-color-based-on()
    • avatar-base()
    • box-shadow-side()
    • button-variant()
    • clearfix()
    • control-shadow()
    • disabled()
    • hide-scrollbar()
    • property()
    • shadow-variant()
    • transition()
    • z-index()
  • Change log
    • Keep a changelog
    • CHANGELOG.md
  • GIT
    • Commit
    • Semantic Versioning
  • License
    • MIT
  • Social
    • Gettr
    • Twitter
    • YouTube
  • Contact
    • ⋯ Chat
    • @ Email
    • ✆ Phone
  • Donate
    • ฿ Cryptocurrency
    • $ Fiat
Powered by GitBook
On this page
  1. Variables
  2. Colors

CSS variable color

PreviousBased on settable SCSSNextUsing CSS variable color

Last updated 2 years ago

Structure and defining

Each hex color is in form defined by the mixin, split into four CSS variables, where suffix h indicates hue, s saturation, l lightness and a alpha.

The single CSS color is defined as follows.

--#{$var-prefix}-#{$name}-color-h: hue($color)
--#{$var-prefix}-#{$name}-color-s: saturation($color)
--#{$var-prefix}-#{$name}-color-l: lightness($color)
--#{$var-prefix}-#{$name}-color-a: alpha($color)

The SCSS variable is to make CSS variable unique, $name is the color name and $color is the hex color to obtain hue, saturation, lightness and alpha.

It's possible to customize the CSS variables' names with the SCSS variable , which by default is set to s.

For example, the primary-color is built from the CSS variables based on hex #5755d9.

--s-primary-color-h: 240.9090909091deg; // Hue.
--s-primary-color-s: 63.4615384615%; // Saturation.
--s-primary-color-l: 59.2156862745%; // Lightness.
--s-primary-color-a: 1; // Alpha.

It is core color and it's defined in the file.

@use '../mixins/define-color' as *;
@use '../variables' as *;

:root, :host {
  // Primary.
  @include define-color('primary-color', $primary-color); // #5755d9
  @include define-color-based-on('primary-color-dark', 'primary-color', $lightness: -3%); // darken($primary-color, 3%)
  @include define-color-based-on('primary-color-light', 'primary-color', $lightness: +3%); // lighten($primary-color, 3%)
}
--s-secondary-color-h: var(--s-primary-color-h);
--s-secondary-color-s: var(--s-primary-color-s);
--s-secondary-color-l: calc(var(--s-primary-color-l) + 37.5%);
--s-secondary-color-a: var(--s-primary-color-a);
@use '../mixins/define-color-based-on' as *;

:root, :host {
  // Secondary.
  @include define-color-based-on('secondary-color', 'primary-color', $lightness: +37.5%); // lighten($primary-color, 37.5%) !default;
  @include define-color-based-on('secondary-color-dark', 'secondary-color', $lightness: -3%); // darken($secondary-color, 3%) !default;
  @include define-color-based-on('secondary-color-light', 'secondary-color', $lightness: +3%); // lighten($secondary-color, 3%) !default;
}

Each color that is based on settable color is defined by the mixin , and it's also split into four CSS variables, which values refer by using CSS function to settable colors CSS property names, where suffix h indicates hue, s saturation, l lightness and a alpha.

For example secondary-color that based on primary-color is defined in both and elements and built from the CSS variables as follows.

CSS variables that are based on others are also defined in both and elements, and secondary-color is also in the file.

define-color-based-on()
var()
:root
:host
:root
:host
_core-colors.scss
hsla()
define-color()
_core-colors.scss
$var-prefix
$var-prefix