Created
March 10, 2019 02:37
-
-
Save mr21/4f675ed404bfb20e36af0fb6191a1187 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Color = { | |
split( col ) { | |
switch ( col[ 0 ] ) { | |
case "r": return ( col[ 3 ] === "a" | |
? Color._rgbaExtract | |
: Color._rgbExtract )( col ); | |
case "#": return ( col.length < 6 | |
? Color._hex3Extract | |
: Color._hex6Extract )( col ); | |
} | |
}, | |
isDark( col ) { | |
const [ r, g, b ] = Color.split( col ); | |
return ( r + g + b ) / 3 < 128; | |
}, | |
// private: | |
_rgbExtract( col ) { | |
const rgb = col.substr( col.indexOf( "(" ) + 1 ).split( "," ); | |
rgb[ 0 ] = parseInt( rgb[ 0 ] ); | |
rgb[ 1 ] = parseInt( rgb[ 1 ] ); | |
rgb[ 2 ] = parseInt( rgb[ 2 ] ); | |
return rgb; | |
}, | |
_rgbaExtract( col ) { | |
const rgb = Color._rgbExtract( col ); | |
rgb[ 3 ] = parseFloat( rgb[ 3 ] ); | |
return rgb; | |
}, | |
_hex6Extract( col ) { | |
return [ | |
parseInt( col.substr( 1, 2 ), 16 ), | |
parseInt( col.substr( 3, 2 ), 16 ), | |
parseInt( col.substr( 5, 2 ), 16 ), | |
]; | |
}, | |
_hex3Extract( col ) { | |
const r = parseInt( col.substr( 1, 1 ), 16 ), | |
g = parseInt( col.substr( 2, 1 ), 16 ), | |
b = parseInt( col.substr( 3, 1 ), 16 ); | |
return [ | |
r * 16 + r, | |
g * 16 + g, | |
b * 16 + b, | |
]; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment