-
Notifications
You must be signed in to change notification settings - Fork 149
[BUGFIX] Render RGB functions with "modern" syntax if required #840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -231,6 +231,8 @@ public function render(OutputFormat $outputFormat): string | |
| && $this->allComponentsAreNumbers() | ||
| ) { | ||
| return $this->renderAsHex(); | ||
| } elseif ($this->shouldRenderInModernSyntax()) { | ||
oliverklee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return $this->renderInModernSyntax($outputFormat); | ||
| } | ||
|
|
||
| return parent::render($outputFormat); | ||
|
|
@@ -282,4 +284,66 @@ private function renderAsHex(): string | |
|
|
||
| return '#' . ($canUseShortVariant ? $result[0] . $result[2] . $result[4] : $result); | ||
| } | ||
|
|
||
| /** | ||
| * The "legacy" syntax does not allow RGB colors to have a mixture of `percentage`s and `number`s. | ||
oliverklee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| private function shouldRenderInModernSyntax(): bool | ||
| { | ||
| $function = $this->getRealName(); | ||
| if ($function !== 'rgb' && $function !== 'rgba') { | ||
oliverklee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return false; | ||
oliverklee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| $hasPercentage = false; | ||
| $hasNumber = false; | ||
| foreach ($this->aComponents as $key => $value) { | ||
| if ($key === 'a') { | ||
| // ALpha can have units that don't match those of the RGB components in the "legacy" syntax. | ||
oliverklee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // So it is not necessary to check it. It's also always last, hence `break` rather than `continue`. | ||
oliverklee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| break; | ||
| } | ||
| if (!$value instanceof Size) { | ||
oliverklee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Unexpected, unknown, or modified via the API | ||
| return false; | ||
| } | ||
| $unit = $value->getUnit(); | ||
| // `switch` only does loose comparison | ||
| if ($unit === null) { | ||
| $hasNumber = true; | ||
| } elseif ($unit === '%') { | ||
| $hasPercentage = true; | ||
| } else { | ||
| // Invalid unit | ||
| return false; | ||
oliverklee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return $hasPercentage && $hasNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @return non-empty-string | ||
| */ | ||
| private function renderInModernSyntax(OutputFormat $outputFormat): string | ||
| { | ||
| \end($this->aComponents); | ||
| if (\key($this->aComponents) === 'a') { | ||
|
||
| $alpha = $this->aComponents['a']; | ||
| $componentsWithoutAlpha = \array_diff_key($this->aComponents, ['a' => 0]); | ||
oliverklee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| $componentsWithoutAlpha = $this->aComponents; | ||
|
||
| } | ||
oliverklee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $arguments = $outputFormat->implode(' ', $componentsWithoutAlpha); | ||
| if (isset($alpha)) { | ||
oliverklee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $arguments = $outputFormat->implode( | ||
| $outputFormat->spaceBeforeListArgumentSeparator('/') . '/' | ||
oliverklee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| . $outputFormat->spaceAfterListArgumentSeparator('/'), | ||
| [$arguments, $alpha] | ||
| ); | ||
| } | ||
|
|
||
| return $this->getName() . '(' . $arguments . ')'; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.