|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CSS Generator Class |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace projectcleverweb\color; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* CSS Generator Class |
|
10
|
|
|
*/ |
|
11
|
|
|
class css { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Force alpha value to be present where possible. |
|
15
|
|
|
* @var boolean |
|
16
|
|
|
*/ |
|
17
|
|
|
public static $force_alpha = FALSE; |
|
18
|
|
|
|
|
19
|
|
|
public static function get(string $input, $support_x11 = TRUE) { |
|
20
|
|
|
if (preg_match('/\A(?:(rgb|hsl)\((\d+|\d+\.(?:\d+)?|\.\d+),(\d+|\d+\.(?:\d+)?|\.\d+),(\d+|\d+\.(?:\d+)?|\.\d+)|(rgb|hsl)a\((\d+|\d+\.(?:\d+)?|\.\d+),(\d+|\d+\.(?:\d+)?|\.\d+),(\d+|\d+\.(?:\d+)?|\.\d+),((?:0|1)|(?:0|1)\.(?:\d+)?|\.\d+))\)\Z/i', $input, $matches)) { |
|
21
|
|
|
return static::format_matches($matches); |
|
22
|
|
|
} elseif ($support_x11 && $x11 = data\x11::get($input)) { |
|
23
|
|
|
return $x11; |
|
24
|
|
|
} |
|
25
|
|
|
return FALSE; |
|
26
|
1 |
|
} |
|
27
|
1 |
|
|
|
28
|
1 |
|
protected static function format_matches(array $matches) { |
|
29
|
1 |
|
$values = array_slice($watches, 1, 4); |
|
|
|
|
|
|
30
|
|
|
$keys = array_slice(str_split($matches[0].'a'), 0, count($values)); |
|
31
|
|
|
$color = array_combine($keys, $values); |
|
32
|
|
|
foreach ($color as $key => &$val) { |
|
33
|
|
|
$val = (float) filter_var($val, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_SCIENTIFIC | FILTER_FLAG_ALLOW_FRACTION); |
|
34
|
|
|
if ($key == 'a') { |
|
35
|
|
|
$val = $val * 100; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
return $color; |
|
39
|
|
|
} |
|
40
|
1 |
|
|
|
41
|
1 |
|
/** |
|
42
|
1 |
|
* Choose the best way to represent the color as a CSS value. Will use either |
|
43
|
|
|
* a hex or rgba value depending on the alpha value. |
|
44
|
1 |
|
* |
|
45
|
|
|
* @param mixed $color The color |
|
46
|
|
|
* @return string The CSS value |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function best($color) { |
|
49
|
|
|
static::_color_instance($color); |
|
50
|
|
|
if ($color->alpha() == 100 && !static::$force_alpha) { |
|
51
|
|
|
return static::hex($color->rgb['r'], $color->rgb['g'], $color->rgb['b']); |
|
52
|
|
|
} |
|
53
|
|
|
return static::rgb($color->rgb['r'], $color->rgb['g'], $color->rgb['b'], $color->alpha()); |
|
54
|
1 |
|
} |
|
55
|
1 |
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Force $color to be an instance of color |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed &$color The color |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
protected static function _color_instance(&$color) { |
|
63
|
|
|
if (!is_a($color, __CLASS__)) { |
|
64
|
|
|
$color = new color($color); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Convert and RGB value to a CSS hex string |
|
70
|
|
|
* |
|
71
|
|
|
* @param float $r The red value |
|
72
|
|
|
* @param float $g The green value |
|
73
|
|
|
* @param float $b The blue value |
|
74
|
|
|
* @return string The CSS string |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function hex(float $r, float $g, float $b) { |
|
77
|
|
|
return '#'.convert::rgb_to_hex($r, $g, $b); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Convert and RGB value to a CSS rgb or rgba string |
|
82
|
|
|
* |
|
83
|
|
|
* @param float $r The red value |
|
84
|
|
|
* @param float $g The green value |
|
85
|
|
|
* @param float $b The blue value |
|
86
|
|
|
* @return string The CSS string |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function rgb(float $r, float $g, float $b, float $a = 1.0) { |
|
89
|
|
|
if ($a == 1.0 && !static::$force_alpha) { |
|
90
|
|
|
return sprintf('rgb(%s,%s,%s)', $r, $g, $b); |
|
91
|
|
|
} |
|
92
|
|
|
return sprintf('rgba(%s,%s,%s,%s)', $r, $g, $b, $a / 100); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Convert and HSL value to a CSS hsl or hsla string |
|
97
|
|
|
* |
|
98
|
|
|
* @param float $h The hue value |
|
99
|
|
|
* @param float $s The saturation value |
|
100
|
|
|
* @param float $l The light value |
|
101
|
|
|
* @return string The CSS string |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function hsl(float $h, float $s, float $l, float $a = 1.0) { |
|
104
|
|
|
if ($a == 1.0 && !static::$force_alpha) { |
|
105
|
|
|
return sprintf('hsl(%s,%s%%,%s%%)', $h, $s, $l); |
|
106
|
|
|
} |
|
107
|
|
|
return sprintf('hsla(%s,%s%%,%s%%,%s)', $h, $s, $l, $a / 100); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.