rgba color parsing support, ignore brightness of transparent colors

This commit is contained in:
Hakim El Hattab 2014-09-10 10:53:24 +02:00
parent 41f20301b6
commit 0d14d87f1a
1 changed files with 25 additions and 8 deletions

View File

@ -721,11 +721,18 @@
// color, no class will be set // color, no class will be set
var computedBackgroundColor = window.getComputedStyle( element ).backgroundColor; var computedBackgroundColor = window.getComputedStyle( element ).backgroundColor;
if( computedBackgroundColor ) { if( computedBackgroundColor ) {
if( colorBrightness( computedBackgroundColor ) < 128 ) { var rgb = colorToRgb( computedBackgroundColor );
slide.classList.add( 'has-dark-background' );
} // Ignore fully transparent backgrounds. Some browsers return
else { // rgba(0,0,0,0) when reading the computed background color of
slide.classList.add( 'has-light-background' ); // an element with no background
if( rgb && rgb.a !== 0 ) {
if( colorBrightness( computedBackgroundColor ) < 128 ) {
slide.classList.add( 'has-dark-background' );
}
else {
slide.classList.add( 'has-light-background' );
}
} }
} }
@ -1112,9 +1119,19 @@
var rgb = color.match( /^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i ); var rgb = color.match( /^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i );
if( rgb ) { if( rgb ) {
return { return {
r: rgb[1], r: parseInt( rgb[1], 10 ),
g: rgb[2], g: parseInt( rgb[2], 10 ),
b: rgb[3] b: parseInt( rgb[3], 10 )
};
}
var rgba = color.match( /^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\,\s*([\d]+|[\d]*.[\d]+)\s*\)$/i );
if( rgba ) {
return {
r: parseInt( rgba[1], 10 ),
g: parseInt( rgba[2], 10 ),
b: parseInt( rgba[3], 10 ),
a: parseFloat( rgba[4] )
}; };
} }