@@ -231,7 +231,9 @@ public function render(OutputFormat $outputFormat): string
231
231
&& $ this ->allComponentsAreNumbers ()
232
232
) {
233
233
return $ this ->renderAsHex ();
234
- } elseif ($ this ->shouldRenderInModernSyntax ()) {
234
+ }
235
+
236
+ if ($ this ->shouldRenderInModernSyntax ()) {
235
237
return $ this ->renderInModernSyntax ($ outputFormat );
236
238
}
237
239
@@ -287,11 +289,23 @@ private function renderAsHex(): string
287
289
288
290
/**
289
291
* The "legacy" syntax does not allow RGB colors to have a mixture of `percentage`s and `number`s.
292
+ *
293
+ * The "legacy" and "modern" monikers are part of the formal W3C syntax.
294
+ * See the following for more information:
295
+ * - {@link
296
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb#formal_syntax
297
+ * Description of the formal syntax for `rgb()` on MDN
298
+ * };
299
+ * - {@link
300
+ * https://www.w3.org/TR/css-color-4/#rgb-functions
301
+ * The same in the CSS Color Module Level 4 W3C Candidate Recommendation Draft
302
+ * } (as of 13 February 2024, at time of writing).
290
303
*/
291
304
private function shouldRenderInModernSyntax (): bool
292
305
{
293
- $ function = $ this ->getRealName ();
294
- if ($ function !== 'rgb ' && $ function !== 'rgba ' ) {
306
+ static $ colorFunctionsThatWithMixedValueTypesCannotBeRenderedInLegacySyntax = ['rgb ' , 'rgba ' ];
307
+ $ colorFunction = $ this ->getRealName ();
308
+ if (!\in_array ($ colorFunction , $ colorFunctionsThatWithMixedValueTypesCannotBeRenderedInLegacySyntax , true )) {
295
309
return false ;
296
310
}
297
311
@@ -327,21 +341,19 @@ private function shouldRenderInModernSyntax(): bool
327
341
*/
328
342
private function renderInModernSyntax (OutputFormat $ outputFormat ): string
329
343
{
330
- \end ($ this ->aComponents );
331
- if (\key ($ this ->aComponents ) === 'a ' ) {
344
+ // Maybe not yet without alpha, but will be...
345
+ $ componentsWithoutAlpha = $ this ->aComponents ;
346
+ \end ($ componentsWithoutAlpha );
347
+ if (\key ($ componentsWithoutAlpha ) === 'a ' ) {
332
348
$ alpha = $ this ->aComponents ['a ' ];
333
- $ componentsWithoutAlpha = \array_diff_key ($ this ->aComponents , ['a ' => 0 ]);
334
- } else {
335
- $ componentsWithoutAlpha = $ this ->aComponents ;
349
+ unset($ componentsWithoutAlpha ['a ' ]);
336
350
}
337
351
338
352
$ arguments = $ outputFormat ->implode (' ' , $ componentsWithoutAlpha );
339
353
if (isset ($ alpha )) {
340
- $ arguments = $ outputFormat ->implode (
341
- $ outputFormat ->spaceBeforeListArgumentSeparator ('/ ' ) . '/ '
342
- . $ outputFormat ->spaceAfterListArgumentSeparator ('/ ' ),
343
- [$ arguments , $ alpha ]
344
- );
354
+ $ separator = $ outputFormat ->spaceBeforeListArgumentSeparator ('/ ' )
355
+ . '/ ' . $ outputFormat ->spaceAfterListArgumentSeparator ('/ ' );
356
+ $ arguments = $ outputFormat ->implode ($ separator , [$ arguments , $ alpha ]);
345
357
}
346
358
347
359
return $ this ->getName () . '( ' . $ arguments . ') ' ;
0 commit comments