@@ -16,20 +16,35 @@ var Colorscale = require('../components/colorscale');
16
16
var colorDflt = require ( '../components/color/attributes' ) . defaultLine ;
17
17
var isArrayOrTypedArray = require ( './array' ) . isArrayOrTypedArray ;
18
18
19
- var colorDfltRgba = rgba ( colorDflt ) ;
19
+ function filterRGBA ( colorIn , desiredType ) {
20
+ var colorOut = rgba ( colorIn , desiredType ) ;
21
+
22
+ if ( typeof colorIn === 'string' &&
23
+ ! colorIn . indexOf ( 'rgb' ) &&
24
+ colorIn . indexOf ( 'rgba' )
25
+ ) {
26
+ // drop alpha if not having rgb
27
+ colorOut = [ colorOut [ 0 ] , colorOut [ 1 ] , colorOut [ 2 ] ] ;
28
+ }
29
+ return colorOut ;
30
+ }
31
+
32
+ var colorDfltRgba = filterRGBA ( colorDflt ) ;
20
33
var opacityDflt = 1 ;
21
34
22
35
function calculateColor ( colorIn , opacityIn ) {
23
36
var colorOut = colorIn ;
24
- colorOut [ 3 ] *= opacityIn ;
37
+
38
+ if ( colorOut . length > 3 ) colorOut [ 3 ] *= opacityIn ;
39
+ else if ( opacityIn < 1 ) colorOut [ 3 ] = opacityIn ;
40
+
25
41
return colorOut ;
26
42
}
27
43
28
44
function validateColor ( colorIn ) {
29
45
if ( isNumeric ( colorIn ) ) return colorDfltRgba ;
30
46
31
- var colorOut = rgba ( colorIn ) ;
32
-
47
+ var colorOut = filterRGBA ( colorIn ) ;
33
48
return colorOut . length ? colorOut : colorDfltRgba ;
34
49
}
35
50
@@ -54,7 +69,7 @@ function formatColor(containerIn, opacityIn, len) {
54
69
if ( isArrayColorIn ) {
55
70
getColor = function ( c , i ) {
56
71
// FIXME: there is double work, considering that sclFunc does the opposite
57
- return c [ i ] === undefined ? colorDfltRgba : rgba ( sclFunc ( c [ i ] ) ) ;
72
+ return c [ i ] === undefined ? colorDfltRgba : filterRGBA ( sclFunc ( c [ i ] ) ) ;
58
73
} ;
59
74
} else getColor = validateColor ;
60
75
@@ -70,7 +85,7 @@ function formatColor(containerIn, opacityIn, len) {
70
85
opacityi = getOpacity ( opacityIn , i ) ;
71
86
colorOut [ i ] = calculateColor ( colori , opacityi ) ;
72
87
}
73
- } else colorOut = calculateColor ( rgba ( colorIn ) , opacityIn ) ;
88
+ } else colorOut = calculateColor ( filterRGBA ( colorIn ) , opacityIn ) ;
74
89
75
90
return colorOut ;
76
91
}
@@ -96,6 +111,7 @@ function parseColorScale(cont, alpha) {
96
111
}
97
112
98
113
module . exports = {
114
+ filterRGBA : filterRGBA ,
99
115
formatColor : formatColor ,
100
116
parseColorScale : parseColorScale
101
117
} ;
0 commit comments