diff --git a/src/components/colorscale/extract_scale.js b/src/components/colorscale/extract_scale.js new file mode 100644 index 00000000000..845b0fde95d --- /dev/null +++ b/src/components/colorscale/extract_scale.js @@ -0,0 +1,35 @@ +/** +* Copyright 2012-2016, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + + +'use strict'; + +/** + * Extract colorscale into numeric domain and color range. + * + * @param {array} scl colorscale array of arrays + * @param {number} cmin minimum color value (used to clamp scale) + * @param {number} cmax maximum color value (used to clamp scale) + */ +module.exports = function extractScale(scl, cmin, cmax) { + var N = scl.length, + domain = new Array(N), + range = new Array(N); + + for(var i = 0; i < N; i++) { + var si = scl[i]; + + domain[i] = cmin + si[0] * (cmax - cmin); + range[i] = si[1]; + } + + return { + domain: domain, + range: range + }; +}; diff --git a/src/components/colorscale/index.js b/src/components/colorscale/index.js index 6da3ccc4532..de016dc8d4f 100644 --- a/src/components/colorscale/index.js +++ b/src/components/colorscale/index.js @@ -27,4 +27,6 @@ exports.getScale = require('./get_scale'); exports.flipScale = require('./flip_scale'); -exports.makeScaleFunction = require('./make_scale_function'); +exports.extractScale = require('./extract_scale'); + +exports.makeColorScaleFunc = require('./make_color_scale_func'); diff --git a/src/components/colorscale/make_color_scale_func.js b/src/components/colorscale/make_color_scale_func.js new file mode 100644 index 00000000000..984a04559b5 --- /dev/null +++ b/src/components/colorscale/make_color_scale_func.js @@ -0,0 +1,94 @@ +/** +* Copyright 2012-2016, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + + +'use strict'; + +var d3 = require('d3'); +var tinycolor = require('tinycolor2'); +var isNumeric = require('fast-isnumeric'); + +var Color = require('../color'); + +/** + * General colorscale function generator. + * + * @param {object} specs output of Colorscale.extractScale or precomputed domain, range. + * - domain {array} + * - range {array} + * + * @param {object} opts + * - noNumericCheck {boolean} if true, scale func bypasses numeric checks + * - returnArray {boolean} if true, scale func return 4-item array instead of color strings + * + * @return {function} + */ +module.exports = function makeColorScaleFunc(specs, opts) { + opts = opts || {}; + + var domain = specs.domain, + range = specs.range, + N = range.length, + _range = new Array(N); + + for(var i = 0; i < N; i++) { + var rgba = tinycolor(range[i]).toRgb(); + _range[i] = [rgba.r, rgba.g, rgba.b, rgba.a]; + } + + var _sclFunc = d3.scale.linear() + .domain(domain) + .range(_range) + .clamp(true); + + var noNumericCheck = opts.noNumericCheck, + returnArray = opts.returnArray, + sclFunc; + + if(noNumericCheck && returnArray) { + sclFunc = _sclFunc; + } + else if(noNumericCheck) { + sclFunc = function(v) { + return colorArray2rbga(_sclFunc(v)); + }; + } + else if(returnArray) { + sclFunc = function(v) { + if(isNumeric(v)) return _sclFunc(v); + else if(tinycolor(v).isValid()) return v; + else return Color.defaultLine; + }; + } + else { + sclFunc = function(v) { + if(isNumeric(v)) return colorArray2rbga(_sclFunc(v)); + else if(tinycolor(v).isValid()) return v; + else return Color.defaultLine; + }; + } + + // colorbar draw looks into the d3 scale closure for domain and range + + sclFunc.domain = _sclFunc.domain; + + sclFunc.range = function() { return range; }; + + return sclFunc; +}; + +function colorArray2rbga(colorArray) { + var colorObj = { + r: colorArray[0], + g: colorArray[1], + b: colorArray[2], + a: colorArray[3] + }; + + return tinycolor(colorObj).toRgbString(); +} diff --git a/src/components/colorscale/make_scale_function.js b/src/components/colorscale/make_scale_function.js deleted file mode 100644 index 041217e4e37..00000000000 --- a/src/components/colorscale/make_scale_function.js +++ /dev/null @@ -1,47 +0,0 @@ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - - -'use strict'; - -var d3 = require('d3'); -var tinycolor = require('tinycolor2'); -var isNumeric = require('fast-isnumeric'); - -var Lib = require('../../lib'); -var Color = require('../color'); - - -module.exports = function makeScaleFunction(scl, cmin, cmax) { - var N = scl.length, - domain = new Array(N), - range = new Array(N), - si; - - for(var i = 0; i < N; i++) { - si = scl[i]; - domain[i] = cmin + si[0] * (cmax - cmin); - range[i] = tinycolor(si[1]).toRgb(); - } - - var sclFunc = d3.scale.linear() - .domain(domain) - .interpolate(d3.interpolateObject) - .range(range); - - return function(v) { - if(isNumeric(v)) { - var sclVal = Lib.constrain(v, cmin, cmax), - colorObj = sclFunc(sclVal); - - return tinycolor(colorObj).toRgbString(); - } - else if(tinycolor(v).isValid()) return v; - else return Color.defaultLine; - }; -}; diff --git a/src/components/drawing/index.js b/src/components/drawing/index.js index b4f1c5581e5..34e2572bc8d 100644 --- a/src/components/drawing/index.js +++ b/src/components/drawing/index.js @@ -331,7 +331,10 @@ drawing.tryColorscale = function(cont, contIn, prefix) { Lib.nestedProperty(contIn, prefix + 'cmin').set(min); Lib.nestedProperty(contIn, prefix + 'cmax').set(max); } - return Colorscale.makeScaleFunction(scl, min, max); + + return Colorscale.makeColorScaleFunc( + Colorscale.extractScale(scl, min, max) + ); } else return Lib.identity; }; diff --git a/src/lib/gl_format_color.js b/src/lib/gl_format_color.js index 79937b6a7dc..73af3ca64e2 100644 --- a/src/lib/gl_format_color.js +++ b/src/lib/gl_format_color.js @@ -12,7 +12,7 @@ var tinycolor = require('tinycolor2'); var isNumeric = require('fast-isnumeric'); -var makeScaleFunction = require('../components/colorscale/make_scale_function'); +var Colorscale = require('../components/colorscale'); var colorDflt = require('../components/color/attributes').defaultLine; var str2RgbaArray = require('./str2rgbarray'); @@ -42,8 +42,12 @@ function formatColor(containerIn, opacityIn, len) { var sclFunc, getColor, getOpacity, colori, opacityi; if(containerIn.colorscale !== undefined) { - sclFunc = makeScaleFunction( - containerIn.colorscale, containerIn.cmin, containerIn.cmax + sclFunc = Colorscale.makeColorScaleFunc( + Colorscale.extractScale( + containerIn.colorscale, + containerIn.cmin, + containerIn.cmax + ) ); } else sclFunc = validateColor; diff --git a/src/traces/choropleth/plot.js b/src/traces/choropleth/plot.js index 2b787c6b1a7..07e92f94d5a 100644 --- a/src/traces/choropleth/plot.js +++ b/src/traces/choropleth/plot.js @@ -15,9 +15,8 @@ var Axes = require('../../plots/cartesian/axes'); var Fx = require('../../plots/cartesian/graph_interact'); var Color = require('../../components/color'); var Drawing = require('../../components/drawing'); +var Colorscale = require('../../components/colorscale'); -var getColorscale = require('../../components/colorscale/get_scale'); -var makeScaleFunction = require('../../components/colorscale/make_scale_function'); var getTopojsonFeatures = require('../../lib/topojson_utils').getTopojsonFeatures; var locationToFeature = require('../../lib/geo_location_utils').locationToFeature; var arrayToCalcItem = require('../../lib/array_to_calc_item'); @@ -151,11 +150,15 @@ plotChoropleth.style = function(geo) { var trace = calcTrace[0].trace, s = d3.select(this), marker = trace.marker || {}, - markerLine = marker.line || {}, - zmin = trace.zmin, - zmax = trace.zmax, - scl = getColorscale(trace.colorscale), - sclFunc = makeScaleFunction(scl, zmin, zmax); + markerLine = marker.line || {}; + + var sclFunc = Colorscale.makeColorScaleFunc( + Colorscale.extractScale( + trace.colorscale, + trace.zmin, + trace.zmax + ) + ); s.selectAll('path.choroplethlocation') .each(function(pt) { diff --git a/src/traces/contour/make_color_map.js b/src/traces/contour/make_color_map.js index 0b37b4ee20d..9227d0fbf73 100644 --- a/src/traces/contour/make_color_map.js +++ b/src/traces/contour/make_color_map.js @@ -10,9 +10,7 @@ 'use strict'; var d3 = require('d3'); - -var getColorscale = require('../../components/colorscale/get_scale'); - +var Colorscale = require('../../components/colorscale'); module.exports = function makeColorMap(trace) { var contours = trace.contours, @@ -22,7 +20,7 @@ module.exports = function makeColorMap(trace) { nc = Math.floor((end + cs / 10 - start) / cs) + 1, extra = contours.coloring === 'lines' ? 0 : 1; - var scl = getColorscale(trace.colorscale), + var scl = trace.colorscale, len = scl.length; var domain = new Array(len), @@ -69,10 +67,10 @@ module.exports = function makeColorMap(trace) { } } - var colorMap = d3.scale.linear() - .interpolate(d3.interpolateRgb) - .domain(domain) - .range(range); - - return colorMap; + return Colorscale.makeColorScaleFunc({ + domain: domain, + range: range, + }, { + noNumericCheck: true + }); }; diff --git a/src/traces/heatmap/colorbar.js b/src/traces/heatmap/colorbar.js index 34b318f6afb..fdaf5a54215 100644 --- a/src/traces/heatmap/colorbar.js +++ b/src/traces/heatmap/colorbar.js @@ -9,19 +9,17 @@ 'use strict'; -var d3 = require('d3'); var isNumeric = require('fast-isnumeric'); var Lib = require('../../lib'); var Plots = require('../../plots/plots'); -var getColorscale = require('../../components/colorscale/get_scale'); +var Colorscale = require('../../components/colorscale'); var drawColorbar = require('../../components/colorbar/draw'); module.exports = function colorbar(gd, cd) { var trace = cd[0].trace, cbId = 'cb' + trace.uid, - scl = getColorscale(trace.colorscale), zmin = trace.zmin, zmax = trace.zmax; @@ -36,9 +34,16 @@ module.exports = function colorbar(gd, cd) { } var cb = cd[0].t.cb = drawColorbar(gd, cbId); - cb.fillcolor(d3.scale.linear() - .domain(scl.map(function(v) { return zmin + v[0] * (zmax - zmin); })) - .range(scl.map(function(v) { return v[1]; }))) + var sclFunc = Colorscale.makeColorScaleFunc( + Colorscale.extractScale( + trace.colorscale, + zmin, + zmax + ), + { noNumericCheck: true } + ); + + cb.fillcolor(sclFunc) .filllevels({start: zmin, end: zmax, size: (zmax - zmin) / 254}) .options(trace.colorbar)(); }; diff --git a/src/traces/heatmap/plot.js b/src/traces/heatmap/plot.js index e1d6694ddd1..21d530f8c80 100644 --- a/src/traces/heatmap/plot.js +++ b/src/traces/heatmap/plot.js @@ -9,12 +9,11 @@ 'use strict'; -var d3 = require('d3'); var tinycolor = require('tinycolor2'); var Registry = require('../../registry'); var Lib = require('../../lib'); -var getColorscale = require('../../components/colorscale/get_scale'); +var Colorscale = require('../../components/colorscale'); var xmlnsNamespaces = require('../../constants/xmlns_namespaces'); var maxRowLength = require('./max_row_length'); @@ -45,9 +44,6 @@ function plotOne(gd, plotinfo, cd) { } var z = cd[0].z, - min = trace.zmin, - max = trace.zmax, - scl = getColorscale(trace.colorscale), x = cd[0].x, y = cd[0].y, isContour = Registry.traceIs(trace, 'contour'), @@ -170,15 +166,14 @@ function plotOne(gd, plotinfo, cd) { canvas.height = canvasH; var context = canvas.getContext('2d'); - // interpolate for color scale - // use an array instead of color strings, so we preserve alpha - var s = d3.scale.linear() - .domain(scl.map(function(si) { return si[0]; })) - .range(scl.map(function(si) { - var c = tinycolor(si[1]).toRgb(); - return [c.r, c.g, c.b, c.a]; - })) - .clamp(true); + var sclFunc = Colorscale.makeColorScaleFunc( + Colorscale.extractScale( + trace.colorscale, + trace.zmin, + trace.zmax + ), + { noNumericCheck: true, returnArray: true } + ); // map brick boundaries to image pixels var xpx, @@ -289,7 +284,7 @@ function plotOne(gd, plotinfo, cd) { function setColor(v, pixsize) { if(v !== undefined) { - var c = s((v - min) / (max - min)); + var c = sclFunc(v); c[0] = Math.round(c[0]); c[1] = Math.round(c[1]); c[2] = Math.round(c[2]); diff --git a/src/traces/scatter/colorbar.js b/src/traces/scatter/colorbar.js index 2a3e7a08b13..d98d23e962f 100644 --- a/src/traces/scatter/colorbar.js +++ b/src/traces/scatter/colorbar.js @@ -9,12 +9,11 @@ 'use strict'; -var d3 = require('d3'); var isNumeric = require('fast-isnumeric'); var Lib = require('../../lib'); var Plots = require('../../plots/plots'); -var getColorscale = require('../../components/colorscale/get_scale'); +var Colorscale = require('../../components/colorscale'); var drawColorbar = require('../../components/colorbar/draw'); @@ -25,16 +24,15 @@ module.exports = function colorbar(gd, cd) { gd._fullLayout._infolayer.selectAll('.' + cbId).remove(); - // TODO unify Scatter.colorbar and Heatmap.colorbar - // TODO make Plotly[module].colorbar support multiple colorbar per trace + // TODO unify scatter and heatmap colorbar + // TODO make Colorbar.draw support multiple colorbar per trace if((marker === undefined) || !marker.showscale) { Plots.autoMargin(gd, cbId); return; } - var scl = getColorscale(marker.colorscale), - vals = marker.color, + var vals = marker.color, cmin = marker.cmin, cmax = marker.cmax; @@ -42,10 +40,16 @@ module.exports = function colorbar(gd, cd) { if(!isNumeric(cmax)) cmax = Lib.aggNums(Math.max, null, vals); var cb = cd[0].t.cb = drawColorbar(gd, cbId); - - cb.fillcolor(d3.scale.linear() - .domain(scl.map(function(v) { return cmin + v[0] * (cmax - cmin); })) - .range(scl.map(function(v) { return v[1]; }))) + var sclFunc = Colorscale.makeColorScaleFunc( + Colorscale.extractScale( + marker.colorscale, + cmin, + cmax + ), + { noNumericCheck: true } + ); + + cb.fillcolor(sclFunc) .filllevels({start: cmin, end: cmax, size: (cmax - cmin) / 254}) .options(marker.colorbar)(); }; diff --git a/src/traces/scattermapbox/calc.js b/src/traces/scattermapbox/calc.js index a3c6f50df57..ecf8d9ee8cb 100644 --- a/src/traces/scattermapbox/calc.js +++ b/src/traces/scattermapbox/calc.js @@ -12,8 +12,8 @@ var isNumeric = require('fast-isnumeric'); var Lib = require('../../lib'); -var hasColorscale = require('../../components/colorscale/has_colorscale'); -var makeColorScaleFn = require('../../components/colorscale/make_scale_function'); +var Colorscale = require('../../components/colorscale'); + var subtypes = require('../scatter/subtypes'); var calcMarkerColorscale = require('../scatter/colorscale_calc'); var makeBubbleSizeFn = require('../scatter/make_bubble_size_func'); @@ -31,8 +31,14 @@ module.exports = function calc(gd, trace) { calcMarkerColorscale(trace); - var colorFn = hasColorscale(trace, 'marker') ? - makeColorScaleFn(marker.colorscale, marker.cmin, marker.cmax) : + var colorFn = Colorscale.hasColorscale(trace, 'marker') ? + Colorscale.makeColorScaleFunc( + Colorscale.extractScale( + marker.colorscale, + marker.cmin, + marker.cmax + ) + ) : Lib.identity; var sizeFn = subtypes.isBubble(trace) ? diff --git a/src/traces/surface/colorbar.js b/src/traces/surface/colorbar.js index 02cb8ef2f04..044f26db4b8 100644 --- a/src/traces/surface/colorbar.js +++ b/src/traces/surface/colorbar.js @@ -9,25 +9,23 @@ 'use strict'; -var d3 = require('d3'); var isNumeric = require('fast-isnumeric'); var Lib = require('../../lib'); var Plots = require('../../plots/plots'); -var getColorscale = require('../../components/colorscale/get_scale'); +var Colorscale = require('../../components/colorscale'); var drawColorbar = require('../../components/colorbar/draw'); module.exports = function colorbar(gd, cd) { var trace = cd[0].trace, cbId = 'cb' + trace.uid, - scl = getColorscale(trace.colorscale), - zmin = trace.cmin, - zmax = trace.cmax, + cmin = trace.cmin, + cmax = trace.cmax, vals = trace.surfacecolor || trace.z; - if(!isNumeric(zmin)) zmin = Lib.aggNums(Math.min, null, vals); - if(!isNumeric(zmax)) zmax = Lib.aggNums(Math.max, null, vals); + if(!isNumeric(cmin)) cmin = Lib.aggNums(Math.min, null, vals); + if(!isNumeric(cmax)) cmax = Lib.aggNums(Math.max, null, vals); gd._fullLayout._infolayer.selectAll('.' + cbId).remove(); @@ -37,9 +35,16 @@ module.exports = function colorbar(gd, cd) { } var cb = cd[0].t.cb = drawColorbar(gd, cbId); - cb.fillcolor(d3.scale.linear() - .domain(scl.map(function(v) { return zmin + v[0] * (zmax - zmin); })) - .range(scl.map(function(v) { return v[1]; }))) - .filllevels({start: zmin, end: zmax, size: (zmax - zmin) / 254}) + var sclFunc = Colorscale.makeColorScaleFunc( + Colorscale.extractScale( + trace.colorscale, + cmin, + cmax + ), + { noNumericCheck: true } + ); + + cb.fillcolor(sclFunc) + .filllevels({start: cmin, end: cmax, size: (cmax - cmin) / 254}) .options(trace.colorbar)(); }; diff --git a/test/image/baselines/colorscale_opacity.png b/test/image/baselines/colorscale_opacity.png index 7816120ecce..d7c4208301c 100644 Binary files a/test/image/baselines/colorscale_opacity.png and b/test/image/baselines/colorscale_opacity.png differ diff --git a/test/image/baselines/geo_bubbles-colorscales.png b/test/image/baselines/geo_bubbles-colorscales.png index 57c73a839bc..1f0fba6a561 100644 Binary files a/test/image/baselines/geo_bubbles-colorscales.png and b/test/image/baselines/geo_bubbles-colorscales.png differ diff --git a/test/image/baselines/gl3d_scatter-colorscale-marker.png b/test/image/baselines/gl3d_scatter-colorscale-marker.png index 7a4c4156a99..f3b29555313 100644 Binary files a/test/image/baselines/gl3d_scatter-colorscale-marker.png and b/test/image/baselines/gl3d_scatter-colorscale-marker.png differ diff --git a/test/image/mocks/colorscale_opacity.json b/test/image/mocks/colorscale_opacity.json index 9da112f70dd..29c4b93b9db 100644 --- a/test/image/mocks/colorscale_opacity.json +++ b/test/image/mocks/colorscale_opacity.json @@ -1,6 +1,523 @@ { "data": [ { + "type": "contour", + "z":[ + [ + 1, + 1.126592, + 1.251148, + 1.371662, + 1.486197, + 1.592908, + 1.690079, + 1.776146, + 1.849725, + 1.909632, + 1.954902, + 1.984808, + 1.998867, + 1.996855, + 1.978802, + 1.945001, + 1.895994, + 1.83257, + 1.75575, + 1.666769, + 1.56706, + 1.458227, + 1.34202, + 1.220311, + 1.095056, + 0.968272, + 0.841999, + 0.718267, + 0.599069, + 0.486323, + 0.381841, + 0.287306, + 0.204238, + 0.133975, + 0.077646, + 0.036158, + 0.010179, + 0.000126, + 0.006162, + 0.028188, + 0.065852, + 0.118547, + 0.185424, + 0.265408, + 0.357212, + 0.459359, + 0.570205, + 0.687967, + 0.810749, + 0.936576, + 1.063424, + 1.189251, + 1.312033, + 1.429795, + 1.540641, + 1.642788, + 1.734592, + 1.814576, + 1.881453, + 1.934148, + 1.971812, + 1.993838, + 1.999874, + 1.989821, + 1.963842, + 1.922354, + 1.866025, + 1.795762, + 1.712694, + 1.618159, + 1.513677, + 1.400931, + 1.281733, + 1.158001, + 1.031728, + 0.904944, + 0.779689, + 0.65798, + 0.541773, + 0.43294, + 0.333231, + 0.24425, + 0.16743, + 0.104006, + 0.054999, + 0.021198, + 0.003145, + 0.001133, + 0.015192, + 0.045098, + 0.090368, + 0.150275, + 0.223854, + 0.309921, + 0.407092, + 0.513803, + 0.628338, + 0.748852, + 0.873408, + 1 + ], + [ + 0.991955, + 1.118547, + 1.243103, + 1.363617, + 1.478152, + 1.584863, + 1.682034, + 1.768101, + 1.84168, + 1.901587, + 1.946857, + 1.976763, + 1.990822, + 1.98881, + 1.970757, + 1.936956, + 1.887949, + 1.824525, + 1.747704, + 1.658724, + 1.559015, + 1.450181, + 1.333975, + 1.212265, + 1.087011, + 0.960227, + 0.833953, + 0.710222, + 0.591024, + 0.478277, + 0.373796, + 0.279261, + 0.196193, + 0.125929, + 0.069601, + 0.028113, + 0.002133, + -0.007919, + -0.001884, + 0.020143, + 0.057807, + 0.110501, + 0.177379, + 0.257363, + 0.349167, + 0.451314, + 0.56216, + 0.679921, + 0.802704, + 0.928531, + 1.055379, + 1.181206, + 1.303988, + 1.42175, + 1.532596, + 1.634742, + 1.726547, + 1.806531, + 1.873408, + 1.926103, + 1.963766, + 1.985793, + 1.991829, + 1.981776, + 1.955797, + 1.914309, + 1.85798, + 1.787717, + 1.704649, + 1.610114, + 1.505632, + 1.392885, + 1.273687, + 1.149956, + 1.023683, + 0.896899, + 0.771644, + 0.649935, + 0.533728, + 0.424895, + 0.325186, + 0.236205, + 0.159385, + 0.095961, + 0.046954, + 0.013152, + -0.0049, + -0.006913, + 0.007147, + 0.037053, + 0.082323, + 0.142229, + 0.215808, + 0.301876, + 0.399047, + 0.505758, + 0.620292, + 0.740807, + 0.865362, + 0.991955 + ], + [ + 0.630553, + 0.757145, + 0.881701, + 1.002215, + 1.116749, + 1.223461, + 1.320632, + 1.406699, + 1.480278, + 1.540185, + 1.585455, + 1.61536, + 1.62942, + 1.627407, + 1.609355, + 1.575553, + 1.526546, + 1.463123, + 1.386302, + 1.297322, + 1.197613, + 1.088779, + 0.972573, + 0.850863, + 0.725609, + 0.598825, + 0.472551, + 0.34882, + 0.229622, + 0.116875, + 0.012394, + -0.082142, + -0.165209, + -0.235473, + -0.291802, + -0.333289, + -0.359269, + -0.369321, + -0.363286, + -0.341259, + -0.303595, + -0.250901, + -0.184023, + -0.104039, + -0.012235, + 0.089912, + 0.200758, + 0.318519, + 0.441301, + 0.567129, + 0.693977, + 0.819804, + 0.942586, + 1.060348, + 1.171193, + 1.27334, + 1.365144, + 1.445129, + 1.512006, + 1.564701, + 1.602364, + 1.624391, + 1.630427, + 1.620374, + 1.594395, + 1.552907, + 1.496578, + 1.426315, + 1.343247, + 1.248712, + 1.14423, + 1.031483, + 0.912285, + 0.788554, + 0.662281, + 0.535497, + 0.410242, + 0.288533, + 0.172326, + 0.063493, + -0.036216, + -0.125197, + -0.202017, + -0.265441, + -0.314448, + -0.34825, + -0.366302, + -0.368315, + -0.354255, + -0.32435, + -0.279079, + -0.219173, + -0.145594, + -0.059526, + 0.037645, + 0.144356, + 0.25889, + 0.379405, + 0.50396, + 0.630553 + ], + [ + 0.527225, + 0.653818, + 0.778373, + 0.898888, + 1.013422, + 1.120133, + 1.217304, + 1.303372, + 1.376951, + 1.436857, + 1.482128, + 1.512033, + 1.526093, + 1.52408, + 1.506028, + 1.472226, + 1.423219, + 1.359795, + 1.282975, + 1.193994, + 1.094285, + 0.985452, + 0.869246, + 0.747536, + 0.622282, + 0.495498, + 0.369224, + 0.245493, + 0.126295, + 0.013548, + -0.090934, + -0.185469, + -0.268536, + -0.3388, + -0.395129, + -0.436617, + -0.462596, + -0.472649, + -0.466613, + -0.444586, + -0.406922, + -0.354228, + -0.28735, + -0.207366, + -0.115562, + -0.013415, + 0.097431, + 0.215192, + 0.337974, + 0.463802, + 0.590649, + 0.716477, + 0.839259, + 0.95702, + 1.067866, + 1.170013, + 1.261817, + 1.341801, + 1.408679, + 1.461373, + 1.499037, + 1.521064, + 1.5271, + 1.517047, + 1.491068, + 1.44958, + 1.393251, + 1.322987, + 1.23992, + 1.145384, + 1.040903, + 0.928156, + 0.808958, + 0.685227, + 0.558953, + 0.432169, + 0.306915, + 0.185205, + 0.068999, + -0.039834, + -0.139544, + -0.228524, + -0.305344, + -0.368768, + -0.417775, + -0.451577, + -0.469629, + -0.471642, + -0.457582, + -0.427677, + -0.382407, + -0.3225, + -0.248921, + -0.162854, + -0.065682, + 0.041029, + 0.155563, + 0.276077, + 0.400633, + 0.527225 + ], + [ + 0.415415, + 0.542007, + 0.666563, + 0.787077, + 0.901612, + 1.008323, + 1.105494, + 1.191561, + 1.26514, + 1.325047, + 1.370317, + 1.400223, + 1.414282, + 1.41227, + 1.394217, + 1.360416, + 1.311409, + 1.247985, + 1.171165, + 1.082184, + 0.982475, + 0.873642, + 0.757435, + 0.635726, + 0.510471, + 0.383687, + 0.257414, + 0.133682, + 0.014484, + -0.098262, + -0.202744, + -0.297279, + -0.380347, + -0.45061, + -0.506939, + -0.548427, + -0.574406, + -0.584459, + -0.578423, + -0.556397, + -0.518733, + -0.466038, + -0.399161, + -0.319177, + -0.227373, + -0.125226, + -0.01438, + 0.103382, + 0.226164, + 0.351991, + 0.478839, + 0.604666, + 0.727448, + 0.84521, + 0.956056, + 1.058203, + 1.150007, + 1.229991, + 1.296868, + 1.349563, + 1.387227, + 1.409253, + 1.415289, + 1.405236, + 1.379257, + 1.337769, + 1.28144, + 1.211177, + 1.128109, + 1.033574, + 0.929092, + 0.816346, + 0.697148, + 0.573416, + 0.447143, + 0.320359, + 0.195104, + 0.073395, + -0.042812, + -0.151645, + -0.251354, + -0.340335, + -0.417155, + -0.480579, + -0.529586, + -0.563387, + -0.58144, + -0.583452, + -0.569393, + -0.539487, + -0.494217, + -0.43431, + -0.360731, + -0.274664, + -0.177493, + -0.070782, + 0.043753, + 0.164267, + 0.288823, + 0.415415 + ] + ], + "colorscale": [[0, "rgba(255,0,0,0.2)"], [1, "rgba(0,255,0,0.8)"]] + }, + { + "dx": 15, "y": [ 5, 5, @@ -33,7 +550,7 @@ } ], "layout": { - "title": "Scatter Plot with a Color Dimension", + "title": "RGBA colorscales", "xaxis": { "range": [ -0.271356783919598, diff --git a/test/jasmine/tests/colorscale_test.js b/test/jasmine/tests/colorscale_test.js index 3610c17cdec..462d53c2a08 100644 --- a/test/jasmine/tests/colorscale_test.js +++ b/test/jasmine/tests/colorscale_test.js @@ -378,22 +378,24 @@ describe('Test colorscale:', function() { }); - describe('makeScaleFunction', function() { + describe('extractScale + makeColorScaleFunc', function() { var scale = [ - [0, 'rgb(5,10,172)'], - [0.35, 'rgb(106,137,247)'], - [0.5, 'rgb(190,190,190)'], - [0.6, 'rgb(220,170,132)'], - [0.7, 'rgb(230,145,90)'], - [1, 'rgb(178,10,28)'] - ], - scaleFunction = Colorscale.makeScaleFunction(scale, 2, 3); + [0, 'rgb(5,10,172)'], + [0.35, 'rgb(106,137,247)'], + [0.5, 'rgb(190,190,190)'], + [0.6, 'rgb(220,170,132)'], + [0.7, 'rgb(230,145,90)'], + [1, 'rgb(178,10,28)'] + ]; + + var specs = Colorscale.extractScale(scale, 2, 3); + var sclFunc = Colorscale.makeColorScaleFunc(specs); it('should constrain color array values between cmin and cmax', function() { - var color1 = scaleFunction(1), - color2 = scaleFunction(2), - color3 = scaleFunction(3), - color4 = scaleFunction(4); + var color1 = sclFunc(1), + color2 = sclFunc(2), + color3 = sclFunc(3), + color4 = sclFunc(4); expect(color1).toEqual(color2); expect(color1).toEqual('rgb(5, 10, 172)');