angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

customize-helpers.js (1100B)


      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 /**
     16  * Get RGB from HEX.
     17  *
     18  * @since Twenty Twenty-One 1.0
     19  *
     20  * @param {string} hex - The hex color.
     21  *
     22  * @return {Object} - Returns an object {r, g, b}
     23  */
     24 function twentytwentyoneGetRgbFromHex( hex ) {
     25 	var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i,
     26 		result;
     27 
     28 	// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF").
     29 	hex = hex.replace( shorthandRegex, function( m, r, g, b ) {
     30 		return r.toString() + r.toString() + g.toString() + g.toString() + b.toString() + b.toString();
     31 	} );
     32 
     33 	result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( hex );
     34 	return result ? {
     35 		r: parseInt( result[1], 16 ),
     36 		g: parseInt( result[2], 16 ),
     37 		b: parseInt( result[3], 16 )
     38 	} : null;
     39 }