88
99class ColorBuilder implements ProtectedContextAwareInterface
1010{
11- const PATTERN_HEX_LONG = '/^#? (?<red>[0-9abcdefABCDEF ]{2})(?<green>[0-9abcdefABCDEF ]{2})(?<blue>[0-9abcdefABCDEF ]{2})(?:(?<alpha>[0-9abcdefABCDEF ]{2}))?^$/u ' ;
11+ const PATTERN_HEX_LONG = '/^#(?<red>[0-9abcdef ]{2})(?<green>[0-9abcdef ]{2})(?<blue>[0-9abcdef ]{2})(?:(?<alpha>[0-9abcdef ]{2}))?$/ ' ;
1212
13- const PATTERN_HEX_SHORT = '/#? (?<red>[0-9abcdefABCDEF ]{1})(?<green>[0-9abcdefABCDEF ]{1})(?<blue>[0-9abcdefABCDEF ]{1})(?:(?<alpha>[0-9abcdefABCDEF ]{1}))?$/u ' ;
13+ const PATTERN_HEX_SHORT = '/^# (?<red>[0-9abcdef ]{1})(?<green>[0-9abcdef ]{1})(?<blue>[0-9abcdef ]{1})(?:(?<alpha>[0-9abcdef ]{1}))?$/u ' ;
1414
1515 const PATTERN_RGBA = '/^rgba? \\s* \\( \\s*(?<red>[0-9 \\.]+%?) \\s*,? \\s*(?<green>[0-9 \\.]+%?) \\s*,? \\s*(?<blue>[0-9 \\.]+%?) \\s*(?:,? \\s*(?<alpha>[0-9 \\.]+%?) \\s*)? \\)$/u ' ;
1616
17- const PATTERN_HSLA = '/hsla? \\s* \\( \\s*(?<hue>[0-9 \\.]+) \\s*,? \\s*(?<saturation>[0-9 \\.]+%) \\s*,? \\s*(?<lightness>[0-9 \\.]+%) \\s*(?:,? \\s*(?<alpha>[0-9 \\.]+%?) \\s*)? \\)$/u ' ;
17+ const PATTERN_HSLA = '/^ hsla? \\s* \\( \\s*(?<hue>[0-9 \\.]+) \\s*,? \\s*(?<saturation>[0-9 \\.]+%) \\s*,? \\s*(?<lightness>[0-9 \\.]+%) \\s*(?:,? \\s*(?<alpha>[0-9 \\.]+%?) \\s*)? \\)$/u ' ;
1818
1919 /**
2020 * @param int $red 0-255
@@ -35,14 +35,13 @@ public function rgb(int $red, int $green, int $blue, int $alpha = 255): ColorHel
3535 * @param int $hue 0-355
3636 * @param int $saturatiom 0-100
3737 * @param int $lightness 0-100
38- * @param int $alpha 0-100
38+ * @param float $alpha 0-1
3939 *
4040 * @return ColorHelper
4141 */
42- public function hsl (int $ hue , int $ saturatiom , int $ lightness , int $ alpha = 100 ): ColorHelper
42+ public function hsl (int $ hue , int $ saturatiom , int $ lightness , float $ alpha = 1 ): ColorHelper
4343 {
44- $ alpha = (int ) round ($ alpha / 100 * 255 );
45-
44+ $ alpha = (int ) round ($ alpha * 255 );
4645 return new ColorHelper (
4746 new HslaColor ($ hue , $ saturatiom , $ lightness , $ alpha )
4847 );
@@ -55,7 +54,9 @@ public function hsl(int $hue, int $saturatiom, int $lightness, int $alpha = 100)
5554 */
5655 public function hex (string $ hex ): ?ColorHelper
5756 {
57+ $ hex = strtolower ($ hex );
5858 if (preg_match (self ::PATTERN_HEX_SHORT , $ hex , $ matches )) {
59+
5960 $ red = (int ) hexdec ($ matches ['red ' ].$ matches ['red ' ]);
6061 $ green = (int ) hexdec ($ matches ['green ' ].$ matches ['green ' ]);
6162 $ blue = (int ) hexdec ($ matches ['blue ' ].$ matches ['blue ' ]);
@@ -65,6 +66,7 @@ public function hex(string $hex): ?ColorHelper
6566 new RgbaColor ($ red , $ green , $ blue , $ alpha )
6667 );
6768 } elseif (preg_match (self ::PATTERN_HEX_LONG , $ hex , $ matches )) {
69+
6870 $ red = (int ) hexdec ($ matches ['red ' ]);
6971 $ green = (int ) hexdec ($ matches ['green ' ]);
7072 $ blue = (int ) hexdec ($ matches ['blue ' ]);
@@ -87,6 +89,7 @@ public function hex(string $hex): ?ColorHelper
8789 */
8890 public function css (string $ colorString ): ?ColorHelper
8991 {
92+ $ colorString = strtolower ($ colorString );
9093 if (preg_match (self ::PATTERN_HEX_SHORT , $ colorString , $ matches )) {
9194 $ red = (int ) hexdec ($ matches ['red ' ].$ matches ['red ' ]);
9295 $ green = (int ) hexdec ($ matches ['green ' ].$ matches ['green ' ]);
@@ -100,7 +103,7 @@ public function css(string $colorString): ?ColorHelper
100103 $ red = (int ) hexdec ($ matches ['red ' ]);
101104 $ green = (int ) hexdec ($ matches ['green ' ]);
102105 $ blue = (int ) hexdec ($ matches ['blue ' ]);
103- $ alpha = (int ) hexdec ($ matches ['alpha ' ] ? $ matches ['alpha ' ] : 'ff ' );
106+ $ alpha = (int ) hexdec (isset ( $ matches ['alpha ' ]) ? $ matches ['alpha ' ] : 'ff ' );
104107
105108 return new ColorHelper (
106109 new RgbaColor ($ red , $ green , $ blue , $ alpha )
0 commit comments