Sin descripción

customize-helpers.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Get luminance from a HEX color.
  3. *
  4. * @since Twenty Twenty-One 1.0
  5. *
  6. * @param {string} hex - The hex color.
  7. *
  8. * @return {number} - Returns the luminance, number between 0 and 255.
  9. */
  10. function twentytwentyoneGetHexLum( hex ) { // jshint ignore:line
  11. var rgb = twentytwentyoneGetRgbFromHex( hex );
  12. return Math.round( ( 0.2126 * rgb.r ) + ( 0.7152 * rgb.g ) + ( 0.0722 * rgb.b ) );
  13. }
  14. /**
  15. * Get RGB from HEX.
  16. *
  17. * @since Twenty Twenty-One 1.0
  18. *
  19. * @param {string} hex - The hex color.
  20. *
  21. * @return {Object} - Returns an object {r, g, b}
  22. */
  23. function twentytwentyoneGetRgbFromHex( hex ) {
  24. var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i,
  25. result;
  26. // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF").
  27. hex = hex.replace( shorthandRegex, function( m, r, g, b ) {
  28. return r.toString() + r.toString() + g.toString() + g.toString() + b.toString() + b.toString();
  29. } );
  30. result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( hex );
  31. return result ? {
  32. r: parseInt( result[1], 16 ),
  33. g: parseInt( result[2], 16 ),
  34. b: parseInt( result[3], 16 )
  35. } : null;
  36. }