Skip to content

Commit 9c00576

Browse files
authored
[CLEANUP] Extract method Color::renderAsHex (#825)
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 61971b8 commit 9c00576

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/Value/Color.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,9 @@ public function render(OutputFormat $outputFormat): string
230230
&& $this->getRealName() === '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

@@ -266,4 +260,26 @@ private function allComponentsAreNumbers(): bool
266260

267261
return true;
268262
}
263+
264+
/**
265+
* Note that this method assumes the following:
266+
* - The `aComponents` array has keys for `r`, `g` and `b`;
267+
* - The values in the array are all instances of `Size`.
268+
*
269+
* Errors will be triggered or thrown if this is not the case.
270+
*
271+
* @return non-empty-string
272+
*/
273+
private function renderAsHex(): string
274+
{
275+
$result = \sprintf(
276+
'%02x%02x%02x',
277+
$this->aComponents['r']->getSize(),
278+
$this->aComponents['g']->getSize(),
279+
$this->aComponents['b']->getSize()
280+
);
281+
$canUseShortVariant = ($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5]);
282+
283+
return '#' . ($canUseShortVariant ? $result[0] . $result[2] . $result[4] : $result);
284+
}
269285
}

0 commit comments

Comments
 (0)