This repository was archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.php
More file actions
655 lines (546 loc) · 18.3 KB
/
colors.php
File metadata and controls
655 lines (546 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
<?php
/**
* Author: Arlo Carreon <http://arlocarreon.com>
* Info: http://mexitek.github.io/phpColors/
* License: http://arlo.mit-license.org/
*/
namespace Mexitek\PHPColors;
use \Exception;
/**
* A color utility that helps manipulate HEX colors
*/
class Color {
private $_hex;
private $_hsl;
private $_rgb;
/**
* Auto darkens/lightens by 10% for sexily-subtle gradients.
* Set this to FALSE to adjust automatic shade to be between given color
* and black (for darken) or white (for lighten)
*/
const DEFAULT_ADJUST = 10;
/**
* Instantiates the class with a HEX value
* @param string $hex
* @throws Exception "Bad color format"
*/
function __construct( $hex ) {
// Strip # sign is present
$color = str_replace("#", "", $hex);
// Make sure it's 6 digits
if( strlen($color) === 3 ) {
$color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2];
} else if( strlen($color) != 6 ) {
throw new Exception("HEX color needs to be 6 or 3 digits long");
}
$this->_hsl = self::hexToHsl( $color );
$this->_hex = $color;
$this->_rgb = self::hexToRgb( $color );
}
// ====================
// = Public Interface =
// ====================
/**
* Given a HEX string returns a HSL array equivalent.
* @param string $color
* @return array HSL associative array
*/
public static function hexToHsl( $color ){
// Sanity check
$color = self::_checkHex($color);
// Convert HEX to DEC
$R = hexdec($color[0].$color[1]);
$G = hexdec($color[2].$color[3]);
$B = hexdec($color[4].$color[5]);
$HSL = array();
$var_R = ($R / 255);
$var_G = ($G / 255);
$var_B = ($B / 255);
$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;
$L = ($var_Max + $var_Min)/2;
if ($del_Max == 0)
{
$H = 0;
$S = 0;
}
else
{
if ( $L < 0.5 ) $S = $del_Max / ( $var_Max + $var_Min );
else $S = $del_Max / ( 2 - $var_Max - $var_Min );
$del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
if ($var_R == $var_Max) $H = $del_B - $del_G;
else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;
if ($H<0) $H++;
if ($H>1) $H--;
}
$HSL['H'] = ($H*360);
$HSL['S'] = $S;
$HSL['L'] = $L;
return $HSL;
}
/**
* Given a HSL associative array returns the equivalent HEX string
* @param array $hsl
* @return string HEX string
* @throws Exception "Bad HSL Array"
*/
public static function hslToHex( $hsl = array() ){
// Make sure it's HSL
if(empty($hsl) || !isset($hsl["H"]) || !isset($hsl["S"]) || !isset($hsl["L"]) ) {
throw new Exception("Param was not an HSL array");
}
list($H,$S,$L) = array( $hsl['H']/360,$hsl['S'],$hsl['L'] );
if( $S == 0 ) {
$r = $L * 255;
$g = $L * 255;
$b = $L * 255;
} else {
if($L<0.5) {
$var_2 = $L*(1+$S);
} else {
$var_2 = ($L+$S) - ($S*$L);
}
$var_1 = 2 * $L - $var_2;
$r = round(255 * self::_huetorgb( $var_1, $var_2, $H + (1/3) ));
$g = round(255 * self::_huetorgb( $var_1, $var_2, $H ));
$b = round(255 * self::_huetorgb( $var_1, $var_2, $H - (1/3) ));
}
// Convert to hex
$r = dechex($r);
$g = dechex($g);
$b = dechex($b);
// Make sure we get 2 digits for decimals
$r = (strlen("".$r)===1) ? "0".$r:$r;
$g = (strlen("".$g)===1) ? "0".$g:$g;
$b = (strlen("".$b)===1) ? "0".$b:$b;
return $r.$g.$b;
}
/**
* Given a HEX string returns a RGB array equivalent.
* @param string $color
* @return array RGB associative array
*/
public static function hexToRgb( $color ){
// Sanity check
$color = self::_checkHex($color);
// Convert HEX to DEC
$R = hexdec($color[0].$color[1]);
$G = hexdec($color[2].$color[3]);
$B = hexdec($color[4].$color[5]);
$RGB['R'] = $R;
$RGB['G'] = $G;
$RGB['B'] = $B;
return $RGB;
}
/**
* Given an RGB associative array returns the equivalent HEX string
* @param array $rgb
* @return string RGB string
* @throws Exception "Bad RGB Array"
*/
public static function rgbToHex( $rgb = array() ){
// Make sure it's RGB
if(empty($rgb) || !isset($rgb["R"]) || !isset($rgb["G"]) || !isset($rgb["B"]) ) {
throw new Exception("Param was not an RGB array");
}
// https://github.com/mexitek/phpColors/issues/25#issuecomment-88354815
// Convert RGB to HEX
$hex[0] = str_pad(dechex($rgb['R']), 2, '0', STR_PAD_LEFT);
$hex[1] = str_pad(dechex($rgb['G']), 2, '0', STR_PAD_LEFT);
$hex[2] = str_pad(dechex($rgb['B']), 2, '0', STR_PAD_LEFT);
return implode( '', $hex );
}
/**
* Given a HEX value, returns a darker color. If no desired amount provided, then the color halfway between
* given HEX and black will be returned.
* @param int $amount
* @return string Darker HEX value
*/
public function darken( $amount = self::DEFAULT_ADJUST ){
// Darken
$darkerHSL = $this->_darken($this->_hsl, $amount);
// Return as HEX
return self::hslToHex($darkerHSL);
}
/**
* Given a HEX value, returns a lighter color. If no desired amount provided, then the color halfway between
* given HEX and white will be returned.
* @param int $amount
* @return string Lighter HEX value
*/
public function lighten( $amount = self::DEFAULT_ADJUST ){
// Lighten
$lighterHSL = $this->_lighten($this->_hsl, $amount);
// Return as HEX
return self::hslToHex($lighterHSL);
}
/**
* Given a HEX value, returns a mixed color. If no desired amount provided, then the color mixed by this ratio
* @param string $hex2 Secondary HEX value to mix with
* @param int $amount = -100..0..+100
* @return string mixed HEX value
*/
public function mix($hex2, $amount = 0){
$rgb2 = self::hexToRgb($hex2);
$mixed = $this->_mix($this->_rgb, $rgb2, $amount);
// Return as HEX
return self::rgbToHex($mixed);
}
/**
* Creates an array with two shades that can be used to make a gradient
* @param int $amount Optional percentage amount you want your contrast color
* @return array An array with a 'light' and 'dark' index
*/
public function makeGradient( $amount = self::DEFAULT_ADJUST ) {
// Decide which color needs to be made
if( $this->isLight() ) {
$lightColor = $this->_hex;
$darkColor = $this->darken($amount);
} else {
$lightColor = $this->lighten($amount);
$darkColor = $this->_hex;
}
// Return our gradient array
return array( "light" => $lightColor, "dark" => $darkColor );
}
/**
* Returns whether or not given color is considered "light"
* @param string|Boolean $color
* @param int $lighterThan
* @return boolean
*/
public function isLight( $color = FALSE, $lighterThan = 130 ){
// Get our color
$color = ($color) ? $color : $this->_hex;
// Calculate straight from rbg
$r = hexdec($color[0].$color[1]);
$g = hexdec($color[2].$color[3]);
$b = hexdec($color[4].$color[5]);
return (( $r*299 + $g*587 + $b*114 )/1000 > $lighterThan);
}
/**
* Returns whether or not a given color is considered "dark"
* @param string|Boolean $color
* @param int $darkerThan
* @return boolean
*/
public function isDark( $color = FALSE, $darkerThan = 130 ){
// Get our color
$color = ($color) ? $color:$this->_hex;
// Calculate straight from rbg
$r = hexdec($color[0].$color[1]);
$g = hexdec($color[2].$color[3]);
$b = hexdec($color[4].$color[5]);
return (( $r*299 + $g*587 + $b*114 )/1000 <= $darkerThan);
}
/**
* Returns the complimentary color
* @return string Complementary hex color
*
*/
public function complementary() {
// Get our HSL
$hsl = $this->_hsl;
// Adjust Hue 180 degrees
$hsl['H'] += ($hsl['H']>180) ? -180:180;
// Return the new value in HEX
return self::hslToHex($hsl);
}
/**
* Returns your color's HSL array
*/
public function getHsl() {
return $this->_hsl;
}
/**
* Returns your original color
*/
public function getHex() {
return $this->_hex;
}
/**
* Returns your color's RGB array
*/
public function getRgb() {
return $this->_rgb;
}
/**
* Returns the cross browser CSS3 gradient
* @param int $amount Optional: percentage amount to light/darken the gradient
* @param boolean $vintageBrowsers Optional: include vendor prefixes for browsers that almost died out already
* @param string $prefix Optional: prefix for every lines
* @param string $suffix Optional: suffix for every lines
* @link http://caniuse.com/css-gradients Resource for the browser support
* @return string CSS3 gradient for chrome, safari, firefox, opera and IE10
*/
public function getCssGradient( $amount = self::DEFAULT_ADJUST, $vintageBrowsers = FALSE, $suffix = "" , $prefix = "" ) {
// Get the recommended gradient
$g = $this->makeGradient($amount);
$css = "";
/* fallback/image non-cover color */
$css .= "{$prefix}background-color: #".$this->_hex.";{$suffix}";
/* IE Browsers */
$css .= "{$prefix}filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#".$g['light']."', endColorstr='#".$g['dark']."');{$suffix}";
/* Safari 4+, Chrome 1-9 */
if ( $vintageBrowsers ) {
$css .= "{$prefix}background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#".$g['light']."), to(#".$g['dark']."));{$suffix}";
}
/* Safari 5.1+, Mobile Safari, Chrome 10+ */
$css .= "{$prefix}background-image: -webkit-linear-gradient(top, #".$g['light'].", #".$g['dark'].");{$suffix}";
/* Firefox 3.6+ */
if ( $vintageBrowsers ) {
$css .= "{$prefix}background-image: -moz-linear-gradient(top, #".$g['light'].", #".$g['dark'].");{$suffix}";
}
/* Opera 11.10+ */
if ( $vintageBrowsers ) {
$css .= "{$prefix}background-image: -o-linear-gradient(top, #".$g['light'].", #".$g['dark'].");{$suffix}";
}
/* Unprefixed version (standards): FF 16+, IE10+, Chrome 26+, Safari 7+, Opera 12.1+ */
$css .= "{$prefix}background-image: linear-gradient(to bottom, #".$g['light'].", #".$g['dark'].");{$suffix}";
// Return our CSS
return $css;
}
// ===========================
// = Private Functions Below =
// ===========================
/**
* Darkens a given HSL array
* @param array $hsl
* @param int $amount
* @return array $hsl
*/
private function _darken( $hsl, $amount = self::DEFAULT_ADJUST){
// Check if we were provided a number
if( $amount ) {
$hsl['L'] = ($hsl['L'] * 100) - $amount;
$hsl['L'] = ($hsl['L'] < 0) ? 0:$hsl['L']/100;
} else {
// We need to find out how much to darken
$hsl['L'] = $hsl['L']/2 ;
}
return $hsl;
}
/**
* Lightens a given HSL array
* @param array $hsl
* @param int $amount
* @return array $hsl
*/
private function _lighten( $hsl, $amount = self::DEFAULT_ADJUST){
// Check if we were provided a number
if( $amount ) {
$hsl['L'] = ($hsl['L'] * 100) + $amount;
$hsl['L'] = ($hsl['L'] > 100) ? 1:$hsl['L']/100;
} else {
// We need to find out how much to lighten
$hsl['L'] += (1-$hsl['L'])/2;
}
return $hsl;
}
/**
* Mix 2 rgb colors and return an rgb color
* @param array $rgb1
* @param array $rgb2
* @param int $amount ranged -100..0..+100
* @return array $rgb
*
* ported from http://phpxref.pagelines.com/nav.html?includes/class.colors.php.source.html
*/
private function _mix($rgb1, $rgb2, $amount = 0) {
$r1 = ($amount + 100) / 100;
$r2 = 2 - $r1;
$rmix = (($rgb1['R'] * $r1) + ($rgb2['R'] * $r2)) / 2;
$gmix = (($rgb1['G'] * $r1) + ($rgb2['G'] * $r2)) / 2;
$bmix = (($rgb1['B'] * $r1) + ($rgb2['B'] * $r2)) / 2;
return array('R' => $rmix, 'G' => $gmix, 'B' => $bmix);
}
/**
* Given a Hue, returns corresponding RGB value
* @param int $v1
* @param int $v2
* @param int $vH
* @return int
*/
private static function _huetorgb( $v1,$v2,$vH ) {
if( $vH < 0 ) {
$vH += 1;
}
if( $vH > 1 ) {
$vH -= 1;
}
if( (6*$vH) < 1 ) {
return ($v1 + ($v2 - $v1) * 6 * $vH);
}
if( (2*$vH) < 1 ) {
return $v2;
}
if( (3*$vH) < 2 ) {
return ($v1 + ($v2-$v1) * ( (2/3)-$vH ) * 6);
}
return $v1;
}
/**
* You need to check if you were given a good hex string
* @param string $hex
* @return string Color
* @throws Exception "Bad color format"
*/
private static function _checkHex( $hex ) {
// Strip # sign is present
$color = str_replace("#", "", $hex);
// Make sure it's 6 digits
if( strlen($color) == 3 ) {
$color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2];
} else if( strlen($color) != 6 ) {
throw new Exception("HEX color needs to be 6 or 3 digits long");
}
return $color;
}
/**
* Converts object into its string representation
* @return string Color
*/
public function __toString() {
return "#".$this->getHex();
}
public function __get($name)
{
switch (strtolower($name))
{
case 'red':
case 'r':
return $this->_rgb["R"];
case 'green':
case 'g':
return $this->_rgb["G"];
case 'blue':
case 'b':
return $this->_rgb["B"];
case 'hue':
case 'h':
return $this->_hsl["H"];
case 'saturation':
case 's':
return $this->_hsl["S"];
case 'lightness':
case 'l':
return $this->_hsl["L"];
}
}
public function __set($name, $value)
{
switch (strtolower($name))
{
case 'red':
case 'r':
$this->_rgb["R"] = $value;
$this->_hex = $this->rgbToHex($this->_rgb);
$this->_hsl = $this->hexToHsl($this->_hex);
break;
case 'green':
case 'g':
$this->_rgb["G"] = $value;
$this->_hex = $this->rgbToHex($this->_rgb);
$this->_hsl = $this->hexToHsl($this->_hex);
break;
case 'blue':
case 'b':
$this->_rgb["B"] = $value;
$this->_hex = $this->rgbToHex($this->_rgb);
$this->_hsl = $this->hexToHsl($this->_hex);
break;
case 'hue':
case 'h':
$this->_hsl["H"] = $value;
$this->_hex = $this->hslToHex($this->_hsl);
$this->_rgb = $this->hexToRgb($this->_hex);
break;
case 'saturation':
case 's':
$this->_hsl["S"] = $value;
$this->_hex = $this->hslToHex($this->_hsl);
$this->_rgb = $this->hexToRgb($this->_hex);
break;
case 'lightness':
case 'light':
case 'l':
$this->_hsl["L"] = $value;
$this->_hex = $this->hslToHex($this->_hsl);
$this->_rgb = $this->hexToRgb($this->_hex);
break;
}
}
}
function hslToHex($hsl)
{
list($h, $s, $l) = $hsl;
if ($s == 0) {
$r = $g = $b = 1;
} else {
$q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s;
$p = 2 * $l - $q;
$r = hue2rgb($p, $q, $h + 1/3);
$g = hue2rgb($p, $q, $h);
$b = hue2rgb($p, $q, $h - 1/3);
}
return rgb2hex($r) . rgb2hex($g) . rgb2hex($b);
}
function hue2rgb($p, $q, $t) {
if ($t < 0) $t += 1;
if ($t > 1) $t -= 1;
if ($t < 1/6) return $p + ($q - $p) * 6 * $t;
if ($t < 1/2) return $q;
if ($t < 2/3) return $p + ($q - $p) * (2/3 - $t) * 6;
return $p;
}
function rgb2hex($rgb) {
return str_pad(dechex($rgb * 255), 2, '0', STR_PAD_LEFT);
}
function roundUpToAny($n,$x=5) {
return (round($n)%$x === 0) ? round($n) : round(($n+$x/2)/$x)*$x;
}
function percent($l) {
return roundUpToAny(round($l * 100));
}
function tints_shades($hsl) {
$hue = $hsl['H'];
$sat = percent($hsl['S']);
$lume = percent($hsl['L']);
$shade_counter = 100;
while ($shade_counter >= 0) {
$hex = hslToHex(array("H" => $hue, "S" => $sat, "L" => $shade_counter));
echo "<div style='background:{$hex}'>";
if($lume == $shade_counter) {
echo "<b>{$shade_counter}</b>";
}
else {
echo $shade_counter;
}
echo "</div>";
$shade_counter = $shade_counter - 5;
}
}
function randcolor($gray = false) {
if($gray == false) {
$h = rand(0, 360);
$s = rand(50,90);
if($s > 75) {
$l = rand(40,65);
}
else {
$l = rand(35,80);
}
}
else {
$h = 0;
$s = 0;
$l = rand(0, 85);
}
return "hsl({$h}, {$s}%,{$l}%)";
}
?>