1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace projectcleverweb\color; |
5
|
|
|
|
6
|
|
|
class regulate { |
7
|
|
|
|
8
|
|
|
public static function percent(float &$value) { |
9
|
|
|
$value = static::max(abs($value), 100); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public static function rgb(int &$value) { |
13
|
|
|
$value = static::max(abs($value), 256); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public static function rgb_array(array &$rgb_array) { |
17
|
|
|
static::standardize_array($rgb_array); |
18
|
|
|
$rgb_array += ['r' => 0, 'g' => 0, 'b' => 0]; |
19
|
|
|
static::rgb($rgb_array['r']); |
20
|
|
|
static::rgb($rgb_array['g']); |
21
|
|
|
static::rgb($rgb_array['b']); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function hsl(float &$value, string $offset) { |
25
|
|
|
if (strtolower($offset) == 'h') { |
26
|
|
|
$value = static::max(abs($value), 359); |
27
|
|
|
} |
28
|
|
|
static::percent($value); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function hsl_array(array &$hsl_array) { |
32
|
|
|
static::standardize_array($hsl_array); |
33
|
|
|
$hsl_array += ['h' => 0, 's' => 0, 'l' => 0]; |
34
|
|
|
static::hsl($hsl_array['h'], 'h'); |
35
|
|
|
static::hsl($hsl_array['s'], 's'); |
36
|
|
|
static::hsl($hsl_array['l'], 'l'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function cmyk(float &$value) { |
40
|
|
|
static::percent($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function cmyk_array(array &$cmyk_array) { |
44
|
|
|
static::standardize_array($cmyk_array); |
45
|
|
|
$hsl_array += ['c' => 0, 'm' => 0, 'y' => 0, 'k' => 0]; |
|
|
|
|
46
|
|
|
static::cmyk($cmyk_array['c']); |
47
|
|
|
static::cmyk($cmyk_array['m']); |
48
|
|
|
static::cmyk($cmyk_array['y']); |
49
|
|
|
static::cmyk($cmyk_array['k']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function hex(string &$color) { |
53
|
|
|
static::_validate_hex_str($color); |
54
|
|
|
static::_strip_hash($color); |
55
|
|
|
static::_expand_shorthand($color); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function hex_int(int &$value) { |
59
|
|
|
$value = static::max(abs($value), 0x1FFFFFF); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function _strip_hash(&$hex) { |
63
|
|
|
if (is_string($hex) && substr($hex, 0 ,1) == '#') { |
64
|
|
|
$hex = substr($hex, 1); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function _expand_shorthand(string &$hex_str) { |
69
|
|
|
if (strlen($hex_str) === 3) { |
70
|
|
|
$r = $hex_str[0]; |
|
|
|
|
71
|
|
|
$g = $hex_str[1]; |
|
|
|
|
72
|
|
|
$b = $hex_str[2]; |
|
|
|
|
73
|
|
|
$hex_str = $r.$r.$g.$g.$b.$b; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function _validate_hex_str(&$hex_str) { |
78
|
|
|
if (is_string($hex_str) && preg_match('/\A#?(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})\Z/i', $hex_str)) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
// Error - Force color and trigger notice |
82
|
|
|
$hex_str = '000000'; |
83
|
|
|
// [todo] Trigger Error |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected static function standardize_array(array &$array) { |
87
|
|
|
$array = array_change_key_case($array); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected static function max(float $number, float $max) :float { |
91
|
|
|
if ($number > $max) { |
92
|
|
|
$number -= floor($number / $max) * $max; |
93
|
|
|
} |
94
|
|
|
return $number; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
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.