Skip to content

Commit a7dbe90

Browse files
committed
[css-color-5] Relative color syntax
1 parent 4213d75 commit a7dbe90

File tree

1 file changed

+107
-2
lines changed

1 file changed

+107
-2
lines changed

css-color-5/Overview.bs

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Modifying colors {#colormodify}
182182
===============================
183183

184184
Note: There are currently two proposals for modifying colors:
185-
''color-adjust'' and [[#extended-color|Extending color() to add modifiers]].
185+
''color-adjust'' and [[#relative-colors|Relative color syntax]].
186186

187187
Issue(3187): there are two proposals for color modification (<a href="https://github.com/w3c/csswg-drafts/issues/3187#issuecomment-499126198">proposal 1</a>, <a href="https://gist.github.com/una/edcfa0d3600e0b89b2ebf266bf549721">proposal 2</a>).
188188
The CSS WG expects that the best aspects of each
@@ -237,6 +237,111 @@ Adjusting colors: the ''color-adjust'' function {#coloradjust}
237237

238238
<img src="images/adjust_red_lightness30.png" alt="Result of adjust-color(red, lightness(30%)" />
239239

240-
Extending ''color()'' to add modifiers {#extended-color}
240+
Relative color syntax {#relative-colors}
241241
--------------------------------------------------------
242242

243+
Besides specifying absolute coordinates, all color functions can also be used with a *relative syntax* to produce colors in the function's target color space, based on an existing 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 color space.
244+
245+
The following sections outline the relative color syntax for each color function.
246+
247+
Issue: A future version of this specification may define a relative syntax for ''color()'' as well.
248+
249+
<h4>Relative RGB colors</h4>
250+
251+
The grammar of the ''rgb()'' function is extended as follows:
252+
253+
<pre class='prod'>
254+
<dfn>rgb()</dfn> = rgb([from <<color>>]? <<percentage>>{3} [ / <<alpha-value>> ]? ) |
255+
rgb([from <<color>>]? <<number>>{3} [ / <<alpha-value>> ]? )
256+
<dfn>&lt;alpha-value></dfn> = <<number>> | <<percentage>>
257+
</pre>
258+
259+
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:
260+
261+
- 'r' is a <<percentage>> that corresponds to the origin color's red channel after its conversion to sRGB
262+
- 'g' is a <<percentage>> that corresponds to the origin color's green channel after its conversion to sRGB
263+
- 'b' is a <<percentage>> that corresponds to the origin color's blue channel after its conversion to sRGB
264+
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
265+
266+
<h4>Relative HSL colors</h4>
267+
268+
The grammar of the ''hsl()'' function is extended as follows:
269+
270+
<pre class='prod'>
271+
<dfn>hsl()</dfn> = hsl([from <<color>>]? <<hue>> <<percentage>> <<percentage>> [ / <<alpha-value>> ]? )
272+
<dfn>&lt;hue></dfn> = <<number>> | <<angle>>
273+
</pre>
274+
275+
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:
276+
277+
- 'h' is a <<number>> that corresponds to the origin color's HSL hue after its conversion to sRGB, normalized to a [0, 360) range.
278+
- 's' is a <<percentage>> that corresponds to the origin color's HSL saturation after its conversion to sRGB
279+
- 'l' is a <<percentage>> that corresponds to the origin color's HSL lightness after its conversion to sRGB
280+
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
281+
282+
<h4>Relative HWB colors</h4>
283+
284+
The grammar of the ''hwb()'' function is extended as follows:
285+
286+
<pre class='prod'>
287+
<dfn>hwb()</dfn> = hwb([from <<color>>]? <<hue>> <<percentage>> <<percentage>> [ / <<alpha-value>> ]? )
288+
</pre>
289+
290+
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:
291+
292+
- 'h' is a <<number>> that corresponds to the origin color's HWB hue after its conversion to sRGB
293+
- 'w' is a <<percentage>> that corresponds to the origin color's HWB whiteness after its conversion to sRGB
294+
- 'b' is a <<percentage>> that corresponds to the origin color's HWB blackness after its conversion to sRGB
295+
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
296+
297+
<h4>Relative Lab colors</h4>
298+
299+
The grammar of the ''lab()'' function is extended as follows:
300+
301+
<pre class='prod'>
302+
<dfn>lab()</dfn> = lab([from <<color>>]? <<percentage>> <<number>> <<number>> [ / <<alpha-value>> ]? )
303+
</pre>
304+
305+
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:
306+
307+
- 'l' is a <<percentage>> that corresponds to the origin color's CIE Lightness
308+
- 'a' is a <<number>> that corresponds to the origin color's CIELab a axis
309+
- 'b' is a <<number>> that corresponds to the origin color's CIELab b axis
310+
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
311+
312+
<h4>Relative LCH colors</h4>
313+
314+
The grammar of the ''lch()'' function is extended as follows:
315+
316+
<pre class='prod'>
317+
<dfn>lch()</dfn> = lch([from <<color>>]? <<percentage>> <<number>> <<hue>> [ / <<alpha-value>> ]? )
318+
</pre>
319+
320+
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:
321+
322+
- 'l' is a <<percentage>> that corresponds to the origin color's CIE Lightness
323+
- 'c' is a <<number>> that corresponds to the origin color's LCH chroma
324+
- 'h' is a <<number>> that corresponds to the origin color's LCH hue, normalized to a [0, 360) range.
325+
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
326+
327+
<div class="example">
328+
''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.
329+
The result is <span class="swatch" style="--color: rgb(57.58%, 32.47%, 3.82%)"> </span> lch(49.80256% 54.0114 63.6769)
330+
</div>
331+
332+
<h4>Relative grayscale colors</h4>
333+
334+
The grammar of the ''gray()'' function is extended as follows:
335+
336+
<pre class='prod'>
337+
<dfn>gray()</dfn> = gray([from <<color>>]? <<number>> [ / <<alpha-value>> ]? )
338+
</pre>
339+
340+
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:
341+
342+
- 'l' is a <<percentage>> that corresponds to the origin color's CIE Lightness
343+
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
344+
345+
<div class="example">
346+
''gray(from var(--base-color) l)'' produces a grayscale version of ''var(--base-color)'' with the same lightness.
347+
</div>

0 commit comments

Comments
 (0)