Skip to content

Commit cf3031f

Browse files
committed
[css-color-5] Add prose about how to normalize percentages now that more than 1 is allowed
Related to w3c#6064
1 parent 84366aa commit cf3031f

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

css-color-5/Overview.bs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,33 @@ Mixing colors: the ''color-mix()'' function {#color-mix}
124124
<dfn>color-mix()</dfn> = color-mix( in <<colorspace>> , [ <<color>> && <<percentage>>? ]#{2})
125125
</pre>
126126

127-
If a percentage is omitted, it defaults to 50% (an equal mix of the two colors).
127+
The percentages are normalized as follows:
128128

129-
The result is produced via the following algorithm:
129+
1. Let <var>p1</var> be the first percentage and <var>p2</var> the second one.
130+
2. If both percentages are omitted, they each default to 50% (an equal mix of the two colors).
131+
3. Otherwise, if <var>p2</var> is omitted, it becomes 100% - <var>p1</var>
132+
4. Otherwise, if <var>p1</var> is omitted, it becomes 100% - <var>p2</var>
133+
5. Otherwise, if both are provided but do not add up to 100%,
134+
they are scaled accordingly so that they add up to 100%.
135+
This means that
136+
<var>p1</var> becomes <var>p1</var> / <var>p1</var> + <var>p2</var>
137+
and
138+
<var>p2</var> becomes <var>p2</var> / <var>p1</var> + <var>p2</var>.
130139

131-
1. If a percentage is omitted, it defaults to 50% (an equal mix of the two colors).
132-
If the color space is omitted, it defaults to 'lch'.
133-
2. Both colors are converted to the specified [=colorspace=].
140+
Issue: Should we handle p1 + p2 &lt; 100% differently? E.g. a mix with `transparent`?
141+
142+
After normalizing both percentages, the result is produced via the following algorithm:
143+
144+
1. Both colors are converted to the specified [=colorspace=].
134145
If the specified colorspace has a smaller gamut
135146
than the one in which the color to be adjusted is specified,
136147
gamut mapping will occur.
137-
3. Colors are then <a href="../css-color-4/#interpolation">interpolated in the specified colorspace</a>.
148+
2. Colors are then <a href="../css-color-4/#interpolation">interpolated in the specified colorspace</a>.
138149
The result of mixing is the color at the specified percentage along the progression
139150
of the second color to the first color.
140151

141-
Note: As a corrollary, 0% just returns the second color converted to the specified colorspace,
142-
100% returns the first color converted to the specified colorspace,
143-
and 50% produces an equal mix of both.
152+
Note: As a corrollary, a percentage of 0% just returns the other color converted to the specified colorspace,
153+
and a percentage of 100% returns the same color converted to the specified colorspace.
144154

145155
<div class="example">
146156
This example produces a mixture of 40% <span class="swatch" style="--color: peru"></span> peru

0 commit comments

Comments
 (0)