Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/webglimpse/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,34 @@ module Webglimpse {
* - hsl/hsla
* - named colors
*
* Also, this can take in an array of rgba values as an optimization. Example: [ 0.5, 0.5, 0.5, 0.5 ]
*
* Behavior is undefined for strings that are not in one of the listed notations.
*
* Note that different browsers may use different color values for the named colors.
* Typescript 1.0.1 doesn't seem to support the | operator, so using the 'any' type until webglimpse moves to a newer version.
*
*/
export var parseCssColor = ( function( ) {
var canvas = document.createElement( 'canvas' );
canvas.width = 1;
canvas.height = 1;
var g = canvas.getContext( '2d' );
return function( cssColorString : string ) : Color {
g.clearRect( 0, 0, 1, 1 );
g.fillStyle = cssColorString;
g.fillRect( 0, 0, 1, 1 );

var rgbaData = g.getImageData( 0, 0, 1, 1 ).data;
var R = rgbaData[ 0 ] / 255;
var G = rgbaData[ 1 ] / 255;
var B = rgbaData[ 2 ] / 255;
var A = rgbaData[ 3 ] / 255;
return rgba( R, G, B, A );
return function( cssColorString : any ) : Color {
if(!Array.isArray(cssColorString)) {
g.clearRect( 0, 0, 1, 1 );
g.fillStyle = cssColorString;
g.fillRect( 0, 0, 1, 1 );

var rgbaData = g.getImageData( 0, 0, 1, 1 ).data;
var R = rgbaData[ 0 ] / 255;
var G = rgbaData[ 1 ] / 255;
var B = rgbaData[ 2 ] / 255;
var A = rgbaData[ 3 ] / 255;
return rgba( R, G, B, A );
} else {
return rgba(cssColorString[0], cssColorString[1], cssColorString[2], cssColorString[3]);
}
}
} )( );

Expand Down