@@ -231,7 +231,9 @@ public function render(OutputFormat $outputFormat): string
231231 && $ this ->allComponentsAreNumbers ()
232232 ) {
233233 return $ this ->renderAsHex ();
234- } elseif ($ this ->shouldRenderInModernSyntax ()) {
234+ }
235+
236+ if ($ this ->shouldRenderInModernSyntax ()) {
235237 return $ this ->renderInModernSyntax ($ outputFormat );
236238 }
237239
@@ -287,11 +289,23 @@ private function renderAsHex(): string
287289
288290 /**
289291 * 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).
290303 */
291304 private function shouldRenderInModernSyntax (): bool
292305 {
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 )) {
295309 return false ;
296310 }
297311
@@ -327,21 +341,19 @@ private function shouldRenderInModernSyntax(): bool
327341 */
328342 private function renderInModernSyntax (OutputFormat $ outputFormat ): string
329343 {
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 ' ) {
332348 $ alpha = $ this ->aComponents ['a ' ];
333- $ componentsWithoutAlpha = \array_diff_key ($ this ->aComponents , ['a ' => 0 ]);
334- } else {
335- $ componentsWithoutAlpha = $ this ->aComponents ;
349+ unset($ componentsWithoutAlpha ['a ' ]);
336350 }
337351
338352 $ arguments = $ outputFormat ->implode (' ' , $ componentsWithoutAlpha );
339353 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 ]);
345357 }
346358
347359 return $ this ->getName () . '( ' . $ arguments . ') ' ;
0 commit comments