|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace projectcleverweb\color; |
|
5
|
|
|
|
|
6
|
|
|
class regulate { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Forces a float to be within the range of 0 and 100. |
|
10
|
|
|
* |
|
11
|
|
|
* @param float &$value The value to modify |
|
12
|
|
|
* @return void |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function percent(float &$value) { |
|
15
|
|
|
$value = static::max(abs($value), 100); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Forces a float to be within the range of 0 and 255. |
|
20
|
|
|
* |
|
21
|
|
|
* @param float &$value The value to modify |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function rgb(int &$value) { |
|
25
|
|
|
$value = static::max(abs($value), 256); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Forces and RGB array to have specific offsets, and max values. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array &$rgb_array The RGB array to modify |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function rgb_array(array &$rgb_array) { |
|
35
|
|
|
static::standardize_array($rgb_array, ['r', 'g', 'b']); |
|
36
|
|
|
static::rgb($rgb_array['r']); |
|
37
|
|
|
static::rgb($rgb_array['g']); |
|
38
|
|
|
static::rgb($rgb_array['b']); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Forces a float to be within the range of 0 and 100 or if the offset is |
|
43
|
|
|
* 'h' 0 and 359. |
|
44
|
|
|
* |
|
45
|
|
|
* @param float &$value The value to modify |
|
46
|
|
|
* @param string $offset The offset that is being modified |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function hsl(float &$value, string $offset) { |
|
50
|
|
|
if (strtolower($offset) == 'h') { |
|
51
|
|
|
$value = static::max(abs($value), 359); |
|
52
|
|
|
} |
|
53
|
|
|
static::percent($value); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Forces and HSL array to have specific offsets, and max values. |
|
58
|
|
|
* |
|
59
|
|
|
* @param array &$hsl_array The HSL array to modify |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function hsl_array(array &$hsl_array) { |
|
63
|
|
|
static::standardize_array($hsl_array, ['h', 's', 'l']); |
|
64
|
|
|
static::hsl($hsl_array['h'], 'h'); |
|
65
|
|
|
static::hsl($hsl_array['s'], 's'); |
|
66
|
|
|
static::hsl($hsl_array['l'], 'l'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Forces a float to be within the range of 0 and 100. |
|
71
|
|
|
* |
|
72
|
|
|
* @param float &$value The value to modify |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function cmyk(float &$value) { |
|
76
|
|
|
static::percent($value); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Forces a float to be within the range of 0 and 100. |
|
82
|
|
|
* |
|
83
|
|
|
* @param float &$value The value to modify |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function alpha(float &$value) { |
|
87
|
|
|
static::percent($value); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Forces and CMYK array to have specific offsets, and max values. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array &$cmyk_array The CMYK array to modify |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function cmyk_array(array &$cmyk_array) { |
|
97
|
|
|
static::standardize_array($cmyk_array, ['c', 'm', 'y', 'k']); |
|
98
|
|
|
static::cmyk($cmyk_array['c']); |
|
99
|
|
|
static::cmyk($cmyk_array['m']); |
|
100
|
|
|
static::cmyk($cmyk_array['y']); |
|
101
|
|
|
static::cmyk($cmyk_array['k']); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Forces hexadecimal strings to be valid, without a "#", and not to be |
|
106
|
|
|
* shorthand. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string &$color The color string to modify |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function hex(string &$color) { |
|
112
|
|
|
static::_validate_hex_str($color); |
|
113
|
|
|
static::_strip_hash($color); |
|
114
|
|
|
static::_expand_shorthand($color); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Forces hexadecimal integers to be within the range of 0x0 and 0xFFFFFF. |
|
119
|
|
|
* |
|
120
|
|
|
* @param int &$value The value to modify |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function hex_int(int &$value) { |
|
124
|
|
|
$value = static::max(abs($value), 0xFFFFFF); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Strips the hash mark (#) off the beginning of a sting if it has one. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string &$hex The hex string |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function _strip_hash(string &$hex) { |
|
134
|
|
|
if (is_string($hex) && substr($hex, 0 ,1) == '#') { |
|
135
|
|
|
$hex = substr($hex, 1); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Expand a hex strings in short notation (e.g. 000 => 000000) |
|
141
|
|
|
* |
|
142
|
|
|
* @param string &$hex_str The hex string to modify |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function _expand_shorthand(string &$hex_str) { |
|
146
|
|
|
if (strlen($hex_str) === 3) { |
|
147
|
|
|
$r = $hex_str[0]; |
|
|
|
|
|
|
148
|
|
|
$g = $hex_str[1]; |
|
|
|
|
|
|
149
|
|
|
$b = $hex_str[2]; |
|
|
|
|
|
|
150
|
|
|
$hex_str = $r.$r.$g.$g.$b.$b; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Verify that the hex string is actually a hex string. If not force it to |
|
156
|
|
|
* '000000' |
|
157
|
|
|
* |
|
158
|
|
|
* @param string &$hex_str The hex string to modify |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
|
|
public static function _validate_hex_str(string &$hex_str) { |
|
162
|
|
|
if (is_string($hex_str) && preg_match('/\A#?(?:[0-9a-f]{3}|[0-9a-f]{6})\Z/i', $hex_str)) { |
|
163
|
|
|
return; |
|
164
|
|
|
} |
|
165
|
|
|
error::call(sprintf( |
|
166
|
|
|
'The input of %s::%s() was not a valid hex string, forcing value to 000000', |
|
167
|
|
|
__CLASS__, |
|
168
|
|
|
__FUNCTION__ |
|
169
|
|
|
)); |
|
170
|
|
|
$hex_str = '000000'; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Force an array to have specific lower-case keys. If the array is an indexed |
|
175
|
|
|
* array and the keys are provided, convert it to an associative array. |
|
176
|
|
|
* |
|
177
|
|
|
* @param array &$array The array to be modified |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
protected static function standardize_array(array &$array, array $keys = []) { |
|
181
|
|
|
if (!empty($keys) && array_keys($array) === range(0, count($array) - 1) && count($array) == count($keys)) { |
|
182
|
|
|
$array = array_combine($keys, $array); |
|
183
|
|
|
} else { |
|
184
|
|
|
$array = array_change_key_case($array) + array_fill_keys($keys, 0); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Absolute and divide any number so it fits between 0 and $max |
|
190
|
|
|
* |
|
191
|
|
|
* @param float $number The orginal number |
|
192
|
|
|
* @param float $max The maximum value $number should be |
|
193
|
|
|
* @return float The rsulting number as a float |
|
194
|
|
|
*/ |
|
195
|
|
|
protected static function max(float $number, float $max) :float { |
|
196
|
|
|
$max = abs($max); |
|
197
|
|
|
if ($number > $max) { |
|
198
|
|
|
$number -= floor($number / $max) * $max; |
|
199
|
|
|
} |
|
200
|
|
|
return $number; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.