Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -256,4 +250,23 @@ private function allComponentsAreNumbers(): bool

return true;
}

/**
* Note that this method assumes the following:
* - The {@see aComponents} array has keys for `r`, `g` and `b`;
* - The values in the array are all instances of {@see Size}.
*
* Errors will be triggered or thrown if this is not the case.
*/
private function renderAsHex(): string
{
$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);
}
}