1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Main Interface For Library |
4
|
|
|
* ========================== |
5
|
|
|
* Provides a simplified interface so programming with colors can be relatively |
6
|
|
|
* quick & easy |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace projectcleverweb\color; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Main Interface For Library |
13
|
|
|
* ========================== |
14
|
|
|
* Provides a simplified interface so programming with colors can be relatively |
15
|
|
|
* quick & easy |
16
|
|
|
*/ |
17
|
|
|
class main extends main_peripheral { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Instance of color for this object |
21
|
|
|
* @var color |
22
|
|
|
*/ |
23
|
|
|
public $color; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Instance of cache for this object |
27
|
|
|
* @var cache |
28
|
|
|
*/ |
29
|
|
|
protected $cache; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Setup color and cache for this object |
33
|
|
|
* |
34
|
|
|
* @param mixed $color A color described as Hex string or int, RGB array, HSL array, HSB array, CMYK array, or instance of color |
35
|
|
|
* @param string $type The type of color to process $color as |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct($color, string $type = '') { |
38
|
1 |
|
$this->set($color, $type); |
39
|
1 |
|
$this->cache = new data\cache; |
|
|
|
|
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Configure the color instance for this object |
44
|
|
|
* |
45
|
|
|
* @param mixed $color A color described as Hex string or int, RGB array, HSL array, HSB array, CMYK array, or instance of color |
46
|
|
|
* @param string $type The type of color to process $color as |
47
|
|
|
*/ |
48
|
1 |
|
protected function set($color, string $type = '') { |
49
|
1 |
|
if (is_a($color, __CLASS__)) { |
50
|
|
|
$this->color = clone $color->color; |
51
|
|
|
} else { |
52
|
1 |
|
$this->color = new data\store($color, $type); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return the current color as an RGB array |
58
|
|
|
* |
59
|
|
|
* @return array RGB array |
60
|
|
|
*/ |
61
|
1 |
|
public function rgb() :array { |
62
|
1 |
|
return (array) $this->color->rgb + ['a' => $this->color->alpha()]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the current color as an HSL array |
67
|
|
|
* |
68
|
|
|
* @return array HSL array |
69
|
|
|
*/ |
70
|
1 |
|
public function hsl(int $accuracy = 0) :array { |
71
|
1 |
|
$color = []; |
72
|
1 |
|
foreach($this->color->hsl() as $key => $value) { |
73
|
1 |
|
$color[$key] = round($value, abs($accuracy)); |
74
|
|
|
} |
75
|
1 |
|
return $color + ['a' => $this->color->alpha()]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return the current color as an CMYK array |
80
|
|
|
* |
81
|
|
|
* @return array CMYK array |
82
|
|
|
*/ |
83
|
1 |
|
public function cmyk() :array { |
84
|
1 |
|
if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) { |
85
|
|
|
return $cached; |
86
|
|
|
} |
87
|
1 |
|
$rgb = $this->color->rgb; |
88
|
1 |
|
$result = convert\rgb::to_cmyk($rgb['r'], $rgb['g'], $rgb['b']) + ['a' => $this->color->alpha()]; |
|
|
|
|
89
|
1 |
|
$this->cache->set(__FUNCTION__, $this->hex(), $result); |
90
|
1 |
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the current color as an HSB array |
95
|
|
|
* |
96
|
|
|
* @return array HSB array |
97
|
|
|
*/ |
98
|
1 |
|
public function hsb(int $accuracy = 0) :array { |
99
|
1 |
|
if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) { |
100
|
|
|
return $cached; |
101
|
|
|
} |
102
|
1 |
|
$rgb = $this->color->rgb; |
103
|
1 |
|
$result = convert\rgb::to_hsb($rgb['r'], $rgb['g'], $rgb['b'], $accuracy) + ['a' => $this->color->alpha()]; |
|
|
|
|
104
|
1 |
|
$this->cache->set(__FUNCTION__, $this->hex(), $result); |
105
|
1 |
|
return $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return the current color as an hex string |
110
|
|
|
* |
111
|
|
|
* @return string hex string |
112
|
|
|
*/ |
113
|
1 |
|
public function hex() :string { |
114
|
1 |
|
return $this->color->hex; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return the current color as an web-safe hex string |
119
|
|
|
* |
120
|
|
|
* @return string web-safe hex string |
121
|
|
|
*/ |
122
|
1 |
View Code Duplication |
public function web_safe() :string { |
|
|
|
|
123
|
1 |
|
if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) { |
124
|
|
|
return $cached; |
125
|
|
|
} |
126
|
1 |
|
$rgb = $this->color->rgb; |
127
|
1 |
|
$result = generate::web_safe($rgb['r'], $rgb['g'], $rgb['b']); |
128
|
1 |
|
$this->cache->set(__FUNCTION__, $this->hex(), $result); |
129
|
1 |
|
return $result; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return the current color as an CSS value |
134
|
|
|
* |
135
|
|
|
* @return string CSS value |
136
|
|
|
*/ |
137
|
1 |
|
public function css() :string { |
138
|
1 |
|
return css::best($this->color); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get (and set) the alpha channel |
143
|
|
|
* |
144
|
|
|
* @param mixed $new_alpha If numeric, the alpha channel is set to this value |
145
|
|
|
* @return float The current alpha value |
146
|
|
|
*/ |
147
|
|
|
public function alpha($new_alpha = NULL) { |
148
|
|
|
return $this->color->alpha($new_alpha); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check if the current color would be considered visually dark |
153
|
|
|
* |
154
|
|
|
* @param int $check_score Minimum score to be considered light (0-255; defaults to 128) |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function is_dark(int $check_score = 128) :bool { |
|
|
|
|
158
|
|
|
if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) { |
159
|
|
|
return $cached; |
160
|
|
|
} |
161
|
|
|
$rgb = $this->color->rgb; |
162
|
|
|
$result = check::is_dark($rgb['r'], $rgb['g'], $rgb['b'], $check_score); |
163
|
|
|
$this->cache->set(__FUNCTION__, $this->hex(), $result); |
164
|
|
|
return $result; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Adjust the red value of the current color |
169
|
|
|
* |
170
|
|
|
* @param float $adjustment The amount to adjust the color by |
171
|
|
|
* @param bool $as_percentage Whether to treat $amount as a percentage |
172
|
|
|
* @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function red(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
176
|
|
|
if ($adjustment == 0.0) { |
177
|
|
|
return $this->color->rgb['r']; |
178
|
|
|
} |
179
|
|
|
return modify::rgb($this->color, 'red', $adjustment, $as_percentage, $set_absolute); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Adjust the green value of the current color |
184
|
|
|
* |
185
|
|
|
* @param float $adjustment The amount to adjust the color by |
186
|
|
|
* @param bool $as_percentage Whether to treat $amount as a percentage |
187
|
|
|
* @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
public function green(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
191
|
|
|
if ($adjustment == 0.0) { |
192
|
|
|
return $this->color->rgb['g']; |
193
|
|
|
} |
194
|
|
|
return modify::rgb($this->color, 'green', $adjustment, $as_percentage, $set_absolute); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Adjust the blue value of the current color |
199
|
|
|
* |
200
|
|
|
* @param float $adjustment The amount to adjust the color by |
201
|
|
|
* @param bool $as_percentage Whether to treat $amount as a percentage |
202
|
|
|
* @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public function blue(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
206
|
|
|
if ($adjustment == 0.0) { |
207
|
|
|
return $this->color->rgb['b']; |
208
|
|
|
} |
209
|
|
|
return modify::rgb($this->color, 'blue', $adjustment, $as_percentage, $set_absolute); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Adjust the hue value of the current color |
214
|
|
|
* |
215
|
|
|
* @param float $adjustment The amount to adjust the color by |
216
|
|
|
* @param bool $as_percentage Whether to treat $amount as a percentage |
217
|
|
|
* @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function hue(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
221
|
|
|
if ($adjustment == 0.0) { |
222
|
|
|
return $this->color->hsl['h']; |
223
|
|
|
} |
224
|
|
|
return modify::hsl($this->color, 'hue', $adjustment, $as_percentage, $set_absolute); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Adjust the saturation value of the current color |
229
|
|
|
* |
230
|
|
|
* @param float $adjustment The amount to adjust the color by |
231
|
|
|
* @param bool $as_percentage Whether to treat $amount as a percentage |
232
|
|
|
* @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
public function saturation(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
236
|
|
|
if ($adjustment == 0.0) { |
237
|
|
|
return $this->color->hsl['s']; |
238
|
|
|
} |
239
|
|
|
return modify::hsl($this->color, 'saturation', $adjustment, $as_percentage, $set_absolute); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Adjust the light value of the current color |
244
|
|
|
* |
245
|
|
|
* @param float $adjustment The amount to adjust the color by |
246
|
|
|
* @param bool $as_percentage Whether to treat $amount as a percentage |
247
|
|
|
* @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
248
|
|
|
* @return void |
249
|
|
|
*/ |
250
|
|
|
public function light(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
251
|
|
|
if ($adjustment == 0.0) { |
252
|
|
|
return $this->color->hsl['l']; |
253
|
|
|
} |
254
|
|
|
return modify::hsl($this->color, 'light', $adjustment, $as_percentage, $set_absolute); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Blend 2 colors together to generate a new color. |
259
|
|
|
* |
260
|
|
|
* @param main $color2 The second color to blend with this color |
261
|
|
|
* @param float $amount The amount as a percentage of the second color to add to the first |
262
|
|
|
* @return main The resulting color as an instance of main. |
263
|
|
|
*/ |
264
|
|
|
public function blend(main $color2, float $amount = 50.0) :main { |
265
|
|
|
$c1 = $this->rgb(); |
266
|
|
|
$c2 = $color2->rgb(); |
267
|
|
|
return new $this(generate::blend( |
268
|
|
|
$c1['r'], $c1['g'], $c1['b'], $c1['a'], |
269
|
|
|
$c2['r'], $c2['g'], $c2['b'], $c2['a'], |
270
|
|
|
$amount |
271
|
|
|
)); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Create a gradient of colors as an array |
276
|
|
|
* |
277
|
|
|
* @param main $color2 The "end" gradient. (The current color is the start) |
278
|
|
|
* @param int $steps The number of steps to have in the gradient. 0 will make it generate one step for every unique RGB color in the gradient. |
279
|
|
|
* @return array The resulting gradient as an array |
280
|
|
|
*/ |
281
|
|
|
public function gradient(main $color2, int $steps = 0, string $return_type = 'rgb', string $mode = 'rgb') :array { |
282
|
|
|
$c1 = $this->rgb(); |
283
|
|
|
$c2 = $color2->rgb(); |
284
|
|
|
return generate::gradient_range( |
285
|
|
|
$c1['r'], $c1['g'], $c1['b'], |
286
|
|
|
$c2['r'], $c2['g'], $c2['b'], |
287
|
|
|
$steps |
288
|
|
|
); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Generate a color scheme based on the current color. |
293
|
|
|
* |
294
|
|
|
* Available scheme algorithms: |
295
|
|
|
* - analogous |
296
|
|
|
* - complementary |
297
|
|
|
* - compound |
298
|
|
|
* - monochromatic |
299
|
|
|
* - shades |
300
|
|
|
* - tetrad |
301
|
|
|
* - weighted_tetrad |
302
|
|
|
* - triad |
303
|
|
|
* - weighted_triad |
304
|
|
|
* - rectangular |
305
|
|
|
* |
306
|
|
|
* @param string $scheme_name The scheme algorithm to use |
307
|
|
|
* @param string $return_type The type of values the scheme will have (hex, rgb, hsl, hsb, cmyk) |
308
|
|
|
* @return array The resulting scheme, where offset 0 is the original color |
309
|
|
|
*/ |
310
|
|
|
public function scheme(string $scheme_name, string $return_type = 'hex') :array { |
311
|
|
|
return static::get_scheme($scheme_name, $return_type, new scheme); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Generate a color scheme, with YIQ weights, based on the current color. |
316
|
|
|
* |
317
|
|
|
* Available scheme algorithms: |
318
|
|
|
* - analogous |
319
|
|
|
* - complementary |
320
|
|
|
* - compound |
321
|
|
|
* - monochromatic |
322
|
|
|
* - shades |
323
|
|
|
* - tetrad |
324
|
|
|
* - weighted_tetrad |
325
|
|
|
* - triad |
326
|
|
|
* - weighted_triad |
327
|
|
|
* - rectangular |
328
|
|
|
* |
329
|
|
|
* @param string $scheme_name The scheme algorithm to use |
330
|
|
|
* @param string $return_type The type of values the scheme will have (hex, rgb, hsl, hsb, cmyk) |
331
|
|
|
* @return array The resulting YIQ scheme, where offset 0 is the original color |
332
|
|
|
*/ |
333
|
|
|
public function yiq_scheme(string $scheme_name, string $return_type = 'hex') :array { |
334
|
|
|
return parent::get_scheme($scheme_name, $return_type, new yiq_scheme); |
|
|
|
|
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Generate a new instance of main with a random RGB color |
339
|
|
|
* |
340
|
|
|
* @param int $min_r Minimum red value allowed |
341
|
|
|
* @param int $max_r Maximum red value allowed |
342
|
|
|
* @param int $min_g Minimum green value allowed |
343
|
|
|
* @param int $max_g Maximum green value allowed |
344
|
|
|
* @param int $min_b Minimum blue value allowed |
345
|
|
|
* @param int $max_b Maximum blue value allowed |
346
|
|
|
* @return main The resulting instance of main |
347
|
|
|
*/ |
348
|
|
|
public function rgb_rand(int $min_r = 0, int $max_r = 255, int $min_g = 0, int $max_g = 255, int $min_b = 0, int $max_b = 255) :main { |
349
|
|
|
return new main(generate::rgb_rand($min_r, $max_r, $min_g, $max_g, $min_b, $max_b)); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Generate a new instance of main with a random HSL color |
354
|
|
|
* |
355
|
|
|
* @param int $min_h Minimum hue value allowed |
356
|
|
|
* @param int $max_h Maximum hue value allowed |
357
|
|
|
* @param int $min_s Minimum saturation value allowed |
358
|
|
|
* @param int $max_s Maximum saturation value allowed |
359
|
|
|
* @param int $min_l Minimum light value allowed |
360
|
|
|
* @param int $max_l Maximum light value allowed |
361
|
|
|
* @return main The resulting instance of main |
362
|
|
|
*/ |
363
|
|
|
public function hsl_rand(int $min_h = 0, int $max_h = 255, int $min_s = 0, int $max_s = 255, int $min_l = 0, int $max_l = 255) :main { |
364
|
|
|
return new main(generate::hsl_rand($min_h, $max_h, $min_s, $max_s, $min_l, $max_l)); |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..