This built-in module is available from the URL sass:color
.
This algorithm takes a SassScript number number
and a number max
. It returns
a number between 0 and max
.
-
If
number
has units other than%
, throw an error. -
If
number
has the unit%
, setnumber
tonumber * max / 100
, without units. -
Return
number
, clamped between 0 andmax
.
This algorithm takes a number number
, a value factor
, and a number max
.
It's written "scale <number>
by <factor>
with a max
of <max>
". It
returns a number with a value between 0 and max
and the same units as
number
.
Note: in practice, this is only ever called with
number <= max
.
-
If
factor
isn't a number with unit%
between-100%
and100%
(inclusive), throw an error. -
If
factor > 0%
, returnnumber + (max - number) * factor / 100%
. -
Otherwise, return
number + number * factor / 100%
.
adjust($color,
$red: null, $green: null, $blue: null,
$hue: null, $saturation: null, $lightness: null,
$whiteness: null, $blackness: null,
$alpha: null)
This function is also available as a global function named adjust-color()
.
-
If
$color
isn't a color, throw an error. -
Let
alpha
be$color
's alpha channel. -
If
$alpha
isn't null:-
If
$alpha
isn't a number between -1 and 1 (inclusive), throw an error. -
Set
alpha
toalpha + $alpha
clamped between 0 and 1.
-
-
If
$hue
isn't a number or null, throw an error. -
If
$hue
is a number and it has units that aren't compatible withdeg
, throw an error.Unitless numbers are allowed.
-
If any of
$red
,$green
, or$blue
aren't null:-
If any of
$hue
,$saturation
,$lightness
,$whiteness
, or$blackness
aren't null, throw an error. -
If any of
$red
,$green
, or$blue
aren't either null or numbers between -255 and 255 (inclusive), throw an error. -
Let
red
,green
, andblue
be$color
's red, green, and blue channels. -
If
$red
isn't null, setred
tored + $red
clamped between 0 and 255. -
If
$green
isn't null, setgreen
togreen + $green
clamped between 0 and 255. -
If
$blue
isn't null, setblue
toblue + $blue
clamped between 0 and 255. -
Return a color with
red
,green
,blue
, andalpha
as the red, green, blue, and alpha channels, respectively.
-
-
Otherwise, if either
$saturation
or$lightness
aren't null:-
If either
$whiteness
or$blackness
aren't null, throw an error. -
If either
$saturation
or$lightness
aren't either null or numbers with unit%
between -100% and 100% (inclusive), throw an error. -
Let
hue
,saturation
, andlightness
be the result of callinghue($color)
,saturation($color)
, andlightness($color)
respectively. -
If
$hue
isn't null, sethue
tohue + $hue
. -
If
$saturation
isn't null, setsaturation
tosaturation + $saturation
clamped between 0% and 100%. -
If
$lightness
isn't null, setlightness
tolightness + $lightness
clamped between 0% and 100%. -
Return the result of calling
hsl()
withhue
,saturation
,lightness
, andalpha
.
-
-
Otherwise, if either
$hue
,$whiteness
, or$blackness
aren't null:-
If either
$whiteness
or$blackness
aren't either null or numbers with unit%
between-100%
and100%
(inclusive), throw an error. -
Let
hue
,whiteness
, andblackness
be the result of callinghue($color)
,whiteness($color)
, andblackness($color)
respectively. -
If
$hue
isn't null, sethue
tohue + $hue
. -
If
$whiteness
isn't null, setwhiteness
towhiteness + $whiteness
clamped between0%
and100%
. -
If
$blackness
isn't null, setblackness
toblackness + $blackness
clamped between0%
and100%
. -
Return the result of calling
hwb()
withhue
,whiteness
,blackness
, andalpha
.
-
-
Otherwise, return a color with the same red, green, and blue channels as
$color
andalpha
as its alpha channel.
adjust-hue($color, $degrees)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
alpha($color)
This function is also available as a global function named opacity()
.
There is also a global function named
alpha()
, but it supports an additional overload with different behavior that's defined with the global functions.
-
If
$color
is a number and this function is called as the globalopacity()
function, return a plain CSS function string with the name"opacity"
and the argument$color
. -
Otherwise, if
$color
is not a color, throw an error. -
Return the alpha channel of
$color
as a unitless number.
blackness($color)
-
If
$color
is not a color, throw an error. -
Return a number with unit
%
between0%
and100%
(inclusive) such that:-
hwb(hue($color), whiteness($color), blackness($color))
returns a color with the same red, green, and blue channels as$color
. -
whiteness($color) + blackness($color) <= 100%
.
The specific number returned here is left purposefully open-ended to allow implementations to pursue different strategies for representing color values. For example, one implementation may eagerly convert all colors to RGB channels and convert back when
whiteness()
orblackness()
is called, where another may keep around their original HWB values and return those as-is. -
blue($color)
This function is also available as a global function named blue()
.
change($color,
$red: null, $green: null, $blue: null,
$hue: null, $saturation: null, $lightness: null,
$whiteness: null, $blackness: null,
$alpha: null)
This function is also available as a global function named change-color()
.
-
If
$color
isn't a color, throw an error. -
If
$alpha
isn't either null or a number between 0 and 1 (inclusive), throw an error. -
Let
alpha
be$color
's alpha channel if$alpha
is null or$alpha
without units otherwise. -
If
$hue
isn't a number or null, throw an error. -
If any of
$red
,$green
, or$blue
aren't null:-
If any of
$hue
,$saturation
,$lightness
,$whiteness
, or$blackness
aren't null, throw an error. -
If any of
$red
,$green
, or$blue
aren't either null or numbers between 0 and 255 (inclusive), throw an error. -
Let
red
be$color
's red channel if$red
is null or$red
without units otherwise. -
Let
green
be$color
's green channel if$green
is null or$green
without units otherwise. -
Let
blue
be$color
's blue channel if$blue
is null or$blue
without units otherwise. -
Return a color with
red
,green
,blue
, andalpha
as the red, green, blue, and alpha channels, respectively.
-
-
Otherwise, if either
$saturation
or$lightness
aren't null:-
If either
$whiteness
or$blackness
aren't null, throw an error. -
If either
$saturation
or$lightness
aren't either null or numbers between 0 and 100 (inclusive), throw an error. -
Let
hue
be the result of callinghue($color)
if$hue
is null, or$hue
otherwise. -
Let
saturation
be the result of callingsaturation($color)
if$saturation
is null, or$saturation
otherwise. -
Let
lightness
be the result of callinglightness($color)
if$lightness
is null, or$lightness
otherwise. -
Return the result of calling
hsl()
withhue
,saturation
,lightness
, andalpha
.
-
-
Otherwise, if either
$hue
,$whiteness
, or$blackness
aren't null:-
If either
$whiteness
or$blackness
aren't either null or numbers with unit%
between0%
and100%
(inclusive), throw an error. -
Let
hue
be the result of callinghue($color)
if$hue
is null, or$hue
otherwise. -
Let
whiteness
be the result of callingwhiteness($color)
if$whiteness
is null, or$whiteness
otherwise. -
Let
blackness
be the result of callingblackness($color)
if$blackness
is null, or$blackness
otherwise. -
Return the result of calling
hwb()
withhue
,whiteness
,blackness
, andalpha
.
-
-
Otherwise, return a color with the same red, green, and blue channels as
$color
andalpha
as its alpha channel.
complement($color)
This function is also available as a global function named complement()
.
darken($color, $amount)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
desaturate($color, $amount)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
fade-in($color, $amount)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
fade-out($color, $amount)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
grayscale($color)
This function is also available as a global function named grayscale()
.
-
If
$color
is a number and this function is called as a global function, return a plain CSS function string with the name"grayscale"
and the argument$color
. -
Otherwise, if
$color
is not a color, throw an error. -
Return a color with the same hue and lightness as
$color
but with saturation 0.
green($color)
This function is also available as a global function named green()
.
hue($color)
This function is also available as a global function named hue()
.
-
hwb($hue, $whiteness, $blackness, $alpha: 1)
-
If any of
$hue
,$whiteness
,$blackness
, or$alpha
aren't numbers, throw an error. -
Let
hue
be the result of converting$hue
todeg
allowing unitless. -
Set
hue
to(hue % 360deg) / 60deg
. -
If either of
$whiteness
or$blackness
don't have unit%
or aren't between0%
and100%
(inclusive), throw an error. -
Let
whiteness
be$whiteness / 100%
. -
Let
blackness
be$blackness / 100%
. -
If
whiteness + blackness > 1
:-
Set
whiteness
towhiteness / (whiteness + blackness)
. -
Set
blackness
toblackness / (whiteness + blackness)
.
-
-
Let
red
,green
, andblue
be the result of convertinghue
,whiteness
, andblackness
to RGB. -
Set
red
,green
, andblue
to their existing values multiplied by 255 and rounded to the nearest integers. -
Let
alpha
be the result of percent-converting$alpha
with amax
of 1. -
Return a color with the given
red
,green
,blue
, andalpha
channels.
-
-
hwb($channels)
-
If
$channels
is not an unbracketed space-separated list, throw an error. -
If
$channels
does not includes exactly three elements, throw an error. -
Let
hue
andwhiteness
be the first two elements of$channels
-
If the third element of
$channels
has preserved its status as two slash-separated numbers:- Let
blackness
be the number before the slash andalpha
the number after the slash.
- Let
-
Otherwise:
- Let
blackness
be the third element of$channels
.
- Let
-
Call
hwb()
withhue
,whiteness
,blackness
, andalpha
(if it's defined) as arguments and return the result.
-
ie-hex-str($color)
This function is also available as a global function named ie-hex-str()
.
invert($color, $weight: 100%)
This function is also available as a global function named invert()
.
-
If
$color
is a number and this function is called as a global function:-
If
$weight
is not100%
, throw an error. -
Return a plain CSS function string with the name
"invert"
and the argument$color
.
-
-
If
$color
is not a color, throw an error. -
Let
inverse
be a color with each RGB channel equal to 255 minus$color
's corresponding channel. -
Call
mix()
with$color
,inverse
, and$weight
and return the result.
lighten($color, $amount)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
lightness($color)
This function is also available as a global function named lightness()
.
mix($color1, $color2, $weight: 50%)
opacify($color, $amount)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
This function is also available as a global function named red()
.
red($color)
This function is also available as a global function named mix()
.
saturate($color, $amount)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
saturation($color)
This function is also available as a global function named saturation()
.
scale($color,
$red: null, $green: null, $blue: null,
$saturation: null, $lightness: null,
$whiteness: null, $blackness: null,
$alpha: null)
This function is also available as a global function named scale-color()
.
-
If
$color
isn't a color, throw an error. -
Let
alpha
be$color
's alpha channel. -
If
$alpha
isn't null, setalpha
to the result of scalingalpha
by$alpha
withmax
1. -
If any of
$red
,$green
, or$blue
aren't null:-
If any of
$saturation
,$lightness
,$whiteness
, or$blackness
aren't null, throw an error. -
Let
red
,green
, andblue
be$color
's red, green, and blue channels. -
If
$red
isn't null, setred
to the result of scalingred
by$red
withmax
255. -
If
$green
isn't null, setgreen
to the result of scalinggreen
by$green
withmax
255. -
If
$blue
isn't null, setblue
to the result of scalingblue
by$blue
withmax
255. -
Return a color with
red
,green
,blue
, andalpha
as the red, green, blue, and alpha channels, respectively.
-
-
Otherwise, if either
$saturation
or$lightness
aren't null:-
If either
$whiteness
or$blackness
aren't null, throw an error. -
Let
hue
,saturation
, andlightness
be the result of callinghue($color)
,saturation($color)
, andlightness($color)
respectively. -
If
$saturation
isn't null, setsaturation
to the result of scalingsaturation
by$saturation
withmax
100%
. -
If
$lightness
isn't null, setlightness
to the result of scalinglightness
by$lightness
withmax
100%
. -
Return the result of calling
hsl()
withhue
,saturation
,lightness
, andalpha
.
-
-
Otherwise, if either
$hue
,$whiteness
, or$blackness
aren't null:-
Let
hue
,whiteness
, andblackness
be the result of callinghue($color)
,whiteness($color)
, andblackness($color)
respectively. -
If
$whiteness
isn't null, setwhiteness
to the result of scalingwhiteness
by$whiteness
withmax
100%
. -
If
$blackness
isn't null, setblackness
to the result of scalingblackness
by$blackness
withmax
100%
. -
Return the result of calling
hwb()
withhue
,whiteness
,blackness
, andalpha
.
-
-
Otherwise, return a color with the same red, green, and blue channels as
$color
andalpha
as its alpha channel.
transparentize($color, $amount)
-
Throw an error.
This error should indicate that the user should use the
adjust()
function instead.
whiteness($color)
-
If
$color
is not a color, throw an error. -
Return a number with unit
%
between0%
and100%
(inclusive) such that:-
hwb(hue($color), whiteness($color), blackness($color))
returns a color with the same red, green, and blue channels as$color
. -
whiteness($color) + blackness($color) <= 100%
.
The specific number returned here is left purposefully open-ended to allow implementations to pursue different strategies for representing color values. For example, one implementation may eagerly convert all colors to RGB channels and convert back when
whiteness()
orblackness()
is called, where another may keep around their original HWB values and return those as-is. -