color-annotations-preview.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * File color-annotations-preview.js.
  3. *
  4. * Instantly live-update customizer settings in the preview for improved user experience,
  5. * targeting the updates needed to hide the page title on the homepage on WordPress.com.
  6. */
  7. // From https://gist.github.com/xenozauros/f6e185c8de2a04cdfecf
  8. function hexToHSL( hex ) {
  9. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( hex );
  10. r = parseInt( result[1], 16 );
  11. g = parseInt( result[2], 16 );
  12. b = parseInt( result[3], 16 );
  13. r /= 255, g /= 255, b /= 255;
  14. var max = Math.max( r, g, b ), min = Math.min( r, g, b );
  15. var h, s, l = ( max + min ) / 2;
  16. if( max == min ){
  17. h = s = 0; // achromatic
  18. } else {
  19. var d = max - min;
  20. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  21. switch( max ){
  22. case r: h = ( g - b ) / d + ( g < b ? 6 : 0 ); break;
  23. case g: h = ( b - r ) / d + 2; break;
  24. case b: h = ( r - g ) / d + 4; break;
  25. }
  26. h /= 6;
  27. }
  28. var HSL = new Object();
  29. HSL['h'] = h;
  30. HSL['s'] = s;
  31. HSL['l'] = l;
  32. return HSL;
  33. }
  34. function changeColorLuminescence( hex, amount ) {
  35. var hsl = hexToHSL( hex );
  36. return 'hsl( ' + hsl.h * 360 + ',' + hsl.s * 100 + '%,' + ( hsl.l * 100 + amount ) + '%)';
  37. }
  38. ( function( $ ) {
  39. // Since the plugin handles customizer preview updates via the postMessage transport,
  40. // we need to manually override the "extra CSS" when a user selects a different color palette.
  41. wp.customize( 'colors_manager[colors]', function( value ) {
  42. value.bind( function( to ) {
  43. var background = to.bg;
  44. var foreground = to.txt;
  45. var primary = to.link;
  46. var secondary = to.fg1;
  47. var border = to.fg2;
  48. var cssVariables = '';
  49. if( background !== '' ){
  50. var backgroundLowContrast = changeColorLuminescence( background, -10 );
  51. var backgroundHighContrast = changeColorLuminescence( background, 10 );
  52. cssVariables += '--wp--preset--color--background: ' + background + ';' +
  53. '--wp--preset--color--background-low-contrast: ' + backgroundLowContrast + ';' +
  54. '--wp--preset--color--background-high-contrast: ' + backgroundHighContrast + ';';
  55. }
  56. if( foreground !== '' ){
  57. var foregroundLowContrast = changeColorLuminescence( foreground, 10 );
  58. var foregroundHighContrast = changeColorLuminescence( foreground, -10 );
  59. cssVariables += '--wp--preset--color--foreground: ' + foreground + ';' +
  60. '--wp--preset--color--foreground-low-contrast: ' + foregroundLowContrast + ';' +
  61. '--wp--preset--color--foreground-high-contrast: ' + foregroundHighContrast + ';';
  62. }
  63. if( primary !== '' ){
  64. var primaryHover = changeColorLuminescence( primary, 10 );
  65. var primaryDark = changeColorLuminescence( primary, -10 );
  66. cssVariables += '--wp--preset--color--primary: ' + primary + ';' +
  67. '--wp--preset--color--primary-hover: ' + primaryHover + ';' +
  68. '--wp--preset--color--primary-dark: ' + primaryDark + ';';
  69. }
  70. if( secondary !== '' ){
  71. var secondaryHover = changeColorLuminescence( secondary, 10 );
  72. cssVariables += '--wp--preset--color--secondary: ' + secondary + ';' +
  73. '--wp--preset--color--secondary-hover: ' + secondaryHover + ';';
  74. }
  75. if( border !== '' ){
  76. var borderLowContrast = changeColorLuminescence( border, 10 );
  77. var borderHighContrast = changeColorLuminescence( border, -10 );
  78. cssVariables += '--wp--preset--color--border: ' + border + ';' +
  79. '--wp--preset--color--border-low-contrast: ' + borderLowContrast + ';' +
  80. '--wp--preset--color--border-high-contrast: ' + borderHighContrast + ';';
  81. }
  82. const extraCSS = ':root body {' + cssVariables + '}';
  83. // Append an extra style element that overrides the previous extra CSS
  84. if ( $('#custom-colors-extra-css').length ) {
  85. $( '#custom-colors-extra-css' ).html( extraCSS );
  86. } else {
  87. $( 'head' ).append( '<style id="custom-colors-extra-css">' + extraCSS + '</style>' );
  88. }
  89. if ( typeof cssVars !== 'undefined' ) {
  90. cssVars( {} );
  91. }
  92. } );
  93. } );
  94. } )( jQuery );