wp-customize-utils.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $key = array_shift( $array );
  21. }
  22. $current->{ $key } = $value;
  23. return $target;
  24. }
  25. /**
  26. *
  27. * Get a value from an object at the given location.
  28. *
  29. * @param array $array The array describing the location of the property to update.
  30. * @param object $object The object.
  31. * @return object The value at the location.
  32. *
  33. */
  34. function get_settings_array( $array, $object ) {
  35. foreach( $array as $property ) {
  36. $object = $object[ $property ];
  37. }
  38. return $object;
  39. }
  40. // These functions are borrowed from the colorline lib
  41. function hex_to_rgb( $hex ) {
  42. return sscanf( str_replace( '#', '', $hex), '%02X%02X%02X' );
  43. }
  44. // RGB values: 0-255
  45. // LUM values: 0-1
  46. function rgb_to_lum( $rgb ) {
  47. list( $r, $g, $b ) = $rgb;
  48. return sqrt( 0.241 * $r * $r + 0.691 * $g * $g + 0.068 * $b * $b ) / 255;
  49. }
  50. // RGB values: 0-255, 0-255, 0-255
  51. // HSV values: 0-360, 0-100, 0-100, 0-100
  52. function rgb_to_hsvl( $rgb ) {
  53. $l = rgb_to_lum( $rgb );
  54. list( $r, $g, $b ) = $rgb;
  55. $r = $r / 255;
  56. $g = $g / 255;
  57. $b = $b / 255;
  58. $max_rgb = max( $r, $g, $b );
  59. $min_rgb = min( $r, $g, $b );
  60. $chroma = $max_rgb - $min_rgb;
  61. $v = 100 * $max_rgb;
  62. if ( $chroma > 0 ) {
  63. $s = 100 * ( $chroma / $max_rgb );
  64. if ( $r === $min_rgb ) {
  65. $h = 3 - ( ( $g - $b ) / $chroma );
  66. } elseif ( $b === $min_rgb ) {
  67. $h = 1 - ( ( $r - $g ) / $chroma );
  68. } else { // $g === $min_rgb
  69. $h = 5 - ( ( $b - $r ) / $chroma );
  70. }
  71. $h = 60 * $h;
  72. return array( $h, $s, $v, $l );
  73. } else {
  74. return array( 0, 0, $v, $l );
  75. }
  76. }
  77. function colorLuminescence( $hex ) {
  78. $rgb = hex_to_rgb( $hex );
  79. $hsvl = rgb_to_hsvl( $rgb );
  80. return $hsvl[3];
  81. }