-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVarColor.php
More file actions
229 lines (204 loc) · 4.83 KB
/
Copy pathVarColor.php
File metadata and controls
229 lines (204 loc) · 4.83 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
<?php
/**
* VarColor
*
* Turns any variable into a color - vars will yield the same color every time!
*
* @author Rich Jenks <rich@richjenks.com>
* @license MIT
*/
class VarColor {
// Base color vars
public $hue = 180;
public $saturation = 50;
public $lightness = 50;
// Return formats
const HEX = 0;
const RGB = 1;
const HSL = 2;
// Current return format
public $format = 0;
/**
* When you don't care about "theming" and just want a quick colour
* Color will have no relation to that of `color()`
*
* @param mixed $string Input
* @return string Hex color
*/
public function hex($string) {
if (!is_string($string)) $string = serialize($string);
$string = md5($string);
$string = substr($string, 0, 6);
return strtoupper($string);
}
/**
* Generates a color from current options
*
* @param mixed $string Variable to be used
* @return mixed Hex color string or array of parts
*/
public function color($string = '') {
// Check variables are valid before continuing
$this->check();
// Ensure input is string
if (!is_string($string)) $string = serialize($string);
/**
* MD5 the string, take the first 3 chars (hex value between 0–4095)
* convert hexadecimal to decimal integer then divide by 11.4066852368
* to get a float between 0-359. Round it down to nearest int for hue
*/
if ($string != '') {
$this->hue = md5($string);
$this->hue = substr($this->hue, 0, 3);
$this->hue = hexdec($this->hue);
$this->hue = $this->hue / 11.4066852368;
$this->hue = (int) round($this->hue);
}
// Return required color
return self::output();
}
/**
* Determines whether black or white has the most contrast with the given hex
* text set over a color should be black or white
* The "centre point" seems the most logical cutt-off, but in practice, leaning more torwards white is more readable
*
* @see https://24ways.org/2010/calculating-color-contrast/
*
* @param string $hex Color in hex format
* @return string 'black' or 'white'
*/
function contrast($hex){
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
$c = (($r*299)+($g*587)+($b*114))/1000;
// return ($c >= 128) ? 'black' : 'white';
return ($c >= 160) ? 'black' : 'white';
}
/**
* Checks variables are valid and throws exception if not
*/
private function check() {
if ($this->saturation < 0 || $this->saturation > 100)
throw new Exception('Saturation must be between 0–100', 1);
if ($this->lightness < 0 || $this->lightness > 100)
throw new Exception('Lightness must be between 0–100', 1);
if (!in_array($this->format, [self::HEX, self::RGB, self::HSL]))
throw new Exception('Format must be HEX, RGB or HSL', 1);
}
/**
* output
*
* Constructs the required return value
*/
private function output() {
// HSL
if ($this->format === self::HSL) {
return [
'h' => $this->hue,
's' => $this->saturation,
'l' => $this->lightness,
];
}
// RGB
if ($this->format === self::RGB) {
$rgb = self::hsl2rgb(
$this->hue / 360,
$this->saturation / 100,
$this->lightness / 100
);
return [
'r' => $rgb['r'],
'g' => $rgb['g'],
'b' => $rgb['b'],
];
}
// HEX
if ($this->format === self::HEX) {
// Convert to RGB
$rgb = self::hsl2rgb(
$this->hue / 360,
$this->saturation / 100,
$this->lightness / 100
);
// Convert to hex and pad
foreach ($rgb as $key => $val)
$rgb[$key] = str_pad(dechex($val), 2, '0', STR_PAD_LEFT);
return strtoupper($rgb['r'].$rgb['g'].$rgb['b']);
}
}
/**
* hsl2rgb
*
* Converts an color's HSL value to RGB
* All parameters are expected to be in the range of 0–0.999
*
* @link http://stackoverflow.com/a/3642787/1562799
*
* @param $h Hue
* @param $s Saturation
* @param $l Lightness
*
* @return array Indexes 'r', 'g', and 'b' hold the color's RGB values
*/
private function hsl2rgb($h, $s, $l){
$r = $l;
$g = $l;
$b = $l;
$v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s);
if ($v > 0){
$m;
$sv;
$sextant;
$fract;
$vsf;
$mid1;
$mid2;
$m = $l + $l - $v;
$sv = ($v - $m ) / $v;
$h *= 6.0;
$sextant = floor($h);
$fract = $h - $sextant;
$vsf = $v * $sv * $fract;
$mid1 = $m + $vsf;
$mid2 = $v - $vsf;
switch ($sextant) {
case 0:
$r = $v;
$g = $mid1;
$b = $m;
break;
case 1:
$r = $mid2;
$g = $v;
$b = $m;
break;
case 2:
$r = $m;
$g = $v;
$b = $mid1;
break;
case 3:
$r = $m;
$g = $mid2;
$b = $v;
break;
case 4:
$r = $mid1;
$g = $m;
$b = $v;
break;
case 5:
$r = $v;
$g = $m;
$b = $mid2;
break;
}
}
return array(
'r' => (int) floor($r * 255),
'g' => (int) floor($g * 255),
'b' => (int) floor($b * 255),
);
}
}