@@ -8,28 +8,38 @@ final class BadgeStyle
88 * @param array<string> $classes
99 * @param array<string, string> $style
1010 */
11- private function __construct (private readonly array $ classes , private readonly array $ style )
11+ private function __construct (private array $ classes , private array $ style )
1212 {
1313 }
1414
15- public static function fromBgColor (string $ backgroundColor , ?string $ textColor = null ): self
15+ public static function new (): self
16+ {
17+ return new self ([], []);
18+ }
19+
20+ public function withBgColor (string $ backgroundColor , bool $ autoTextContrast = true ): self
1621 {
1722 if (!self ::isSupportedColor ($ backgroundColor )) {
1823 throw new \InvalidArgumentException (sprintf ('The background color must be a full 6-digit hexadecimal color ("%s" given). ' , $ backgroundColor ));
1924 }
2025
21- $ classes = [];
22- $ styleProperties = ['background-color ' => $ backgroundColor ];
26+ $ this ->style ['background-color ' ] = $ backgroundColor ;
27+ if ($ autoTextContrast ) {
28+ $ this ->classes [] = self ::generateTextClassFromBackgroundColor ($ backgroundColor );
29+ }
30+
31+ return $ this ;
32+ }
2333
24- if (null === $ textColor ) {
25- $ classes [] = self ::computeTextClass ($ backgroundColor );
26- } elseif (self ::isSupportedColor ($ textColor )) {
27- $ styleProperties ['color ' ] = $ textColor ;
28- } else {
34+ public function withTextColor (string $ textColor ): self
35+ {
36+ if (!self ::isSupportedColor ($ textColor )) {
2937 throw new \InvalidArgumentException (sprintf ('The text color must be a full 6-digit hexadecimal color ("%s" given). ' , $ textColor ));
3038 }
3139
32- return new self ($ classes , $ styleProperties );
40+ $ this ->style ['color ' ] = $ textColor ;
41+
42+ return $ this ;
3343 }
3444
3545 public function getClasses (): string
@@ -60,7 +70,7 @@ private static function isSupportedColor(string $color): bool
6070 return 1 === preg_match ('/^#[0-9a-f]{6}$/iD ' , $ color );
6171 }
6272
63- private static function computeTextClass (string $ backgroundColor ): string
73+ private static function generateTextClassFromBackgroundColor (string $ backgroundColor ): string
6474 {
6575 [$ r , $ g , $ b ] = [
6676 hexdec (substr ($ backgroundColor , 1 , 2 )),
0 commit comments