From 8a50a46665b699236920dd7f0aca74beb3fb0077 Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Tue, 9 Sep 2014 16:14:24 +0200 Subject: [PATCH] util methods for calculating color brightness --- js/reveal.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/js/reveal.js b/js/reveal.js index e756110..f92eca9 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -714,6 +714,10 @@ if( data.backgroundPosition ) element.style.backgroundPosition = data.backgroundPosition; if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition ); + if( data.backgroundColor ) { + + } + container.appendChild( element ); return element; @@ -1065,6 +1069,63 @@ } + /** + * Measures the distance in pixels between point a and point b. + * + * @param {String} color The string representation of a color, + * the following formats are supported: + * - #000 + * - #000000 + * - rgb(0,0,0) + */ + function colorToRgb( color ) { + + var hex3 = color.match( /^#([0-9a-f]{3})$/i ); + if( hex3 && hex3[1] ) { + hex3 = hex3[1]; + return { + r: parseInt( hex3.charAt( 0 ), 16 ) * 0x11, + g: parseInt( hex3.charAt( 1 ), 16 ) * 0x11, + b: parseInt( hex3.charAt( 2 ), 16 ) * 0x11 + }; + } + + var hex6 = color.match( /^#([0-9a-f]{6})$/i ); + if( hex6 && hex6[1] ) { + hex6 = hex6[1]; + return { + r: parseInt( hex6.substr( 0, 2 ), 16 ), + g: parseInt( hex6.substr( 2, 2 ), 16 ), + b: parseInt( hex6.substr( 4, 2 ), 16 ) + }; + } + + var rgb = color.match( /^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i ); + if( rgb ) { + return { + r: rgb[1], + g: rgb[2], + b: rgb[3] + }; + } + + return null; + + } + + /** + * Calculates brightness on a scale of 0-255. + * + * @param color See colorStringToRgb for supported formats. + */ + function colorBrightness( color ) { + + if( typeof color === 'string' ) color = colorToRgb( color ); + + return ( color.r * 299 + color.g * 587 + color.b * 114 ) / 1000; + + } + /** * Retrieves the height of the given element by looking * at the position and height of its immediate children. @@ -4039,6 +4100,9 @@ addEventListeners: addEventListeners, removeEventListeners: removeEventListeners, + colorToRgb: colorToRgb, + colorBrightness: colorBrightness, + // Facility for persisting and restoring the presentation state getState: getState, setState: setState,