|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plugin Name: ariColor |
|
4
|
|
|
* Plugin URI: http://aristath.github.io/ariColor/ |
|
5
|
|
|
* Description: A PHP library for color manipulation in WordPress themes and plugins |
|
6
|
|
|
* Author: Aristeides Stathopoulos |
|
7
|
|
|
* Author URI: http://aristeides.com |
|
8
|
|
|
* Version: 1.0 |
|
9
|
|
|
* Text Domain: aricolor |
|
10
|
|
|
* |
|
11
|
|
|
* GitHub Plugin URI: aristath/ariColor |
|
12
|
|
|
* GitHub Plugin URI: https://github.com/aristath/ariColor |
|
13
|
|
|
* |
|
14
|
|
|
* |
|
15
|
|
|
* @package ariColor |
|
16
|
|
|
* @category Core |
|
17
|
|
|
* @author Aristeides Stathopoulos |
|
18
|
|
|
* @copyright Copyright (c) 2016, Aristeides Stathopoulos |
|
19
|
|
|
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
// Exit if accessed directly. |
|
24
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
25
|
|
|
exit; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if ( ! class_exists( 'ariColor' ) ) { |
|
29
|
|
|
|
|
30
|
|
|
class ariColor { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Each color is a separate instance of this class. |
|
34
|
|
|
* Each instance is stored as an object in this array. |
|
35
|
|
|
* Allows easier access & performance. |
|
36
|
|
|
* |
|
37
|
|
|
* @static |
|
38
|
|
|
* @access public |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
public static $instances = array(); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The color as defined in the input |
|
45
|
|
|
* |
|
46
|
|
|
* @access public |
|
47
|
|
|
* @var string|array |
|
48
|
|
|
*/ |
|
49
|
|
|
public $color; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The mode of this color (hex/rgb/rgba etc.). |
|
53
|
|
|
* |
|
54
|
|
|
* @access public |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
public $mode = 'hex'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* An array of word-defined colors. |
|
61
|
|
|
* Example: white = #ffffff |
|
62
|
|
|
* |
|
63
|
|
|
* @access public |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
public $word_colors = array(); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The HEX value of the color. |
|
70
|
|
|
* |
|
71
|
|
|
* @access public |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
public $hex; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @access public |
|
78
|
|
|
* @var int|double |
|
79
|
|
|
*/ |
|
80
|
|
|
public $red = 0; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @access public |
|
84
|
|
|
* @var int|double |
|
85
|
|
|
*/ |
|
86
|
|
|
public $green = 0; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @access public |
|
90
|
|
|
* @var int|double |
|
91
|
|
|
*/ |
|
92
|
|
|
public $blue = 0; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @access public |
|
96
|
|
|
* @var int|float |
|
97
|
|
|
*/ |
|
98
|
|
|
public $alpha = 1; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @access public |
|
102
|
|
|
* @var int|float |
|
103
|
|
|
*/ |
|
104
|
|
|
public $hue; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @access public |
|
108
|
|
|
* @var int|float |
|
109
|
|
|
*/ |
|
110
|
|
|
public $saturation; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @access public |
|
114
|
|
|
* @var int|float |
|
115
|
|
|
*/ |
|
116
|
|
|
public $lightness; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @access public |
|
120
|
|
|
* @var int|float |
|
121
|
|
|
*/ |
|
122
|
|
|
public $chroma; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @access public |
|
126
|
|
|
* @var array |
|
127
|
|
|
*/ |
|
128
|
|
|
public $brightness = array(); |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @access public |
|
132
|
|
|
* @var int|float |
|
133
|
|
|
*/ |
|
134
|
|
|
public $luminance; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* The class constructor |
|
138
|
|
|
* |
|
139
|
|
|
* @param $color string|array The defined color. |
|
140
|
|
|
* @param $mode string Color mode. Set to 'auto' if you want this auto-detected. |
|
141
|
|
|
*/ |
|
142
|
|
|
private function __construct( $color = '', $mode = 'auto' ) { |
|
143
|
|
|
$this->color = $color; |
|
144
|
|
|
if ( ! method_exists( $this, 'from_' . $mode ) ) { |
|
145
|
|
|
$mode = $this->get_mode( $color ); |
|
146
|
|
|
} |
|
147
|
|
|
if ( null === $mode ) { |
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
$this->mode = $mode; |
|
151
|
|
|
$method = 'from_' . $mode; |
|
152
|
|
|
// call the from_{$color_mode} method |
|
153
|
|
|
$this->$method(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Gets an instance for this color. |
|
158
|
|
|
* We use a separate instance per color |
|
159
|
|
|
* because there's no need to create a completely new instance each time we call this class. |
|
160
|
|
|
* Instead using instances helps us improve performance & footprint. |
|
161
|
|
|
* |
|
162
|
|
|
* @param $color string|array |
|
163
|
|
|
* @param $mode string |
|
164
|
|
|
* |
|
165
|
|
|
* @return ariColor (object) |
|
166
|
|
|
*/ |
|
167
|
|
|
public static function newColor( $color, $mode = 'auto' ) { |
|
168
|
|
|
// get an md5 for this color |
|
169
|
|
|
$color_md5 = ( is_array( $color ) ) ? md5( json_encode( $color ) . $mode ) : md5( $color . $mode ); |
|
170
|
|
|
// Set the instance if it does not already exist. |
|
171
|
|
|
if ( ! isset( self::$instances[ $color_md5 ] ) ) { |
|
172
|
|
|
self::$instances[ $color_md5 ] = new self( $color, $mode ); |
|
173
|
|
|
} |
|
174
|
|
|
return self::$instances[ $color_md5 ]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Allows us to get a new instance by modifying a property of the existing one. |
|
179
|
|
|
* |
|
180
|
|
|
* @param $property string can be one of the following: |
|
181
|
|
|
* red, |
|
182
|
|
|
* green, |
|
183
|
|
|
* blue, |
|
184
|
|
|
* alpha, |
|
185
|
|
|
* hue, |
|
186
|
|
|
* saturation, |
|
187
|
|
|
* lightness, |
|
188
|
|
|
* brightness |
|
189
|
|
|
* @param $value int|float|string the new value |
|
190
|
|
|
* |
|
191
|
|
|
* @return ariColor|null |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getNew( $property = '', $value = '' ) { |
|
194
|
|
|
// Check if we're changing any of the rgba values |
|
195
|
|
|
if ( in_array( $property, array( 'red', 'green', 'blue', 'alpha' ) ) ) { |
|
196
|
|
|
$this->$property = $value; |
|
197
|
|
|
$this->red = max( 0, min( 255, $this->red ) ); |
|
198
|
|
|
$this->green = max( 0, min( 255, $this->green ) ); |
|
199
|
|
|
$this->blue = max( 0, min( 255, $this->blue ) ); |
|
200
|
|
|
$this->alpha = max( 0, min( 255, $this->alpha ) ); |
|
201
|
|
|
return self::newColor( 'rgba(' . $this->red . ',' . $this->green . ',' . $this->blue . ',' . $this->alpha . ')', 'rgba' ); |
|
202
|
|
|
} |
|
203
|
|
|
// Check if we're changing any of the hsl values |
|
204
|
|
|
elseif ( in_array( $property, array( 'hue', 'saturation', 'lightness' ) ) ) { |
|
205
|
|
|
$this->$property = $value; |
|
206
|
|
|
$this->hue = max( 0, min( 360, $this->hue ) ); |
|
207
|
|
|
$this->saturation = max( 0, min( 100, $this->saturation ) ); |
|
208
|
|
|
$this->lightness = max( 0, min( 100, $this->lightness ) ); |
|
209
|
|
|
return self::newColor( 'hsla(' . $this->hue . ',' . $this->saturation . '%,' . $this->lightness . '%,' . $this->alpha . ')', 'hsla' ); |
|
210
|
|
|
} |
|
211
|
|
|
// Check if we're changing the brightness |
|
212
|
|
|
elseif ( 'brightness' == $property ) { |
|
213
|
|
|
if ( $value < $this->brightness['total'] ) { |
|
214
|
|
|
$this->red = max( 0, min( 255, $this->red - ( $this->brightness['total'] - $value ) ) ); |
|
215
|
|
|
$this->green = max( 0, min( 255, $this->green - ( $this->brightness['total'] - $value ) ) ); |
|
216
|
|
|
$this->blue = max( 0, min( 255, $this->blue - ( $this->brightness['total'] - $value ) ) ); |
|
217
|
|
|
} elseif ( $value > $this->brightness['total'] ) { |
|
218
|
|
|
$this->red = max( 0, min( 255, $this->red + ( $value - $this->brightness['total'] ) ) ); |
|
219
|
|
|
$this->green = max( 0, min( 255, $this->green + ( $value - $this->brightness['total'] ) ) ); |
|
220
|
|
|
$this->blue = max( 0, min( 255, $this->blue + ( $value - $this->brightness['total'] ) ) ); |
|
221
|
|
|
} else { |
|
222
|
|
|
// if it's not smaller and it's not greater, then it's equal. |
|
223
|
|
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
return self::newColor( 'rgba(' . $this->red . ',' . $this->green . ',' . $this->blue . ',' . $this->alpha . ')' ); |
|
226
|
|
|
} |
|
227
|
|
|
return null; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Figure out what mode we're using. |
|
232
|
|
|
* |
|
233
|
|
|
* @param string|array |
|
234
|
|
|
* @param string $color |
|
235
|
|
|
* |
|
236
|
|
|
* @return string|null |
|
237
|
|
|
*/ |
|
238
|
|
|
public function get_mode( $color ) { |
|
239
|
|
|
// Check if value is an array |
|
240
|
|
|
if ( is_array( $color ) ) { |
|
241
|
|
|
// does the array have an 'rgba' key? |
|
242
|
|
|
if ( isset( $color['rgba'] ) ) { |
|
243
|
|
|
$this->color = $color['rgba']; |
|
244
|
|
|
return 'rgba'; |
|
245
|
|
|
} |
|
246
|
|
|
// Does the array have a 'color' key? |
|
247
|
|
|
elseif ( isset( $color['color'] ) ) { |
|
248
|
|
|
$this->color = $color['color']; |
|
249
|
|
|
return 'hex'; |
|
250
|
|
|
} |
|
251
|
|
|
// is this a simple array with 4 items? |
|
252
|
|
|
if ( 4 == count( $color ) && isset( $color[0] ) && isset( $color[1] ) && isset( $color[2] ) && isset( $color[3] ) ) { |
|
253
|
|
|
$this->color = 'rgba(' . intval( $color[0] ) . ',' . intval( $color[1] ) . ',' . intval( $color[2] ) . ',' . intval( $color[3] ) . ')'; |
|
254
|
|
|
return 'rgba'; |
|
255
|
|
|
} |
|
256
|
|
|
// Is this a simple array with 3 items? |
|
257
|
|
|
elseif ( 3 == count( $color ) && isset( $color[0] ) && isset( $color[1] ) && isset( $color[2] ) ) { |
|
258
|
|
|
$this->color = 'rgba(' . intval( $color[0] ) . ',' . intval( $color[1] ) . ',' . intval( $color[2] ) . ',' . '1)'; |
|
259
|
|
|
return 'rgba'; |
|
260
|
|
|
} |
|
261
|
|
|
// Check for other keys in the array and get values from there |
|
262
|
|
|
$finders_keepers = array( |
|
263
|
|
|
'r' => 'red', |
|
264
|
|
|
'g' => 'green', |
|
265
|
|
|
'b' => 'blue', |
|
266
|
|
|
'a' => 'alpha', |
|
267
|
|
|
'red' => 'red', |
|
268
|
|
|
'green' => 'green', |
|
269
|
|
|
'blue' => 'blue', |
|
270
|
|
|
'alpha' => 'alpha', |
|
271
|
|
|
'opacity' => 'alpha', |
|
272
|
|
|
); |
|
273
|
|
|
$found = false; |
|
274
|
|
|
foreach ( $finders_keepers as $finder => $keeper ) { |
|
275
|
|
|
if ( isset( $color[ $finder ] ) ) { |
|
276
|
|
|
$found = true; |
|
277
|
|
|
$this->$keeper = $color[ $finder ]; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
// We failed, return null. |
|
281
|
|
|
if ( ! $found ) { |
|
282
|
|
|
return null; |
|
283
|
|
|
} |
|
284
|
|
|
// We did not fail, so use rgba values recovered above. |
|
285
|
|
|
$this->color = 'rgba(' . $this->red . ',' . $this->green . ',' . $this->blue . ',' . $this->alpha . ')'; |
|
286
|
|
|
return 'rgba'; |
|
287
|
|
|
} |
|
288
|
|
|
// If we got this far, it's not an array. |
|
289
|
|
|
|
|
290
|
|
|
// Check for key identifiers in the value |
|
291
|
|
|
$finders_keepers = array( |
|
292
|
|
|
'#' => 'hex', |
|
293
|
|
|
'rgba' => 'rgba', |
|
294
|
|
|
'rgb' => 'rgb', |
|
295
|
|
|
'hsla' => 'hsla', |
|
296
|
|
|
'hsl' => 'hsl', |
|
297
|
|
|
); |
|
298
|
|
|
foreach ( $finders_keepers as $finder => $keeper ) { |
|
299
|
|
|
if ( false !== strrpos( $color, $finder ) ) { |
|
300
|
|
|
return $keeper; |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
// Perhaps we're using a word like "orange"? |
|
304
|
|
|
$wordcolors = $this->get_word_colors(); |
|
305
|
|
|
if ( array_key_exists( $color, $wordcolors ) ) { |
|
306
|
|
|
$this->color = '#' . $wordcolors[ $color ]; |
|
307
|
|
|
return 'hex'; |
|
308
|
|
|
} |
|
309
|
|
|
// fallback to hex. |
|
310
|
|
|
return 'hex'; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Starts with a HEX color and calculates all other properties. |
|
315
|
|
|
* |
|
316
|
|
|
* @return void |
|
317
|
|
|
*/ |
|
318
|
|
|
private function from_hex() { |
|
319
|
|
|
|
|
320
|
|
|
if ( ! function_exists( 'sanitize_hex_color' ) ) { |
|
321
|
|
|
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; |
|
322
|
|
|
} |
|
323
|
|
|
// Is this perhaps a word-color? |
|
324
|
|
|
$word_colors = $this->get_word_colors(); |
|
325
|
|
|
if ( array_key_exists( $this->color, $word_colors ) ) { |
|
326
|
|
|
$this->color = '#' . $word_colors[ $this->color ]; |
|
327
|
|
|
} |
|
328
|
|
|
// Sanitize color |
|
329
|
|
|
$this->hex = sanitize_hex_color( maybe_hash_hex_color( $this->color ) ); |
|
330
|
|
|
$hex = ltrim( $this->hex, '#' ); |
|
331
|
|
|
// Make sure we have 6 digits for the below calculations |
|
332
|
|
|
if ( 3 == strlen( $hex ) ) { |
|
333
|
|
|
$hex = ltrim( $this->hex, '#' ); |
|
334
|
|
|
$hex = substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) . substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) . substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ); |
|
335
|
|
|
} |
|
336
|
|
|
// Set red, green, blue |
|
337
|
|
|
$this->red = hexdec( substr( $hex, 0, 2 ) ); |
|
338
|
|
|
$this->green = hexdec( substr( $hex, 2, 2 ) ); |
|
339
|
|
|
$this->blue = hexdec( substr( $hex, 4, 2 ) ); |
|
340
|
|
|
$this->alpha = 1; |
|
341
|
|
|
// set other color properties |
|
342
|
|
|
$this->set_brightness(); |
|
343
|
|
|
$this->set_hsl(); |
|
344
|
|
|
$this->set_luminance(); |
|
345
|
|
|
|
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Starts with an RGB color and calculates all other properties. |
|
350
|
|
|
* |
|
351
|
|
|
* @return void |
|
352
|
|
|
*/ |
|
353
|
|
|
private function from_rgb() { |
|
354
|
|
|
$value = explode( ',', str_replace( array( ' ', 'rgb', '(', ')' ), '', $this->color ) ); |
|
355
|
|
|
// set red, green, blue |
|
356
|
|
|
$this->red = ( isset( $value[0] ) ) ? intval( $value[0] ) : 255; |
|
357
|
|
|
$this->green = ( isset( $value[1] ) ) ? intval( $value[1] ) : 255; |
|
358
|
|
|
$this->blue = ( isset( $value[2] ) ) ? intval( $value[2] ) : 255; |
|
359
|
|
|
$this->alpha = 1; |
|
360
|
|
|
// set the hex |
|
361
|
|
|
$this->hex = $this->rgb_to_hex( $this->red, $this->green, $this->blue ); |
|
362
|
|
|
// set other color properties |
|
363
|
|
|
$this->set_brightness(); |
|
364
|
|
|
$this->set_hsl(); |
|
365
|
|
|
$this->set_luminance(); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Starts with an RGBA color and calculates all other properties. |
|
370
|
|
|
* |
|
371
|
|
|
* @return void |
|
372
|
|
|
*/ |
|
373
|
|
|
private function from_rgba() { |
|
374
|
|
|
// Set r, g, b, a properties |
|
375
|
|
|
$value = explode( ',', str_replace( array( ' ', 'rgba', '(', ')' ), '', $this->color ) ); |
|
376
|
|
|
$this->red = ( isset( $value[0] ) ) ? intval( $value[0] ) : 255; |
|
377
|
|
|
$this->green = ( isset( $value[1] ) ) ? intval( $value[1] ) : 255; |
|
378
|
|
|
$this->blue = ( isset( $value[2] ) ) ? intval( $value[2] ) : 255; |
|
379
|
|
|
$this->alpha = ( isset( $value[3] ) ) ? filter_var( $value[3], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : 1; |
|
380
|
|
|
// limit values in the range of 0 - 255 |
|
381
|
|
|
$this->red = max( 0, min( 255, $this->red ) ); |
|
382
|
|
|
$this->green = max( 0, min( 255, $this->green ) ); |
|
383
|
|
|
$this->blue = max( 0, min( 255, $this->blue ) ); |
|
384
|
|
|
// limit values 0 - 1 |
|
385
|
|
|
$this->alpha = max( 0, min( 1, $this->alpha ) ); |
|
386
|
|
|
// set hex |
|
387
|
|
|
$this->hex = $this->rgb_to_hex( $this->red, $this->green, $this->blue ); |
|
388
|
|
|
// set other color properties |
|
389
|
|
|
$this->set_brightness(); |
|
390
|
|
|
$this->set_hsl(); |
|
391
|
|
|
$this->set_luminance(); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* Starts with an HSL color and calculates all other properties. |
|
396
|
|
|
* |
|
397
|
|
|
* @return void |
|
398
|
|
|
*/ |
|
399
|
|
|
private function from_hsl() { |
|
400
|
|
|
$value = explode( ',', str_replace( array( ' ', 'hsl', '(', ')', '%' ), '', $this->color ) ); |
|
401
|
|
|
$this->hue = $value[0]; |
|
402
|
|
|
$this->saturation = $value[1]; |
|
403
|
|
|
$this->lightness = $value[2]; |
|
404
|
|
|
$this->from_hsl_array(); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* Starts with an HSLA color and calculates all other properties. |
|
409
|
|
|
* |
|
410
|
|
|
* @return void |
|
411
|
|
|
*/ |
|
412
|
|
|
private function from_hsla() { |
|
413
|
|
|
$value = explode( ',', str_replace( array( ' ', 'hsla', '(', ')', '%' ), '', $this->color ) ); |
|
414
|
|
|
$this->hue = $value[0]; |
|
415
|
|
|
$this->saturation = $value[1]; |
|
416
|
|
|
$this->lightness = $value[2]; |
|
417
|
|
|
$this->alpha = $value[3]; |
|
418
|
|
|
$this->from_hsl_array(); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Generates the HEX value of a color given values for $red, $green, $blue |
|
423
|
|
|
* |
|
424
|
|
|
* @param $red int|string |
|
425
|
|
|
* @param $green int|string |
|
426
|
|
|
* @param $blue int|string |
|
427
|
|
|
* |
|
428
|
|
|
* @return string |
|
429
|
|
|
*/ |
|
430
|
|
|
private function rgb_to_hex( $red, $green, $blue ) { |
|
431
|
|
|
// get hex values properly formatted |
|
432
|
|
|
$hex_red = $this->dexhex_double_digit( $red ); |
|
433
|
|
|
$hex_green = $this->dexhex_double_digit( $green ); |
|
434
|
|
|
$hex_blue = $this->dexhex_double_digit( $blue ); |
|
435
|
|
|
return '#' . $hex_red . $hex_green . $hex_blue; |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* Convert a decimal value to hex and make sure it's 2 characters |
|
440
|
|
|
* |
|
441
|
|
|
* @param $value int|string |
|
442
|
|
|
* |
|
443
|
|
|
* @return string |
|
444
|
|
|
*/ |
|
445
|
|
|
private function dexhex_double_digit( $value ) { |
|
446
|
|
|
$value = ( 9 >= $value ) ? '0' . $value : dechex( $value ); |
|
447
|
|
|
if ( 1 == strlen( $value ) ) { |
|
448
|
|
|
$value .= $value; |
|
449
|
|
|
} |
|
450
|
|
|
return $value; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* Calculates the red, green, blue values of an HSL color |
|
455
|
|
|
* @see https://gist.github.com/brandonheyer/5254516 |
|
456
|
|
|
*/ |
|
457
|
|
|
private function from_hsl_array() { |
|
458
|
|
|
$h = $this->hue / 360; |
|
459
|
|
|
$s = $this->saturation / 100; |
|
460
|
|
|
$l = $this->lightness / 100; |
|
461
|
|
|
|
|
462
|
|
|
$r = $l; |
|
463
|
|
|
$g = $l; |
|
464
|
|
|
$b = $l; |
|
465
|
|
|
$v = ( $l <= 0.5 ) ? ( $l * ( 1.0 + $s ) ) : ( $l + $s - $l * $s ); |
|
466
|
|
|
if ( $v > 0 ) { |
|
467
|
|
|
$m = $l + $l - $v; |
|
468
|
|
|
$sv = ( $v - $m ) / $v; |
|
469
|
|
|
$h *= 6.0; |
|
470
|
|
|
$sextant = floor( $h ); |
|
471
|
|
|
$fract = $h - $sextant; |
|
472
|
|
|
$vsf = $v * $sv * $fract; |
|
473
|
|
|
$mid1 = $m + $vsf; |
|
474
|
|
|
$mid2 = $v - $vsf; |
|
475
|
|
|
switch ( $sextant ) { |
|
476
|
|
|
case 0: |
|
477
|
|
|
$r = $v; |
|
478
|
|
|
$g = $mid1; |
|
479
|
|
|
$b = $m; |
|
480
|
|
|
break; |
|
481
|
|
|
case 1: |
|
482
|
|
|
$r = $mid2; |
|
483
|
|
|
$g = $v; |
|
484
|
|
|
$b = $m; |
|
485
|
|
|
break; |
|
486
|
|
|
case 2: |
|
487
|
|
|
$r = $m; |
|
488
|
|
|
$g = $v; |
|
489
|
|
|
$b = $mid1; |
|
490
|
|
|
break; |
|
491
|
|
|
case 3: |
|
492
|
|
|
$r = $m; |
|
493
|
|
|
$g = $mid2; |
|
494
|
|
|
$b = $v; |
|
495
|
|
|
break; |
|
496
|
|
|
case 4: |
|
497
|
|
|
$r = $mid1; |
|
498
|
|
|
$g = $m; |
|
499
|
|
|
$b = $v; |
|
500
|
|
|
break; |
|
501
|
|
|
case 5: |
|
502
|
|
|
$r = $v; |
|
503
|
|
|
$g = $m; |
|
504
|
|
|
$b = $mid2; |
|
505
|
|
|
break; |
|
506
|
|
|
} |
|
507
|
|
|
} |
|
508
|
|
|
$this->red = round( $r * 255, 0 ); |
|
509
|
|
|
$this->green = round( $g * 255, 0 ); |
|
510
|
|
|
$this->blue = round( $b * 255, 0 ); |
|
511
|
|
|
|
|
512
|
|
|
$this->hex = $this->rgb_to_hex( $this->red, $this->green, $this->blue ); |
|
513
|
|
|
$this->set_luminance(); |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
/** |
|
517
|
|
|
* Returns a CSS-formatted value for colors. |
|
518
|
|
|
* |
|
519
|
|
|
* @param $mode string |
|
520
|
|
|
* @return string |
|
521
|
|
|
*/ |
|
522
|
|
|
public function toCSS( $mode = 'hex' ) { |
|
523
|
|
|
|
|
524
|
|
|
$value = ''; |
|
525
|
|
|
|
|
526
|
|
|
switch ( $mode ) { |
|
527
|
|
|
case 'hex': |
|
528
|
|
|
$value = strtoupper( $this->hex ); |
|
529
|
|
|
break; |
|
530
|
|
|
case 'rgba': |
|
531
|
|
|
$value = 'rgba(' . $this->red . ',' . $this->green . ',' . $this->blue . ',' . $this->alpha . ')'; |
|
532
|
|
|
break; |
|
533
|
|
|
case 'rgb': |
|
534
|
|
|
$value = 'rgb(' . $this->red . ',' . $this->green . ',' . $this->blue . ')'; |
|
535
|
|
|
break; |
|
536
|
|
|
case 'hsl': |
|
537
|
|
|
$value = 'hsl(' . $this->hue . ',' . round( $this->saturation ) . '%,' . round( $this->lightness ) . '%)'; |
|
538
|
|
|
break; |
|
539
|
|
|
case 'hsla': |
|
540
|
|
|
$value = 'hsla(' . $this->hue . ',' . round( $this->saturation ) . '%,' . round( $this->lightness ) . '%,' . $this->alpha . ')'; |
|
541
|
|
|
break; |
|
542
|
|
|
} |
|
543
|
|
|
return $value; |
|
544
|
|
|
} |
|
545
|
|
|
|
|
546
|
|
|
/** |
|
547
|
|
|
* Sets the HSL values of a color based on the values of red, green, blue |
|
548
|
|
|
*/ |
|
549
|
|
|
private function set_hsl() { |
|
550
|
|
|
$red = $this->red / 255; |
|
551
|
|
|
$green = $this->green / 255; |
|
552
|
|
|
$blue = $this->blue / 255; |
|
553
|
|
|
|
|
554
|
|
|
$max = max( $red, $green, $blue ); |
|
555
|
|
|
$min = min( $red, $green, $blue ); |
|
556
|
|
|
|
|
557
|
|
|
$lightness = ( $max + $min ) / 2; |
|
558
|
|
|
$difference = $max - $min; |
|
559
|
|
|
|
|
560
|
|
|
if ( 0 == $difference ) { |
|
561
|
|
|
$hue = $saturation = 0; // achromatic |
|
562
|
|
|
} else { |
|
563
|
|
|
$saturation = $difference / ( 1 - abs( 2 * $lightness - 1 ) ); |
|
564
|
|
|
switch ( $max ) { |
|
565
|
|
|
case $red: |
|
566
|
|
|
$hue = 60 * fmod( ( ( $green - $blue ) / $difference ), 6 ); |
|
567
|
|
|
if ( $blue > $green ) { |
|
568
|
|
|
$hue += 360; |
|
569
|
|
|
} |
|
570
|
|
|
break; |
|
571
|
|
|
case $green: |
|
572
|
|
|
$hue = 60 * ( ( $blue - $red ) / $difference + 2 ); |
|
573
|
|
|
break; |
|
574
|
|
|
case $blue: |
|
575
|
|
|
$hue = 60 * ( ( $red - $green ) / $difference + 4 ); |
|
576
|
|
|
break; |
|
577
|
|
|
} |
|
578
|
|
|
} |
|
579
|
|
|
|
|
580
|
|
|
$this->hue = round( $hue ); |
|
581
|
|
|
$this->saturation = round( $saturation * 100 ); |
|
582
|
|
|
$this->lightness = round( $lightness * 100 ); |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
/** |
|
586
|
|
|
* Sets the brightness of a color based on the values of red, green, blue |
|
587
|
|
|
*/ |
|
588
|
|
|
private function set_brightness() { |
|
589
|
|
|
$this->brightness = array( |
|
590
|
|
|
'red' => round( $this->red * .299 ), |
|
591
|
|
|
'green' => round( $this->green * .587 ), |
|
592
|
|
|
'blue' => round( $this->blue * .114 ), |
|
593
|
|
|
'total' => intval( ( $this->red * .299 ) + ( $this->green * .587 ) + ( $this->blue * .114 ) ) |
|
594
|
|
|
); |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
/** |
|
598
|
|
|
* Sets the luminance of a color (range:0-255) based on the values of red, green, blue |
|
599
|
|
|
*/ |
|
600
|
|
|
private function set_luminance() { |
|
601
|
|
|
$lum = ( 0.2126 * $this->red ) + ( 0.7152 * $this->green ) + ( 0.0722 * $this->blue ); |
|
602
|
|
|
$this->luminance = round( $lum ); |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
/** |
|
606
|
|
|
* Gets an array of all the wordcolors |
|
607
|
|
|
* |
|
608
|
|
|
* @return array |
|
609
|
|
|
*/ |
|
610
|
|
|
private function get_word_colors() { |
|
611
|
|
|
return array( |
|
612
|
|
|
'aliceblue' => 'F0F8FF', |
|
613
|
|
|
'antiquewhite' => 'FAEBD7', |
|
614
|
|
|
'aqua' => '00FFFF', |
|
615
|
|
|
'aquamarine' => '7FFFD4', |
|
616
|
|
|
'azure' => 'F0FFFF', |
|
617
|
|
|
'beige' => 'F5F5DC', |
|
618
|
|
|
'bisque' => 'FFE4C4', |
|
619
|
|
|
'black' => '000000', |
|
620
|
|
|
'blanchedalmond' => 'FFEBCD', |
|
621
|
|
|
'blue' => '0000FF', |
|
622
|
|
|
'blueviolet' => '8A2BE2', |
|
623
|
|
|
'brown' => 'A52A2A', |
|
624
|
|
|
'burlywood' => 'DEB887', |
|
625
|
|
|
'cadetblue' => '5F9EA0', |
|
626
|
|
|
'chartreuse' => '7FFF00', |
|
627
|
|
|
'chocolate' => 'D2691E', |
|
628
|
|
|
'coral' => 'FF7F50', |
|
629
|
|
|
'cornflowerblue' => '6495ED', |
|
630
|
|
|
'cornsilk' => 'FFF8DC', |
|
631
|
|
|
'crimson' => 'DC143C', |
|
632
|
|
|
'cyan' => '00FFFF', |
|
633
|
|
|
'darkblue' => '00008B', |
|
634
|
|
|
'darkcyan' => '008B8B', |
|
635
|
|
|
'darkgoldenrod' => 'B8860B', |
|
636
|
|
|
'darkgray' => 'A9A9A9', |
|
637
|
|
|
'darkgreen' => '006400', |
|
638
|
|
|
'darkgrey' => 'A9A9A9', |
|
639
|
|
|
'darkkhaki' => 'BDB76B', |
|
640
|
|
|
'darkmagenta' => '8B008B', |
|
641
|
|
|
'darkolivegreen' => '556B2F', |
|
642
|
|
|
'darkorange' => 'FF8C00', |
|
643
|
|
|
'darkorchid' => '9932CC', |
|
644
|
|
|
'darkred' => '8B0000', |
|
645
|
|
|
'darksalmon' => 'E9967A', |
|
646
|
|
|
'darkseagreen' => '8FBC8F', |
|
647
|
|
|
'darkslateblue' => '483D8B', |
|
648
|
|
|
'darkslategray' => '2F4F4F', |
|
649
|
|
|
'darkslategrey' => '2F4F4F', |
|
650
|
|
|
'darkturquoise' => '00CED1', |
|
651
|
|
|
'darkviolet' => '9400D3', |
|
652
|
|
|
'deeppink' => 'FF1493', |
|
653
|
|
|
'deepskyblue' => '00BFFF', |
|
654
|
|
|
'dimgray' => '696969', |
|
655
|
|
|
'dimgrey' => '696969', |
|
656
|
|
|
'dodgerblue' => '1E90FF', |
|
657
|
|
|
'firebrick' => 'B22222', |
|
658
|
|
|
'floralwhite' => 'FFFAF0', |
|
659
|
|
|
'forestgreen' => '228B22', |
|
660
|
|
|
'fuchsia' => 'FF00FF', |
|
661
|
|
|
'gainsboro' => 'DCDCDC', |
|
662
|
|
|
'ghostwhite' => 'F8F8FF', |
|
663
|
|
|
'gold' => 'FFD700', |
|
664
|
|
|
'goldenrod' => 'DAA520', |
|
665
|
|
|
'gray' => '808080', |
|
666
|
|
|
'green' => '008000', |
|
667
|
|
|
'greenyellow' => 'ADFF2F', |
|
668
|
|
|
'grey' => '808080', |
|
669
|
|
|
'honeydew' => 'F0FFF0', |
|
670
|
|
|
'hotpink' => 'FF69B4', |
|
671
|
|
|
'indianred' => 'CD5C5C', |
|
672
|
|
|
'indigo' => '4B0082', |
|
673
|
|
|
'ivory' => 'FFFFF0', |
|
674
|
|
|
'khaki' => 'F0E68C', |
|
675
|
|
|
'lavender' => 'E6E6FA', |
|
676
|
|
|
'lavenderblush' => 'FFF0F5', |
|
677
|
|
|
'lawngreen' => '7CFC00', |
|
678
|
|
|
'lemonchiffon' => 'FFFACD', |
|
679
|
|
|
'lightblue' => 'ADD8E6', |
|
680
|
|
|
'lightcoral' => 'F08080', |
|
681
|
|
|
'lightcyan' => 'E0FFFF', |
|
682
|
|
|
'lightgoldenrodyellow' => 'FAFAD2', |
|
683
|
|
|
'lightgray' => 'D3D3D3', |
|
684
|
|
|
'lightgreen' => '90EE90', |
|
685
|
|
|
'lightgrey' => 'D3D3D3', |
|
686
|
|
|
'lightpink' => 'FFB6C1', |
|
687
|
|
|
'lightsalmon' => 'FFA07A', |
|
688
|
|
|
'lightseagreen' => '20B2AA', |
|
689
|
|
|
'lightskyblue' => '87CEFA', |
|
690
|
|
|
'lightslategray' => '778899', |
|
691
|
|
|
'lightslategrey' => '778899', |
|
692
|
|
|
'lightsteelblue' => 'B0C4DE', |
|
693
|
|
|
'lightyellow' => 'FFFFE0', |
|
694
|
|
|
'lime' => '00FF00', |
|
695
|
|
|
'limegreen' => '32CD32', |
|
696
|
|
|
'linen' => 'FAF0E6', |
|
697
|
|
|
'magenta' => 'FF00FF', |
|
698
|
|
|
'maroon' => '800000', |
|
699
|
|
|
'mediumaquamarine' => '66CDAA', |
|
700
|
|
|
'mediumblue' => '0000CD', |
|
701
|
|
|
'mediumorchid' => 'BA55D3', |
|
702
|
|
|
'mediumpurple' => '9370D0', |
|
703
|
|
|
'mediumseagreen' => '3CB371', |
|
704
|
|
|
'mediumslateblue' => '7B68EE', |
|
705
|
|
|
'mediumspringgreen' => '00FA9A', |
|
706
|
|
|
'mediumturquoise' => '48D1CC', |
|
707
|
|
|
'mediumvioletred' => 'C71585', |
|
708
|
|
|
'midnightblue' => '191970', |
|
709
|
|
|
'mintcream' => 'F5FFFA', |
|
710
|
|
|
'mistyrose' => 'FFE4E1', |
|
711
|
|
|
'moccasin' => 'FFE4B5', |
|
712
|
|
|
'navajowhite' => 'FFDEAD', |
|
713
|
|
|
'navy' => '000080', |
|
714
|
|
|
'oldlace' => 'FDF5E6', |
|
715
|
|
|
'olive' => '808000', |
|
716
|
|
|
'olivedrab' => '6B8E23', |
|
717
|
|
|
'orange' => 'FFA500', |
|
718
|
|
|
'orangered' => 'FF4500', |
|
719
|
|
|
'orchid' => 'DA70D6', |
|
720
|
|
|
'palegoldenrod' => 'EEE8AA', |
|
721
|
|
|
'palegreen' => '98FB98', |
|
722
|
|
|
'paleturquoise' => 'AFEEEE', |
|
723
|
|
|
'palevioletred' => 'DB7093', |
|
724
|
|
|
'papayawhip' => 'FFEFD5', |
|
725
|
|
|
'peachpuff' => 'FFDAB9', |
|
726
|
|
|
'peru' => 'CD853F', |
|
727
|
|
|
'pink' => 'FFC0CB', |
|
728
|
|
|
'plum' => 'DDA0DD', |
|
729
|
|
|
'powderblue' => 'B0E0E6', |
|
730
|
|
|
'purple' => '800080', |
|
731
|
|
|
'red' => 'FF0000', |
|
732
|
|
|
'rosybrown' => 'BC8F8F', |
|
733
|
|
|
'royalblue' => '4169E1', |
|
734
|
|
|
'saddlebrown' => '8B4513', |
|
735
|
|
|
'salmon' => 'FA8072', |
|
736
|
|
|
'sandybrown' => 'F4A460', |
|
737
|
|
|
'seagreen' => '2E8B57', |
|
738
|
|
|
'seashell' => 'FFF5EE', |
|
739
|
|
|
'sienna' => 'A0522D', |
|
740
|
|
|
'silver' => 'C0C0C0', |
|
741
|
|
|
'skyblue' => '87CEEB', |
|
742
|
|
|
'slateblue' => '6A5ACD', |
|
743
|
|
|
'slategray' => '708090', |
|
744
|
|
|
'slategrey' => '708090', |
|
745
|
|
|
'snow' => 'FFFAFA', |
|
746
|
|
|
'springgreen' => '00FF7F', |
|
747
|
|
|
'steelblue' => '4682B4', |
|
748
|
|
|
'tan' => 'D2B48C', |
|
749
|
|
|
'teal' => '008080', |
|
750
|
|
|
'thistle' => 'D8BFD8', |
|
751
|
|
|
'tomato' => 'FF6347', |
|
752
|
|
|
'turquoise' => '40E0D0', |
|
753
|
|
|
'violet' => 'EE82EE', |
|
754
|
|
|
'wheat' => 'F5DEB3', |
|
755
|
|
|
'white' => 'FFFFFF', |
|
756
|
|
|
'whitesmoke' => 'F5F5F5', |
|
757
|
|
|
'yellow' => 'FFFF00', |
|
758
|
|
|
'yellowgreen' => '9ACD32', |
|
759
|
|
|
); |
|
760
|
|
|
|
|
761
|
|
|
} |
|
762
|
|
|
|
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
} |
|
766
|
|
|
|