wp-customize-utils.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. *
  4. * Assign a value to an object at the given location. Create the nested objects if they aren't already available.
  5. *
  6. * @param object $target The object to assign the value to
  7. * @param array $array The array describing the location of the property to update
  8. * @param object $value The value to assign
  9. * @return object The modified $target object with $value assigned where $array describes
  10. *
  11. */
  12. function set_settings_array( $target, $array, $value ) {
  13. $key = array_shift( $array );
  14. $current =& $target;
  15. while ( 0 < sizeof( $array ) ) {
  16. if ( ! property_exists( $current, $key ) ) {
  17. $current->{ $key } = (object) array();
  18. }
  19. $current =& $current->{ $key };
  20. // Cast to an object in the case where it's been set as an array.
  21. $current = (object) $current;
  22. $key = array_shift( $array );
  23. }
  24. $current->{ $key } = $value;
  25. return $target;
  26. }
  27. /**
  28. *
  29. * Get a value from an object at the given location.
  30. *
  31. * @param array $array The array describing the location of the property to update.
  32. * @param object $object The object.
  33. * @return object The value at the location.
  34. *
  35. */
  36. function get_settings_array( $array, $object ) {
  37. foreach( $array as $property ) {
  38. $object = $object[ $property ];
  39. }
  40. return $object;
  41. }
  42. // These functions are borrowed from the colorline lib
  43. function hex_to_rgb( $hex ) {
  44. return sscanf( str_replace( '#', '', $hex), '%02X%02X%02X' );
  45. }
  46. // RGB values: 0-255
  47. // LUM values: 0-1
  48. function rgb_to_lum( $rgb ) {
  49. list( $r, $g, $b ) = $rgb;
  50. return sqrt( 0.241 * $r * $r + 0.691 * $g * $g + 0.068 * $b * $b ) / 255;
  51. }
  52. // RGB values: 0-255, 0-255, 0-255
  53. // HSV values: 0-360, 0-100, 0-100, 0-100
  54. function rgb_to_hsvl( $rgb ) {
  55. $l = rgb_to_lum( $rgb );
  56. list( $r, $g, $b ) = $rgb;
  57. $r = $r / 255;
  58. $g = $g / 255;
  59. $b = $b / 255;
  60. $max_rgb = max( $r, $g, $b );
  61. $min_rgb = min( $r, $g, $b );
  62. $chroma = $max_rgb - $min_rgb;
  63. $v = 100 * $max_rgb;
  64. if ( $chroma > 0 ) {
  65. $s = 100 * ( $chroma / $max_rgb );
  66. if ( $r === $min_rgb ) {
  67. $h = 3 - ( ( $g - $b ) / $chroma );
  68. } elseif ( $b === $min_rgb ) {
  69. $h = 1 - ( ( $r - $g ) / $chroma );
  70. } else { // $g === $min_rgb
  71. $h = 5 - ( ( $b - $r ) / $chroma );
  72. }
  73. $h = 60 * $h;
  74. return array( $h, $s, $v, $l );
  75. } else {
  76. return array( 0, 0, $v, $l );
  77. }
  78. }
  79. function colorLuminescence( $hex ) {
  80. $rgb = hex_to_rgb( $hex );
  81. $hsvl = rgb_to_hsvl( $rgb );
  82. return $hsvl[3];
  83. }