Skip to content

Commit 66e6be3

Browse files
committed
[CLEANUP] Extract method Color::renderAsHex
An additional method to render in the "modern" syntax will soon be needed. Splitting up the `render` method into sub-methods will help as a precursor to that.
1 parent 59bcb6c commit 66e6be3

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/Value/Color.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,9 @@ public function render(OutputFormat $outputFormat): string
230230
&& \implode('', \array_keys($this->aComponents)) === 'rgb'
231231
&& $this->allComponentsAreNumbers()
232232
) {
233-
$result = \sprintf(
234-
'%02x%02x%02x',
235-
$this->aComponents['r']->getSize(),
236-
$this->aComponents['g']->getSize(),
237-
$this->aComponents['b']->getSize()
238-
);
239-
return '#' . (($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5])
240-
? "$result[0]$result[2]$result[4]" : $result);
233+
return $this->renderAsHex();
241234
}
235+
242236
return parent::render($outputFormat);
243237
}
244238

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

257251
return true;
258252
}
253+
254+
/**
255+
* Note that this method assumes the following:
256+
* - The {@see aComponents} array has keys for `r`, `g` and `b`;
257+
* - The values in the array are all instances of {@see Size}.
258+
*
259+
* Errors will be triggered or thrown if this is not the case.
260+
*/
261+
private function renderAsHex(): string
262+
{
263+
$result = \sprintf(
264+
'%02x%02x%02x',
265+
$this->aComponents['r']->getSize(),
266+
$this->aComponents['g']->getSize(),
267+
$this->aComponents['b']->getSize()
268+
);
269+
return '#' . (($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5])
270+
? "$result[0]$result[2]$result[4]" : $result);
271+
}
259272
}

0 commit comments

Comments
 (0)