66class ColorHelper implements ProtectedContextAwareInterface
77{
88
9+ const PATTERN_HEX_LONG = '/^#?(?<red>[0-9abcdefABCDEF]{2})(?<green>[0-9abcdefABCDEF]{2})(?<blue>[0-9abcdefABCDEF]{2})(?:(?<alpha>[0-9abcdefABCDEF]{2}))?^$/u ' ;
10+
11+ const PATTERN_HEX_SHORT = '/#?(?<red>[0-9abcdefABCDEF]{1})(?<green>[0-9abcdefABCDEF]{1})(?<blue>[0-9abcdefABCDEF]{1})(?:(?<alpha>[0-9abcdefABCDEF]{1}))?$/u ' ;
12+
13+ 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 ' ;
14+
15+ 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 ' ;
16+
917 /**
10- * @param $red
11- * @param $green
12- * @param $blue
13- * @param $alpha
18+ * @param $red 0-255
19+ * @param $green 0-255
20+ * @param $blue 0-255
21+ * @param $alpha 0-255
1422 * @return Color
1523 */
16- public function rgb ($ red , $ green , $ blue , $ alpha = 1 ): Color
24+ public function rgb ($ red , $ green , $ blue , $ alpha = 255 ): Color
1725 {
1826 return Color::createFromRgb ($ red , $ green , $ blue , $ alpha );
1927 }
2028
2129 /**
22- * @param $hue
23- * @param $saturatiom
24- * @param $lightness
25- * @param $alpha
30+ * @param $hue 0-355
31+ * @param $saturatiom 0-100
32+ * @param $lightness 0-100
33+ * @param $alpha 0-100
2634 * @return Color
2735 */
28- public function hsl ($ hue , $ saturatiom , $ lightness , $ alpha = 1 ): Color
36+ public function hsl ($ hue , $ saturatiom , $ lightness , $ alpha = 100 ): Color
2937 {
3038 return Color::createFromHSL ($ hue , $ saturatiom , $ lightness , $ alpha );
3139 }
@@ -39,6 +47,59 @@ public function hex($hex): Color
3947 return Color::createFromHex ($ hex );
4048 }
4149
50+ /**
51+ * @param string $colorString
52+ * @return Color
53+ * @throws \Exception
54+ */
55+ public function css (string $ colorString ): Color
56+ {
57+ if (preg_match (self ::PATTERN_HEX_SHORT , $ colorString , $ matches )) {
58+ $ red = hexdec ($ matches ['red ' ].$ matches ['red ' ]);
59+ $ green = hexdec ($ matches ['green ' ].$ matches ['green ' ]);
60+ $ blue = hexdec ($ matches ['blue ' ].$ matches ['blue ' ]);
61+ $ alpha = hexdec (isset ($ matches ['alpha ' ]) ? $ matches ['alpha ' ] . $ matches ['alpha ' ] : 1.0 );
62+ return Color::createFromRgb ($ red , $ green , $ blue , $ alpha );
63+ } elseif (preg_match (self ::PATTERN_HEX_LONG , $ colorString , $ matches )) {
64+ $ red = hexdec ($ matches ['red ' ]);
65+ $ green = hexdec ($ matches ['green ' ]);
66+ $ blue = hexdec ($ matches ['blue ' ]);
67+ $ alpha = hexdec ($ matches ['alpha ' ] ?? 1 );
68+ return Color::createFromRgb ($ red , $ green , $ blue , $ alpha );
69+ } elseif (preg_match (self ::PATTERN_RGBA , $ colorString , $ matches )) {
70+ $ red = $ this ->parseNumber ($ matches ['red ' ]);
71+ $ green = $ this ->parseNumber ($ matches ['red ' ]);
72+ $ blue = $ this ->parseNumber ($ matches ['red ' ]);
73+ $ alpha = $ this ->parseNumber ($ matches ['alpha ' ] ?? 1 , 1 );
74+ return Color::createFromRgb ($ red , $ green , $ blue , $ alpha );
75+ } elseif (preg_match (self ::PATTERN_HSLA , $ colorString , $ matches )) {
76+ $ hue = $ this ->parseNumber ($ matches ['hue ' ], 355 );
77+ $ saturation = $ this ->parseNumber ($ matches ['saturation ' ], 100 );
78+ $ lightness = $ this ->parseNumber ($ matches ['lightness ' ], 100 );
79+ $ alpha = $ this ->parseNumber ($ matches ['alpha ' ] ?? 1 , 1 );
80+ return Color::createFromHSL ($ hue , $ saturation , $ lightness , $ alpha );
81+ } else {
82+ return Color::createFromRgb (0 ,0 ,0 ,1 );
83+ }
84+ }
85+
86+ /**
87+ * @param string $value
88+ * @param int $max
89+ * @return float
90+ */
91+ protected function parseNumber (string $ value , int $ max = 255 ): float
92+ {
93+ if (substr ($ value , -1 ) == '% ' ) {
94+ $ number = (int )(
95+ substr ($ value , 0 , -1 )
96+ );
97+ return $ max * ($ number / 100 );
98+ } else {
99+ return (float ) $ value ;
100+ }
101+ }
102+
42103 /**
43104 * @param string $methodName
44105 * @return bool
0 commit comments