This proposal expands Sass's built-in rgb()
, rgba()
, hsl()
, and hsla()
functions to support the new terse syntax defined in CSS Color Level 4.
This section is non-normative.
Sass's rgb()
and hsl()
functions will add support for the Color Level 4
syntax, so rgb(179 115 153)
, rgb(70% 45% 60%)
, and hsl(324deg 29% 57%)
will all return the same color. They'll also support the slash-separated alpha
syntax using the normal rules for distinguishing between division and /
,
so rgb(179 115 153 / 50%)
will return a 50%-opacity color but
rgb(179 115 $blue / 50%)
will not.
Color Level 4 redefines rgba()
and hsla()
as pure aliases for rgb()
and
hsl()
, which also means that rgb()
and hsl()
support the old syntax for
defining colors with alpha channels. In keeping with this, Sass will redefine
rgba()
and hsla()
as aliases for rgb()
and hsl()
as well, and add
support to the latter for the former's syntax. This includes the Sass-specific
rgba($color, $alpha)
syntax, which will now work with rgb()
and hsl()
as
well.
Sass will continue generating colors with alpha channels as rgba()
calls, for
backwards-compatibility with older browsers that don't yet support Color Level
4.
A special number string is an unquoted string that CSS will recognize as a
function that may return a number. For the purposes of Sass, this is any
unquoted string that begins with calc(
or var(
. This matching is
case-insensitive.
Sass functions that shadow CSS functions must work with any invocation that CSS allows, which includes allowing special number strings anywhere a number would be allowed.
This is intended to match the existing behavior for determining special number strings for
rgb()
,hsl()
,rgba()
, andhsla()
.
A special variable string is special number string that begins with
var(
. This matching is case-insensitive.
Unlike other special number strings, variables can expand into multiple arguments to a single function.
This is intended to match the existing behavior for determining special variable strings for
rgb()
,hsl()
,rgba()
, andhsla()
.
This proposal adds new overloads to the rgb()
, hsl()
, rgba()
, and hsla()
functions:
-
rgb($channels)
-
If
$channels
is a special variable string, return a plain CSS function string with the name"rgb"
and the argument$channels
. -
If
$channels
is not an unbracketed space-separated list, throw an error. -
If
$channels
has more than three elements, throw an error. -
If
$channels
has fewer than three elements:-
If any element of
$channels
is a special variable string, return a plain CSS function string with the name"rgb"
and the argument$channels
. -
If the last element of
$channels
is an unquoted string that begins withvar(
and contains/
, return a plain CSS function string with the name"rgb"
and the argument$channels
. -
Otherwise, throw an error.
-
-
Let
red
andgreen
be the first two elements of$channels
. -
If the third element of
$channels
is an unquoted string that contains/
:- Return a plain CSS function string with the name
"rgb"
and the argument$channels
.
- Return a plain CSS function string with the name
-
Otherwise, if the third element of
$channels
has preserved its status as two slash-separated numbers:- Let
blue
be the number before the slash andalpha
the number after the slash.
- Let
-
Otherwise:
- Let
blue
be the third element of$channels
.
- Let
-
Call
rgb()
withred
,green
,blue
, andalpha
(if it's defined) as arguments and return the result.
-
-
rgb($red, $green, $blue, $alpha)
- Call
rgba()
with the same argument and return the result, except that if it would return a plain CSS function named"rgb"
that function is named"rgba"
instead.
- Call
-
rgb($color, $alpha)
-
If either argument is a special variable string, return a plain CSS function string with the name
"rgb"
and the same arguments. -
Call
rgba()
with the same arguments and return the result.
-
-
hsl($channels)
-
If
$channels
is a special variable string, return a plain CSS function string with the name"hsl"
and the argument$channels
. -
If
$channels
is not an unbracketed space-separated list, throw an error. -
If
$channels
has more than three elements, throw an error. -
If
$channels
has fewer than three elements:-
If any element of
$channels
is a special variable string, return a plain CSS function string with the name"hsl"
and the argument$channels
. -
If the last element of
$channels
is an unquoted string that begins withvar(
and contains/
, return a plain CSS function string with the name"hsl"
and the argument$channels
. -
Otherwise, throw an error.
-
-
Let
hue
andsaturation
be the first two elements of$channels
. -
If the third element of
$channels
is an unquoted string that contains/
:- Return a plain CSS function string with the name
"rgb"
and the argument$channels
.
- Return a plain CSS function string with the name
-
Otherwise, if the third element of
$channels
has preserved its status as two slash-separated numbers:- Let
lightness
be the number before the slash andalpha
the number after the slash.
- Let
-
Otherwise:
- Let
lightness
be the third element of$channels
.
- Let
-
Call
hsl()
withhue
,saturation
,lightness
, andalpha
(if it's defined) as arguments and return the result.
-
-
hsl($hue, $saturation, $lightness, $alpha)
- Call
hsla()
with the same arguments and return the result, except that if it would return a plain CSS function named"hsla"
that function is named"hsl"
instead.
- Call
-
rgba($channels)
- Call
rgb()
with the same argument and return the result, except that if it would return a plain CSS function named"rgb"
that function is named"rgba"
instead.
- Call
-
rgba($red, $green, $blue)
- Call
rgb()
with the same argument and return the result, except that if it would return a plain CSS function named"rgb"
that function is named"rgba"
instead.
- Call
-
hsla($channels)
- Call
hsl()
with the same arguments and return the result, except that if it would return a plain CSS function named"hsl"
that function is named"hsla"
instead.
- Call
-
hsla($hue, $saturation, $lightness)
- Call
hsl()
with the same arguments and return the result, except that if it would return a plain CSS function named"hsl"
that function is named"hsla"
instead.
- Call