|
4 | 4 |
|
5 | 5 | final class BadgeStyle |
6 | 6 | { |
7 | | - private ?string $textColor = null; |
8 | | - |
9 | | - public function __construct( |
10 | | - private string $backgroundColor, |
11 | | - ?string $textColor = null |
12 | | - ) { |
13 | | - if (!$this->isSupportedColor($this->backgroundColor)) { |
14 | | - throw new \InvalidArgumentException(sprintf('The background color must be a full 6-digit hexadecimal color ("%s" given).', $this->backgroundColor)); |
| 7 | + private function __construct(private string $classes, private string $style) |
| 8 | + { |
| 9 | + } |
| 10 | + |
| 11 | + public static function fromColor(string $backgroundColor, ?string $textColor = null): self |
| 12 | + { |
| 13 | + if (!self::isSupportedColor($backgroundColor)) { |
| 14 | + throw new \InvalidArgumentException(sprintf('The background color must be a full 6-digit hexadecimal color ("%s" given).', $backgroundColor)); |
15 | 15 | } |
16 | 16 |
|
| 17 | + $classes = []; |
| 18 | + $styleProperties = ['background-color' => $backgroundColor]; |
| 19 | + |
17 | 20 | if (null === $textColor) { |
18 | | - $this->textColor = $this->computeTextColor($this->backgroundColor); |
19 | | - } elseif (!$this->isSupportedColor($this->textColor)) { |
20 | | - throw new \InvalidArgumentException(sprintf('The text color must be a full 6-digit hexadecimal color ("%s" given).', $this->textColor)); |
| 21 | + $classes[] = self::computeTextClass($backgroundColor); |
| 22 | + } elseif (self::isSupportedColor($textColor)) { |
| 23 | + $styleProperties['color'] = $textColor; |
| 24 | + } else { |
| 25 | + throw new \InvalidArgumentException(sprintf('The text color must be a full 6-digit hexadecimal color ("%s" given).', $textColor)); |
21 | 26 | } |
| 27 | + |
| 28 | + return new self(implode(' ', $classes), self::generateStyle($styleProperties)); |
| 29 | + } |
| 30 | + |
| 31 | + public function getClasses(): string |
| 32 | + { |
| 33 | + return $this->classes; |
22 | 34 | } |
23 | 35 |
|
24 | | - public function toStyle(): string |
| 36 | + public function getStyle(): string |
25 | 37 | { |
26 | | - return sprintf('background-color:%s; color:%s;', $this->backgroundColor, $this->textColor); |
| 38 | + return $this->style; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param array<string, string> $properties |
| 43 | + */ |
| 44 | + private static function generateStyle(array $properties): string |
| 45 | + { |
| 46 | + $style = []; |
| 47 | + foreach ($properties as $key => $value) { |
| 48 | + $style[] = sprintf('%s:%s;', $key, $value); |
| 49 | + } |
| 50 | + |
| 51 | + return implode(' ', $style); |
27 | 52 | } |
28 | 53 |
|
29 | | - private function isSupportedColor(string $color): bool |
| 54 | + private static function isSupportedColor(string $color): bool |
30 | 55 | { |
31 | 56 | return 1 === preg_match('/^#[0-9a-f]{6}$/iD', $color); |
32 | 57 | } |
33 | 58 |
|
34 | | - private function computeTextColor(string $bgColor): string |
| 59 | + private static function computeTextClass(string $backgroundColor): string |
35 | 60 | { |
36 | 61 | [$r, $g, $b] = [ |
37 | | - hexdec(substr($bgColor, 1, 2)), |
38 | | - hexdec(substr($bgColor, 3, 2)), |
39 | | - hexdec(substr($bgColor, 5, 2)), |
| 62 | + hexdec(substr($backgroundColor, 1, 2)), |
| 63 | + hexdec(substr($backgroundColor, 3, 2)), |
| 64 | + hexdec(substr($backgroundColor, 5, 2)), |
40 | 65 | ]; |
41 | 66 |
|
42 | 67 | $luminance = (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255; |
43 | 68 |
|
44 | | - return $luminance > 0.5 ? '#000000' : '#FFFFFF'; |
| 69 | + return $luminance > 0.5 ? 'text-dark' : 'text-light'; |
45 | 70 | } |
46 | 71 | } |
0 commit comments