diff --git a/src/Value/Color.php b/src/Value/Color.php index 07f11c346..a58357a66 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -230,15 +230,9 @@ public function render(OutputFormat $outputFormat): string && \implode('', \array_keys($this->aComponents)) === 'rgb' && $this->allComponentsAreNumbers() ) { - $result = \sprintf( - '%02x%02x%02x', - $this->aComponents['r']->getSize(), - $this->aComponents['g']->getSize(), - $this->aComponents['b']->getSize() - ); - return '#' . (($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5]) - ? "$result[0]$result[2]$result[4]" : $result); + return $this->renderAsHex(); } + return parent::render($outputFormat); } @@ -256,4 +250,26 @@ private function allComponentsAreNumbers(): bool return true; } + + /** + * Note that this method assumes the following: + * - The `aComponents` array has keys for `r`, `g` and `b`; + * - The values in the array are all instances of `Size`. + * + * Errors will be triggered or thrown if this is not the case. + * + * @return non-empty-string + */ + private function renderAsHex(): string + { + $result = \sprintf( + '%02x%02x%02x', + $this->aComponents['r']->getSize(), + $this->aComponents['g']->getSize(), + $this->aComponents['b']->getSize() + ); + $canUseShortVariant = ($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5]); + + return '#' . ($canUseShortVariant ? $result[0] . $result[2] . $result[4] : $result); + } }