modifier.hex2rgba.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 
12 /**
13  * Convert a hex color value into rgba
14  *
15  * Example:
16  * @code
17  * {$hexColor|hex2rgba:"0.5"}
18  * @endcode
19  *
20  * @param $hexColor The color value in hex notation
21  * @param $alpha The alpha value (optional)
22  * @return String
23  */
24 function smarty_modifier_hex2rgba($hexColor, $alpha=null) {
25  $hexColor = str_replace("#", "", $hexColor);
26 
27  if(strlen($hexColor) == 3) {
28  $r = hexdec($hexColor[0].$hexColor[0]);
29  $g = hexdec($hexColor[1].$hexColor[1]);
30  $b = hexdec($hexColor[2].$hexColor[2]);
31  }
32  else {
33  $r = hexdec($hexColor[0].$hexColor[1]);
34  $g = hexdec($hexColor[2].$hexColor[3]);
35  $b = hexdec($hexColor[4].$hexColor[5]);
36  }
37  return 'rgba('.$r.', '.$g.', '.$b.', '.($alpha !== null ? $alpha : '1.0').')';
38 }
39 ?>