1 | <?php |
||
23 | class StylesheetTwigExtension extends AbstractTwigExtension { |
||
24 | |||
25 | /** |
||
26 | * Service name. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | const SERVICE_NAME = "wbw.core.twig.extension.stylesheet"; |
||
31 | |||
32 | /** |
||
33 | * Displays a rgba(). |
||
34 | * |
||
35 | * @param string $color The hexadecimal color. |
||
36 | * @param float $alpha The alpha channel. |
||
37 | * @return string Returns the rgba(). |
||
38 | */ |
||
39 | public function cssRgba($color, $alpha = 1.00) { |
||
40 | |||
41 | if (0 === preg_match_all("/^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/", strtolower($color), $hex)) { |
||
42 | return ""; |
||
43 | } |
||
44 | |||
45 | $length = strlen($hex[1][0]); |
||
46 | |||
47 | $pattern = "/[A-Fa-f0-9]{" . intval($length / 3) . "}/"; |
||
48 | preg_match_all($pattern, $hex[1][0], $channels); |
||
49 | |||
50 | if (3 === $length) { |
||
51 | $channels[0][0] = str_repeat($channels[0][0], 2); |
||
52 | $channels[0][1] = str_repeat($channels[0][1], 2); |
||
53 | $channels[0][2] = str_repeat($channels[0][2], 2); |
||
54 | } |
||
55 | |||
56 | $template = "rgba(%r%, %g%, %b%, %a%)"; |
||
57 | |||
58 | return str_replace(["%r%", "%g%", "%b%", "%a%"], [hexdec($channels[0][0]), hexdec($channels[0][1]), hexdec($channels[0][2]), number_format($alpha, 2)], $template); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get the Twig filters. |
||
63 | * |
||
64 | * @return TwigFilter[] Returns the Twig filters. |
||
65 | */ |
||
66 | public function getFilters() { |
||
71 | |||
72 | /** |
||
73 | * Get the Twig functions. |
||
74 | * |
||
75 | * @return TwigFunction[] Returns the Twig functions. |
||
76 | */ |
||
77 | public function getFunctions() { |
||
82 | } |
||
83 |