|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +var d3 = require('d3'); |
| 13 | +var tinycolor = require('tinycolor2'); |
| 14 | +var isNumeric = require('fast-isnumeric'); |
| 15 | + |
| 16 | +var Color = require('../color'); |
| 17 | + |
| 18 | +/** |
| 19 | + * General colorscale function generator. |
| 20 | + * |
| 21 | + * @param {object} specs output of Colorscale.extractScale or precomputed domain, range. |
| 22 | + * - domain {array} |
| 23 | + * - range {array} |
| 24 | + * |
| 25 | + * @param {object} opts |
| 26 | + * - noNumericCheck {boolean} if true, scale func bypasses numeric checks |
| 27 | + * - returnArray {boolean} if true, scale func return 4-item array instead of color strings |
| 28 | + * |
| 29 | + * @return {function} |
| 30 | + */ |
| 31 | +module.exports = function makeColorScaleFunc(specs, opts) { |
| 32 | + opts = opts || {}; |
| 33 | + |
| 34 | + var domain = specs.domain, |
| 35 | + range = specs.range, |
| 36 | + N = range.length, |
| 37 | + _range = new Array(N); |
| 38 | + |
| 39 | + for(var i = 0; i < N; i++) { |
| 40 | + var rgba = tinycolor(range[i]).toRgb(); |
| 41 | + _range[i] = [rgba.r, rgba.g, rgba.b, rgba.a]; |
| 42 | + } |
| 43 | + |
| 44 | + var _sclFunc = d3.scale.linear() |
| 45 | + .domain(domain) |
| 46 | + .range(_range) |
| 47 | + .clamp(true); |
| 48 | + |
| 49 | + var noNumericCheck = opts.noNumericCheck, |
| 50 | + returnArray = opts.returnArray, |
| 51 | + sclFunc; |
| 52 | + |
| 53 | + if(noNumericCheck && returnArray) { |
| 54 | + sclFunc = _sclFunc; |
| 55 | + } |
| 56 | + else if(noNumericCheck) { |
| 57 | + sclFunc = function(v) { |
| 58 | + return colorArray2rbga(_sclFunc(v)); |
| 59 | + }; |
| 60 | + } |
| 61 | + else if(returnArray) { |
| 62 | + sclFunc = function(v) { |
| 63 | + if(isNumeric(v)) return _sclFunc(v); |
| 64 | + else if(tinycolor(v).isValid()) return v; |
| 65 | + else return Color.defaultLine; |
| 66 | + }; |
| 67 | + } |
| 68 | + else { |
| 69 | + sclFunc = function(v) { |
| 70 | + if(isNumeric(v)) return colorArray2rbga(_sclFunc(v)); |
| 71 | + else if(tinycolor(v).isValid()) return v; |
| 72 | + else return Color.defaultLine; |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + // colorbar draw looks into the d3 scale closure for domain and range |
| 77 | + |
| 78 | + sclFunc.domain = _sclFunc.domain; |
| 79 | + |
| 80 | + sclFunc.range = function() { return range; }; |
| 81 | + |
| 82 | + return sclFunc; |
| 83 | +}; |
| 84 | + |
| 85 | +function colorArray2rbga(colorArray) { |
| 86 | + var colorObj = { |
| 87 | + r: colorArray[0], |
| 88 | + g: colorArray[1], |
| 89 | + b: colorArray[2], |
| 90 | + a: colorArray[3] |
| 91 | + }; |
| 92 | + |
| 93 | + return tinycolor(colorObj).toRgbString(); |
| 94 | +} |
0 commit comments