@@ -216,7 +216,7 @@ Mixing Colors: the ''color-mix()'' Function {#color-mix}
216
216
217
217
218
218
<pre class='prod'>
219
- <dfn>color-mix()</dfn> = color-mix( <<color-interpolation-method>> , [ <<color>> && <<percentage [0,100]>> ? ]#{2} )
219
+ <dfn>color-mix()</dfn> = color-mix( <<color-interpolation-method>> , [ <<color>> && <<percentage [0,100]>> ? ]#)
220
220
</pre>
221
221
222
222
<wpt>
@@ -229,24 +229,9 @@ Mixing Colors: the ''color-mix()'' Function {#color-mix}
229
229
Percentage Normalization
230
230
</h3>
231
231
232
- Percentages are required to be in the range 0% to 100%. Negative percentages are specifically disallowed. The percentages are normalized as follows:
233
-
234
- 1. Let <var> p1</var> be the first percentage and <var> p2</var> the second one.
235
- 2. If both percentages are omitted, they each default to 50%
236
- (an equal mix of the two colors).
237
- 3. Otherwise, if <var> p2</var> is omitted, it becomes 100% - <var> p1</var>
238
- 4. Otherwise, if <var> p1</var> is omitted, it becomes 100% - <var> p2</var>
239
- 5. Otherwise, if both are provided and add up to greater than 100%,
240
- they are scaled accordingly so that they add up to 100%.
241
- 6. Otherwise, if both are provided and add up to less than 100%,
242
- the sum is saved as an alpha multiplier.
243
- If the sum is greater than zero,
244
- they are then scaled accordingly so that they add up to 100%.
245
-
246
- This means that
247
- <var> p1</var> becomes <var> p1</var> / (<var> p1</var> + <var> p2</var> )
248
- and
249
- <var> p2</var> becomes <var> p2</var> / (<var> p1</var> + <var> p2</var> ).
232
+ Percentages are required to be in the range 0% to 100%.
233
+ Negative percentages are specifically disallowed.
234
+ Percentages are normalized by [=normalizing mix percentages=] .
250
235
251
236
<wpt>
252
237
color-mix-percents-01.html
@@ -282,23 +267,53 @@ Mixing Colors: the ''color-mix()'' Function {#color-mix}
282
267
Calculating the Result of color-mix
283
268
</h3>
284
269
285
- After normalizing both percentages, the result is produced via the following algorithm:
286
-
287
- 1. If the alpha multiplier is zero, the result is ''transparent'' , converted to the specified interpolation <<color-space>>
288
- 2. Otherwise, as described in [[css-color-4#interpolation]] ,
289
- both colors are converted to the specified interpolation <<color-space>> ,
290
- taking into account any [=analogous components=] .
291
- 3. Colors are then interpolated in the specified color space,
292
- as described in [[css-color-4#interpolation]] .
293
- If the specified color space is a ''cylindrical-polar-color'' space,
294
- then the <<hue-interpolation-method>> controls the
295
- interpolation of hue, as described in
296
- [[css-color-4#hue-interpolation]] .
297
- If no <<hue-interpolation-method>> is specified, it is as if
298
- ''shorter'' had been specified.
299
- 4. If an alpha multiplier was produced during percentage normalization,
300
- the alpha component of the interpolated result is multiplied
301
- by the alpha multiplier.
270
+ <div algorithm>
271
+ To <dfn export>calculate a color-mix()</dfn> :
272
+
273
+ 1. [=Normalize mix percentages=] from the list of [=mix items=] passed to the function,
274
+ letting |items| and |leftover| be the result.
275
+ 2. Let |alpha mult| be <code> 1 - |leftover|</code> ,
276
+ interpreting |leftover| as a number between 0 and 1.
277
+ 3. If |alpha mult| is 0,
278
+ return [=transparent black=] ,
279
+ converted to the specified interpolation <<color-space>> .
280
+ 4. If |items| is length 1,
281
+ set |color| to the color of that sole item.
282
+
283
+ Otherwise:
284
+
285
+ 1. Let |item stack| be a [=/stack=] made by reversing |items|.
286
+ (Thus, with the first item at the top of the stack.)
287
+ 2. While |item stack| has length 2 or greater:
288
+ 1. [=stack/Pop=] from |item stack| twice,
289
+ letting |a| and |b| be the two results in order.
290
+ Let |combined percentage| be the sum of |a| and |b|’s percentages.
291
+ 2. Interpolate |a| and |b|’s colors
292
+ as described in [[css-color-4#interpolation]] ,
293
+ with a progress percentage equal to <code> (|b|’s percentage) / |combined percentage|)</code> .
294
+ If the specified color space is a [=cylindrical polar color=] space,
295
+ then the <<hue-interpolation-method>> controls the
296
+ interpolation of hue, as described in
297
+ [[css-color-4#hue-interpolation]] .
298
+ If no <<hue-interpolation-method>> is specified,
299
+ assume ''shorter'' .
300
+ 3. Create a new [=mix item=] with the resulting color
301
+ and a percentage of |combined percentage|,
302
+ and [=stack/push=] it onto |item stack|.
303
+ 3. Set |color| to the color of the sole remaining item in |item stack|.
304
+ 5. Multiply the alpha component of |color| by |alpha mult|.
305
+ 6. Return |color|.
306
+
307
+ Note: In [=cylindrical polar color=] spaces,
308
+ mixing is order-dependent,
309
+ as which direction is “shorter” or “longer” around the hue circle
310
+ can change depending on what other mixes have already been performed.
311
+ This algorithm mixes each color in the specified order,
312
+ mixing the result with the next in the list.
313
+ For [=rectangular orthogonal color=] spaces,
314
+ the order doesn't matter,
315
+ and the process can be simplified.
316
+ </div>
302
317
303
318
<wpt>
304
319
color-mix-basic-001.html
0 commit comments