PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.1
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / customizer / lib / class-kirki-color.php
kirki / customizer / lib Last commit date
class-aricolor.php 5 months ago class-kirki-color.php 5 months ago
class-kirki-color.php
343 lines
1 <?php
2 /**
3 * Color Calculations class for Kirki
4 * Initially built for the Shoestrap-3 theme and then tweaked for Kirki.
5 *
6 * @package Kirki
7 * @category Core
8 * @author Themeum
9 * @copyright Copyright (c) 2023, Themeum
10 * @license https://opensource.org/licenses/MIT
11 * @since 1.0
12 */
13
14 // phpcs:ignoreFile
15
16 // Exit if accessed directly.
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 /**
22 * Helper class for color manipulation.
23 */
24 final class Kirki_Color extends ariColor {
25
26 /**
27 * A proxy for the sanitize_color method.
28 *
29 * @param string|array $color The color.
30 * @param bool $hash Whether we want to include a hash (#) at the beginning or not.
31 * @return string The sanitized hex color.
32 */
33 public static function sanitize_hex( $color = '#FFFFFF', $hash = true ) {
34 if ( ! $hash ) {
35 return ltrim( self::sanitize_color( $color, 'hex' ), '#' );
36 }
37 return self::sanitize_color( $color, 'hex' );
38 }
39
40 /**
41 * A proxy the sanitize_color method.
42 *
43 * @static
44 * @access public
45 * @param string $color The color.
46 * @return string
47 */
48 public static function sanitize_rgba( $color ) {
49 return self::sanitize_color( $color, 'rgba' );
50 }
51
52 /**
53 * Sanitize colors.
54 * Determine if the current value is a hex or an rgba color and call the appropriate method.
55 *
56 * @static
57 * @access public
58 * @since 0.8.5
59 * @param string|array $color The color.
60 * @param string $mode The mode to be used.
61 * @return string
62 */
63 public static function sanitize_color( $color = '', $mode = 'auto' ) {
64 if ( is_string( $color ) && 'transparent' == trim( $color ) ) {
65 return 'transparent';
66 }
67 $obj = ariColor::newColor( $color );
68 if ( 'auto' == $mode ) {
69 $mode = $obj->mode;
70 }
71 return $obj->toCSS( $mode );
72 }
73
74 /**
75 * Gets the rgb value of a color.
76 *
77 * @static
78 * @access public
79 * @param string $color The color.
80 * @param boolean $implode Whether we want to implode the values or not.
81 * @return array|string
82 */
83 public static function get_rgb( $color, $implode = false ) {
84 $obj = ariColor::newColor( $color );
85 if ( $implode ) {
86 return $obj->toCSS( 'rgb' );
87 }
88 return array( $obj->red, $obj->green, $obj->blue );
89 }
90
91 /**
92 * A proxy for the sanitize_color method.
93 *
94 * @static
95 * @access public
96 * @param string|array $color The color to convert.
97 * @return string The hex value of the color.
98 */
99 public static function rgba2hex( $color ) {
100 return self::sanitize_color( $color, 'hex' );
101 }
102
103 /**
104 * Get the alpha channel from an rgba color.
105 *
106 * @static
107 * @access public
108 * @param string $color The rgba color formatted like rgba(r,g,b,a).
109 * @return int|float The alpha value of the color.
110 */
111 public static function get_alpha_from_rgba( $color ) {
112 $obj = ariColor::newColor( $color );
113 return $obj->alpha;
114 }
115
116 /**
117 * Gets the rgba value of the $color.
118 *
119 * @static
120 * @access public
121 * @param string $color The hex value of a color.
122 * @param int|float $alpha Opacity level (0-1).
123 * @return string
124 */
125 public static function get_rgba( $color = '#fff', $alpha = 1 ) {
126 $obj = ariColor::newColor( $color );
127 if ( 1 == $alpha ) {
128 return $obj->toCSS( 'rgba' );
129 }
130 // Make sure that opacity is properly formatted.
131 // Converts 1-100 values to 0-1.
132 if ( $alpha > 1 || $alpha < -1 ) {
133 // Divide by 100.
134 $alpha /= 100;
135 }
136 // Get absolute value.
137 $alpha = abs( $alpha );
138 // Max 1.
139 if ( 1 < $alpha ) {
140 $alpha = 1;
141 }
142 $new_obj = $obj->getNew( 'alpha', $alpha );
143 return $new_obj->toCSS( 'rgba' );
144 }
145
146 /**
147 * Strips the alpha value from an RGBA color string.
148 *
149 * @static
150 * @access public
151 * @param string $color The RGBA color string.
152 * @return string The corresponding RGB string.
153 */
154 public static function rgba_to_rgb( $color ) {
155 $obj = ariColor::newColor( $color );
156 return $obj->toCSS( 'rgb' );
157 }
158
159 /**
160 * Gets the brightness of the $hex color.
161 *
162 * @static
163 * @access public
164 * @param string $hex The hex value of a color.
165 * @return int Value between 0 and 255.
166 */
167 public static function get_brightness( $hex ) {
168 $hex = self::sanitize_hex( $hex, false );
169
170 // Returns brightness value from 0 to 255.
171 return intval( ( ( hexdec( substr( $hex, 0, 2 ) ) * 299 ) + ( hexdec( substr( $hex, 2, 2 ) ) * 587 ) + ( hexdec( substr( $hex, 4, 2 ) ) * 114 ) ) / 1000 );
172 }
173
174 /**
175 * Adjusts brightness of the $hex color.
176 *
177 * @static
178 * @access public
179 * @param string $hex The hex value of a color.
180 * @param integer $steps Should be between -255 and 255. Negative = darker, positive = lighter.
181 * @return string Returns hex color.
182 */
183 public static function adjust_brightness( $hex, $steps ) {
184 $hex = self::sanitize_hex( $hex, false );
185 $steps = max( -255, min( 255, $steps ) );
186
187 // Adjust number of steps and keep it inside 0 to 255.
188 $red = max( 0, min( 255, hexdec( substr( $hex, 0, 2 ) ) + $steps ) );
189 $green = max( 0, min( 255, hexdec( substr( $hex, 2, 2 ) ) + $steps ) );
190 $blue = max( 0, min( 255, hexdec( substr( $hex, 4, 2 ) ) + $steps ) );
191
192 $red_hex = str_pad( dechex( $red ), 2, '0', STR_PAD_LEFT );
193 $green_hex = str_pad( dechex( $green ), 2, '0', STR_PAD_LEFT );
194 $blue_hex = str_pad( dechex( $blue ), 2, '0', STR_PAD_LEFT );
195 return self::sanitize_hex( $red_hex . $green_hex . $blue_hex );
196 }
197
198 /**
199 * Mixes 2 hex colors.
200 * The "percentage" variable is the percent of the first color.
201 * to be used it the mix. default is 50 (equal mix).
202 *
203 * @static
204 * @access public
205 * @param string|false $hex1 Color.
206 * @param string|false $hex2 Color.
207 * @param int $percentage A value between 0 and 100.
208 * @return string Returns hex color.
209 */
210 public static function mix_colors( $hex1, $hex2, $percentage ) {
211 $hex1 = self::sanitize_hex( $hex1, false );
212 $hex2 = self::sanitize_hex( $hex2, false );
213 $red = ( $percentage * hexdec( substr( $hex1, 0, 2 ) ) + ( 100 - $percentage ) * hexdec( substr( $hex2, 0, 2 ) ) ) / 100;
214 $green = ( $percentage * hexdec( substr( $hex1, 2, 2 ) ) + ( 100 - $percentage ) * hexdec( substr( $hex2, 2, 2 ) ) ) / 100;
215 $blue = ( $percentage * hexdec( substr( $hex1, 4, 2 ) ) + ( 100 - $percentage ) * hexdec( substr( $hex2, 4, 2 ) ) ) / 100;
216 $red_hex = str_pad( dechex( $red ), 2, '0', STR_PAD_LEFT );
217 $green_hex = str_pad( dechex( $green ), 2, '0', STR_PAD_LEFT );
218 $blue_hex = str_pad( dechex( $blue ), 2, '0', STR_PAD_LEFT );
219 return self::sanitize_hex( $red_hex . $green_hex . $blue_hex );
220 }
221
222 /**
223 * Convert hex color to hsv.
224 *
225 * @static
226 * @access public
227 * @param string $hex The hex value of color 1.
228 * @return array Returns array( 'h', 's', 'v' ).
229 */
230 public static function hex_to_hsv( $hex ) {
231 $rgb = (array) (array) self::get_rgb( self::sanitize_hex( $hex, false ) );
232 return self::rgb_to_hsv( $rgb );
233 }
234
235 /**
236 * Convert hex color to hsv.
237 *
238 * @static
239 * @access public
240 * @param string $color The rgb color to convert array( 'r', 'g', 'b' ).
241 * @return array Returns array( 'h', 's', 'v' ).
242 */
243 public static function rgb_to_hsv( $color = array() ) {
244 $var_r = ( $color[0] / 255 );
245 $var_g = ( $color[1] / 255 );
246 $var_b = ( $color[2] / 255 );
247 $var_min = min( $var_r, $var_g, $var_b );
248 $var_max = max( $var_r, $var_g, $var_b );
249 $del_max = $var_max - $var_min;
250 $h = 0;
251 $s = 0;
252 $v = $var_max;
253 if ( 0 != $del_max ) {
254 $s = $del_max / $var_max;
255 $del_r = ( ( ( $var_max - $var_r ) / 6 ) + ( $del_max / 2 ) ) / $del_max;
256 $del_g = ( ( ( $var_max - $var_g ) / 6 ) + ( $del_max / 2 ) ) / $del_max;
257 $del_b = ( ( ( $var_max - $var_b ) / 6 ) + ( $del_max / 2 ) ) / $del_max;
258 if ( $var_r == $var_max ) {
259 $h = $del_b - $del_g;
260 } elseif ( $var_g == $var_max ) {
261 $h = ( 1 / 3 ) + $del_r - $del_b;
262 } elseif ( $var_b == $var_max ) {
263 $h = ( 2 / 3 ) + $del_g - $del_r;
264 }
265 if ( $h < 0 ) {
266 $h++;
267 }
268 if ( $h > 1 ) {
269 $h--;
270 }
271 }
272 return array(
273 'h' => round( $h, 2 ),
274 's' => round( $s, 2 ),
275 'v' => round( $v, 2 ),
276 );
277 }
278
279 /**
280 * This is a very simple algorithm that works by summing up the differences between the three color components red, green and blue.
281 * A value higher than 500 is recommended for good readability.
282 *
283 * @static
284 * @access public
285 * @param string $color_1 The 1st color.
286 * @param string $color_2 The 2nd color.
287 * @return string
288 */
289 public static function color_difference( $color_1 = '#ffffff', $color_2 = '#000000' ) {
290 $color_1 = self::sanitize_hex( $color_1, false );
291 $color_2 = self::sanitize_hex( $color_2, false );
292 $color_1_rgb = self::get_rgb( $color_1 );
293 $color_2_rgb = self::get_rgb( $color_2 );
294 $r_diff = max( $color_1_rgb[0], $color_2_rgb[0] ) - min( $color_1_rgb[0], $color_2_rgb[0] );
295 $g_diff = max( $color_1_rgb[1], $color_2_rgb[1] ) - min( $color_1_rgb[1], $color_2_rgb[1] );
296 $b_diff = max( $color_1_rgb[2], $color_2_rgb[2] ) - min( $color_1_rgb[2], $color_2_rgb[2] );
297 $color_diff = $r_diff + $g_diff + $b_diff;
298 return $color_diff;
299 }
300
301 /**
302 * This function tries to compare the brightness of the colors.
303 * A return value of more than 125 is recommended.
304 * Combining it with the color_difference function above might make sense.
305 *
306 * @static
307 * @access public
308 * @param string $color_1 The 1st color.
309 * @param string $color_2 The 2nd color.
310 * @return string
311 */
312 public static function brightness_difference( $color_1 = '#ffffff', $color_2 = '#000000' ) {
313 $color_1 = self::sanitize_hex( $color_1, false );
314 $color_2 = self::sanitize_hex( $color_2, false );
315 $color_1_rgb = self::get_rgb( $color_1 );
316 $color_2_rgb = self::get_rgb( $color_2 );
317 $br_1 = ( 299 * $color_1_rgb[0] + 587 * $color_1_rgb[1] + 114 * $color_1_rgb[2] ) / 1000;
318 $br_2 = ( 299 * $color_2_rgb[0] + 587 * $color_2_rgb[1] + 114 * $color_2_rgb[2] ) / 1000;
319 return intval( abs( $br_1 - $br_2 ) );
320 }
321
322 /**
323 * Uses the luminosity to calculate the difference between the given colors.
324 * The returned value should be bigger than 5 for best readability.
325 *
326 * @static
327 * @access public
328 * @param string $color_1 The 1st color.
329 * @param string $color_2 The 2nd color.
330 * @return string
331 */
332 public static function lumosity_difference( $color_1 = '#ffffff', $color_2 = '#000000' ) {
333 $color_1 = self::sanitize_hex( $color_1, false );
334 $color_2 = self::sanitize_hex( $color_2, false );
335 $color_1_rgb = self::get_rgb( $color_1 );
336 $color_2_rgb = self::get_rgb( $color_2 );
337 $l1 = 0.2126 * pow( $color_1_rgb[0] / 255, 2.2 ) + 0.7152 * pow( $color_1_rgb[1] / 255, 2.2 ) + 0.0722 * pow( $color_1_rgb[2] / 255, 2.2 );
338 $l2 = 0.2126 * pow( $color_2_rgb[0] / 255, 2.2 ) + 0.7152 * pow( $color_2_rgb[1] / 255, 2.2 ) + 0.0722 * pow( $color_2_rgb[2] / 255, 2.2 );
339 $lum_diff = ( $l1 > $l2 ) ? ( $l1 + 0.05 ) / ( $l2 + 0.05 ) : ( $l2 + 0.05 ) / ( $l1 + 0.05 );
340 return round( $lum_diff, 2 );
341 }
342 }
343