-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-color-adjuster.php
215 lines (182 loc) · 5.44 KB
/
simple-color-adjuster.php
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
<?php
/**
* Simple class to do Sass' `lighten()` and `darken()` using PHP
* Intentionally made for WordPress theming. [SCSS PHP](http://leafo.net/scssphp/) is amazing but it triggers too much alert on theme-check. Hence a need for a simpler one.
* This library is originally made for [Materialist theme](http://fikrirasy.id/portfolio/materialist).
*
* @copyright Fikri Rasyid
* @license http://opensource.org/licenses/gpl-license GPL-3.0
*
* Simple Color Adjuster is developed by modifying following resources:
*
* scssphp library - http://leafo.net/scssphp/
* License: Distributed under the terms of GNU General Public License v3
* Copyright: Leaf Corcoran, http://leafo.net/
*
* Twenty Fifteen - https://wordpress.org/themes/twentyfifteen
* License: Distributed under the terms of GNU General Public License
* Copyright: The WordPress team, http://wordpress.org
*
* Opus - http://fikrirasy.id/portfolio/opus
* License: Distributed under the terms of GNU General Public License
* Copyright: Fikri Rasyid, http://fikrirasy.id
*/
class Simple_Color_Adjuster{
/**
* Method for lightening
*
* @param string hexacode
* @param float 0 - 100 (percentage)
*
* @return string hexacode
*/
public function lighten( $hex, $amount ){
return $this->adjust_color( $hex, $amount );
}
/**
* Method for darkening
*
* @param string hexacode
* @param float 0 - 100 (percentage)
*
* @return string hexacode
*/
public function darken( $hex, $amount ){
$amount = 0 - $amount;
return $this->adjust_color( $hex, $amount );
}
/**
* Darken/Lighten color based on hexacode and percentage given
* The code is based on Leaf Corcoran's SCSS compiler based on PHP which is released under GPL v3
*
* @param string hexacode
* @param float lighten / darken percentage -100 - 100
*
* @return string hexacode
*/
public function adjust_color( $hex, $amount ){
// Translate hexacode into RGB
$rgb = $this->hex_to_rgb( $hex );
// Translate RBG to HSL
$hsl = $this->rgb_to_hsl( $rgb[0], $rgb[1], $rgb[2] );
// Darkening / Lightening
$hsl[2] += $amount;
// Translate HSL to RGB
$color = $this->hsl_to_rgb( $hsl[0], $hsl[1], $hsl[2] );
// Translate RGB to hex
$adjusted_color = $this->rgb_to_hex( $color );
return $adjusted_color;
}
/**
* Convert hexacode color into array of RGB
* The code is based on Twenty Fifteen's twentyfifteen_hex2rgb() which is released under GPL v2
*
* @param string hexacode
* @return array rgb value
*/
public function hex_to_rgb( $hex ){
$color = trim( $hex, '#' );
if ( strlen( $color ) == 3 ) {
$r = hexdec( substr( $color, 0, 1 ).substr( $color, 0, 1 ) );
$g = hexdec( substr( $color, 1, 1 ).substr( $color, 1, 1 ) );
$b = hexdec( substr( $color, 2, 1 ).substr( $color, 2, 1 ) );
} else if ( strlen( $color ) == 6 ) {
$r = hexdec( substr( $color, 0, 2 ) );
$g = hexdec( substr( $color, 2, 2 ) );
$b = hexdec( substr( $color, 4, 2 ) );
} else {
return array();
}
return array( $r, $g, $b );
}
/**
* Convert array of RGB into hexacode color
* The code is based on Opus Theme's Opus_Customizer->adjust_brightness() which is released under GPL v2
*
* @param array rgb value
* @return param string hexacode
*/
public function rgb_to_hex( $rgb ){
$hex = "#";
$hex .= str_pad( dechex( round( $rgb[0] ) ), 2, "0", STR_PAD_LEFT );
$hex .= str_pad( dechex( round( $rgb[1] ) ), 2, "0", STR_PAD_LEFT );
$hex .= str_pad( dechex( round( $rgb[2] ) ), 2, "0", STR_PAD_LEFT );
return $hex;
}
/**
* Convert RGB into HSL
* The code is based on Leaf Corcoran's SCSS compiler based on PHP which is released under GPL v3
*
* @param int red of RGB
* @param int green of RGB
* @param int blue of RGB
*
* @return array of HSL value
*/
public function rgb_to_hsl( $red, $green, $blue ){
$r = $red / 255;
$g = $green / 255;
$b = $blue / 255;
$min = min($r, $g, $b);
$max = max($r, $g, $b);
$d = $max - $min;
$l = ($min + $max) / 2;
if ($min == $max) {
$s = $h = 0;
} else {
if ($l < 0.5)
$s = $d / (2 * $l);
else
$s = $d / (2 - 2 * $l);
if ($r == $max)
$h = 60 * ($g - $b) / $d;
elseif ($g == $max)
$h = 60 * ($b - $r) / $d + 120;
elseif ($b == $max)
$h = 60 * ($r - $g) / $d + 240;
}
return array( fmod($h, 360), $s * 100, $l * 100);
}
/**
* Convert HUE into RGB
* The code is based on Leaf Corcoran's SCSS compiler based on PHP which is released under GPL v3
*/
public function hue_to_rgb( $m1, $m2, $h ){
if ($h < 0)
$h += 1;
elseif ($h > 1)
$h -= 1;
if ($h * 6 < 1)
return $m1 + ($m2 - $m1) * $h * 6;
if ($h * 2 < 1)
return $m2;
if ($h * 3 < 2)
return $m1 + ($m2 - $m1) * (2/3 - $h) * 6;
return $m1;
}
/**
* Convert HSL to RGB
* The code is based on Leaf Corcoran's SCSS compiler based on PHP which is released under GPL v3
*
* @param float hue
* @param float saturation
* @param float lightenss
*
* @return array RGB color
*/
public function hsl_to_rgb( $hue, $saturation, $lightness ){
if ($hue < 0) {
$hue += 360;
}
$h = $hue / 360;
$s = min(100, max(0, $saturation)) / 100;
$l = min(100, max(0, $lightness)) / 100;
$m2 = $l <= 0.5 ? $l * ($s + 1) : $l + $s - $l * $s;
$m1 = $l * 2 - $m2;
$r = $this->hue_to_rgb($m1, $m2, $h + 1/3) * 255;
$g = $this->hue_to_rgb($m1, $m2, $h) * 255;
$b = $this->hue_to_rgb($m1, $m2, $h - 1/3) * 255;
$out = array($r, $g, $b);
return $out;
}
}