PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/kirki/lib/class-kirki-color.php

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