Skip to content

Commit f2b9bb1

Browse files
committed
[css-color-5] Rewrite relative colors section to be more specific about functionality, dfn some terms.
1 parent 2bcf97b commit f2b9bb1

File tree

1 file changed

+147
-34
lines changed

1 file changed

+147
-34
lines changed

css-color-5/Overview.bs

Lines changed: 147 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -629,28 +629,125 @@ Adjusting colors: the ''color-adjust'' function {#coloradjust}
629629
Relative color syntax {#relative-colors}
630630
----------------------------------------
631631

632-
Besides specifying absolute coordinates, all color functions can also be used with a *relative syntax* to produce colors in the function's target [=colorspace=], based on an existing color (henceforth referred to as "origin color"). This syntax consists of the keyword ''from'', a <<color>> value, and optionally numerical coordinates specific to the color function. To allow calculations on the original color's coordinates, there are single-letter keywords for each coordinate and `alpha` that corresponds to the color's alpha. If no coordinates are specified, the function merely converts the origin color to the target function's [=colorspace=].
633-
634-
The following sections outline the relative color syntax for each color function.
632+
In previous levels of this specification,
633+
the color functions could only specify colors in an absolute manner,
634+
by directly specifying all of the color channels.
635+
636+
The new <dfn export>relative color</dfn> syntax
637+
allows existing colors to be modified
638+
using the color functions:
639+
if an <dfn>origin color</dfn> is specified,
640+
then each color channel can <em>either</em> be directly specified,
641+
or taken from the origin color
642+
(and possibly modified with [=math functions=]).
635643

636644
Issue: A future version of this specification may define a relative syntax for ''color()'' as well.
637645

646+
The precise details of each function's changes to accomodate [=relative colors=] are listed below,
647+
but they all follow a common structure:
648+
649+
* An [=origin color=] can be specified with a ''from <<color>>'' value at the start of the function.
650+
* If an [=origin color=] is specified,
651+
the remaining arguments can either be specified directly, as normal,
652+
be specified as a <dfn>channel keyword</dfn> referring to one of the channels of the [=origin color=].
653+
[=Math functions=] can also use these keywords
654+
to do dynamic modifications of the [=origin color's=] channels.
655+
* [=Relative color=] syntax doesn't change whether an argument is required or optional.
656+
If the alpha value is omitted, however,
657+
it defaults to taking from the [=origin color=]
658+
(rather than defaulting to ''100%'', as it does in the absolute syntax).
659+
660+
If the [=origin color=] was originally specified with a different color function,
661+
it's first converted into the chosen color function,
662+
so it has meaningful values for the channels.
663+
664+
<div class=example>
665+
For example, if a theme color is specified as opaque,
666+
but in a particular instance you need it to be partially transparent:
667+
668+
<pre highlight=css>
669+
html { --bg-color: blue; }
670+
.overlay {
671+
background: rgb(from var(--bg-color) r g b / 80%);
672+
}
673+
</pre>
674+
675+
In this example, the r, g, and b channels of the [=origin color=] are unchanged,
676+
indicated by specifying them with the keywords
677+
drawing their values from the [=origin color=],
678+
but the opacity is set to ''80%'' to make it slightly transparent,
679+
regardless of what the [=origin color's=] opacity was.
680+
</div>
681+
682+
<div class=example>
683+
By using the [=channel keywords=] in a [=math function=],
684+
an [=origin color=] can be manipulated in more advanced ways.
685+
686+
<pre highlight=css>
687+
html { --color: green; }
688+
.foo {
689+
--darker-accent: lch(from var(--color) calc(l / 2) c h);
690+
}
691+
</pre>
692+
693+
In this example, the [=origin color=] is darkened
694+
by cutting its lightness in half,
695+
without changing any other aspect of the color.
696+
697+
Note as well that the [=origin color=] is a color keyword
698+
(effectively RGB),
699+
but it's automatically interpreted as an LCH color
700+
due to being used in the ''lch()'' function.
701+
</div>
702+
703+
<div class=example>
704+
While most uses of [=relative color=] syntax
705+
will use the [=channel keywords=] in their corresponding argument,
706+
you can use them in any position.
707+
708+
For example, to do a rough approximation of grayscaling a color:
709+
710+
<pre highlight=css>
711+
--blue-into-gray: rgb(from var(--color)
712+
calc(r * .3 + g * .59 + b * .11)
713+
calc(r * .3 + g * .59 + b * .11)
714+
calc(r * .3 + g * .59 + b * .11));
715+
</pre>
716+
717+
Using this,
718+
''red'' would become ''rgb(30% 30% 30%)'',
719+
''green'' would become ''rgb(59% 59% 59%)'',
720+
and ''blue'' would become ''rgb(11% 11% 11%)''.
721+
A more moderate color, like ''darkolivegreen'',
722+
which has RGB values ''rgb(85 107 47)'',
723+
would become approximately ''rgb(37% 37% 37%)''.
724+
725+
(Note, tho, that an easier and more accurate way to grayscale a color
726+
is to use the ''lch()'' function,
727+
as that colorspace is more accurate to human perception:
728+
''lch(from var(--color) l 0 h)'' preserves the lightness,
729+
but zeroes out the chroma,
730+
which determines how "colorful" the color is.)
731+
</div>
732+
638733
<h4 id="relative-RGB">Relative RGB colors</h4>
639734

640735
The grammar of the ''rgb()'' function is extended as follows:
641736

642737
<pre class='prod'>
643-
<dfn>rgb()</dfn> = rgb([from <<color>>]? <<percentage>>{3} [ / <<alpha-value>> ]? ) |
644-
rgb([from <<color>>]? <<number>>{3} [ / <<alpha-value>> ]? )
645-
<dfn>&lt;alpha-value></dfn> = <<number>> | <<percentage>>
738+
<dfn>rgb()</dfn> = rgb( <<percentage>>{3} [ / <<alpha-value>> ]? ) |
739+
rgb( <<number>>{3} [ / <<alpha-value>> ]? ) |
740+
rgb( [ from <<color>> ]? [ <<number>> | <<percentage>> ]{3} [ / <<alpha-value>> ]? )
646741
</pre>
647742

648-
When an origin color is present, the following keywords can also be used in this function (provided the end result conforms to the expected type for the parameter) and correspond to:
743+
Within a [=relative color=] syntax ''rgb()'' function,
744+
the allowed [=channel keywords=] are:
649745

650-
- 'r' is a <<percentage>> that corresponds to the origin color's red channel after its conversion to sRGB
651-
- 'g' is a <<percentage>> that corresponds to the origin color's green channel after its conversion to sRGB
652-
- 'b' is a <<percentage>> that corresponds to the origin color's blue channel after its conversion to sRGB
653-
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
746+
* <dfn value for="rgb()">r</dfn>, <dfn value for="rgb()">g</dfn>, and <dfn value for="rgb()">b</dfn>
747+
are all <<percentage>>s
748+
that correspond to the [=origin color's=] red, green, and blue channels
749+
after its conversion to sRGB
750+
* <dfn value for="rgb()">alpha</dfn> is a <<percentage>> that corresponds to the [=origin color's=] alpha transparency
654751

655752
<div class="example">
656753
To manipulate color channels in the sRGB colorspace:
@@ -668,21 +765,25 @@ The grammar of the ''hsl()'' function is extended as follows:
668765

669766
<pre class='prod'>
670767
<dfn>hsl()</dfn> = hsl([from <<color>>]? <<hue>> <<percentage>> <<percentage>> [ / <<alpha-value>> ]? )
671-
<dfn>&lt;hue></dfn> = <<number>> | <<angle>>
672768
</pre>
673769

674-
When an origin color is present, the following keywords can also be used in this function (provided the end result conforms to the expected type for the parameter) and correspond to:
770+
Within a [=relative color=] syntax ''hsl()'' function,
771+
the allowed [=channel keywords=] are:
675772

676-
- 'h' is a <<number>> that corresponds to the origin color's HSL hue after its conversion to sRGB, normalized to a [0, 360) range.
677-
- 's' is a <<percentage>> that corresponds to the origin color's HSL saturation after its conversion to sRGB
678-
- 'l' is a <<percentage>> that corresponds to the origin color's HSL lightness after its conversion to sRGB
679-
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
773+
* <dfn value for="hsl()">h</dfn> is an <<angle>>
774+
that corresponds to the [=origin color's=] HSL hue
775+
after its conversion to sRGB,
776+
normalized to a [0deg, 360deg) range
777+
* <dfn value for="hsl()">s</dfn> and <dfn value for="hsl()">l</dfn>
778+
are <<percentage>>s that correspond to the [=origin color's=] HSL saturation and lightness
779+
after its conversion to sRGB
780+
* <dfn value for="hsl()">alpha</dfn> is a <<percentage>> that corresponds to the [=origin color's=] alpha transparency
680781

681782
<div class="example">
682783
This adds 180 degrees to the hue angle, giving a complementary color.
683784
<pre>
684785
--accent: <span class="swatch" style="--color: lightseagreen"></span> lightseagreen;
685-
--complement: <span class="swatch" style="--color: hsl(357deg 70% 41%)"></span> hsl(from var(--accent) calc(h+180) s l);
786+
--complement: <span class="swatch" style="--color: hsl(357deg 70% 41%)"></span> hsl(from var(--accent) calc(h + 180deg) s l);
686787
</pre>
687788
lightseagreen is hsl(177deg 70% 41%), so --complement is <span class="swatch" style="--color: hsl(357deg 70% 41%)"></span> hsl(357deg 70% 41%)
688789
</div>
@@ -695,12 +796,17 @@ The grammar of the ''hwb()'' function is extended as follows:
695796
<dfn>hwb()</dfn> = hwb([from <<color>>]? <<hue>> <<percentage>> <<percentage>> [ / <<alpha-value>> ]? )
696797
</pre>
697798

698-
When an origin color is present, the following keywords can also be used in this function (provided the end result conforms to the expected type for the parameter) and correspond to:
799+
Within a [=relative color=] syntax ''hwb()'' function,
800+
the allowed [=channel keywords=] are:
699801

700-
- 'h' is a <<number>> that corresponds to the origin color's HWB hue after its conversion to sRGB
701-
- 'w' is a <<percentage>> that corresponds to the origin color's HWB whiteness after its conversion to sRGB
702-
- 'b' is a <<percentage>> that corresponds to the origin color's HWB blackness after its conversion to sRGB
703-
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
802+
* <dfn value for="hwb()">h</dfn> is an <<angle>>
803+
that corresponds to the [=origin color's=] HWB hue
804+
after its conversion to sRGB,
805+
normalized to a [0deg, 360deg) range
806+
* <dfn value for="hwb()">w</dfn> and <dfn value for="hwb()">b</dfn>
807+
are <<percentage>>s that correspond to the [=origin color's=] HWB whiteness and blackness
808+
after its conversion to sRGB
809+
* <dfn value for="hwb()">alpha</dfn> is a <<percentage>> that corresponds to the [=origin color's=] alpha transparency
704810

705811
<h4 id="relative-Lab">Relative Lab colors</h4>
706812

@@ -710,12 +816,14 @@ The grammar of the ''lab()'' function is extended as follows:
710816
<dfn>lab()</dfn> = lab([from <<color>>]? <<percentage>> <<number>> <<number>> [ / <<alpha-value>> ]? )
711817
</pre>
712818

713-
When an origin color is present, the following keywords can also be used in this function (provided the end result conforms to the expected type for the parameter) and correspond to:
819+
Within a [=relative color=] syntax ''lab()'' function,
820+
the allowed [=channel keywords=] are:
714821

715-
- 'l' is a <<percentage>> that corresponds to the origin color's CIE Lightness
716-
- 'a' is a <<number>> that corresponds to the origin color's CIELab a axis
717-
- 'b' is a <<number>> that corresponds to the origin color's CIELab b axis
718-
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
822+
* <dfn value for="lab()">l</dfn> is a <<percentage>>
823+
that corresponds to the [=origin color's=] CIE Lightness
824+
* <dfn value for="lab()">a</dfn> and <dfn value for="lab()">b</dfn> are <<number>>s
825+
that correspond to the [=origin color's=] CIELab a and b axises
826+
* <dfn value for="lab()">alpha</dfn> is a <<percentage>> that corresponds to the [=origin color's=] alpha transparency
719827

720828
<div class="example">
721829
Multiple ways to adjust the transparency of a base color:
@@ -747,12 +855,17 @@ The grammar of the ''lch()'' function is extended as follows:
747855
<dfn>lch()</dfn> = lch([from <<color>>]? <<percentage>> <<number>> <<hue>> [ / <<alpha-value>> ]? )
748856
</pre>
749857

750-
When an origin color is present, the following keywords can also be used in this function (provided the end result conforms to the expected type for the parameter) and correspond to:
858+
Within a [=relative color=] syntax ''lch()'' function,
859+
the allowed [=channel keywords=] are:
751860

752-
- 'l' is a <<percentage>> that corresponds to the origin color's CIE Lightness
753-
- 'c' is a <<number>> that corresponds to the origin color's LCH chroma
754-
- 'h' is a <<number>> that corresponds to the origin color's LCH hue, normalized to a [0, 360) range.
755-
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
861+
* <dfn value for="lch()">l</dfn> is a <<percentage>>
862+
that corresponds to the [=origin color's=] CIE Lightness
863+
* <dfn value for="lch()">c</dfn> is a <<number>>
864+
that corresponds to the [=origin color's=] LCH chroma
865+
* <dfn value for="lch()">h</dfn> is an <<angle>>
866+
that corresponds to the [=origin color's=] LCH hue,
867+
normalized to a [0deg, 360deg) range.
868+
* <dfn value for="lch()">alpha</dfn> is a <<percentage>> that corresponds to the [=origin color's=] alpha transparency
756869

757870
<div class="example">
758871
''lch(from peru calc(l * 0.8) c h)'' produces a color that is 20% darker than <span class="swatch" style="--color: peru"></span> peru or lch(62.2532% 54.0114 63.6769), with its chroma and hue left unchanged.
@@ -763,7 +876,7 @@ When an origin color is present, the following keywords can also be used in this
763876
This adds 180 degrees to the hue angle, giving the complementary color.
764877
<pre>
765878
--accent: <span class="swatch" style="--color: lightseagreen"></span> lightseagreen;
766-
--complement: <span class="swatch" style="--color: rgb(88.2814% 51.1047% 58.3039%)"></span> LCH(from var(--accent) l c calc(h + 180));
879+
--complement: <span class="swatch" style="--color: rgb(88.2814% 51.1047% 58.3039%)"></span> LCH(from var(--accent) l c calc(h + 180deg));
767880
</pre>
768881
lightseagreen is LCH(65.4937% 39.4484 190.1013), so --complement is <span class="swatch" style="--color: rgb(88.2814% 51.1047% 58.3039%)"></span> LCH(65.4937% 39.4484 370.1013)
769882
</div>

0 commit comments

Comments
 (0)