npm install rainbow-dash
In the browser, simply copy
/lib/rainbow-dash.min.js and include it in your site:
var rainbowDash = require'rainbow-dash';var gradeScale = rainbowDash100: 'rgb(0,0,255)'90: 'rgb(0,255,0)'80: 'rgb(255,255,0)'60: 'rgb(255,0,0)';gradeScale100; // Returns 'rgb(0,0,255)'gradeScale95; // Returns 'rgb(0,127,127)'gradeScale50; // Returns 'rgb(255,0,0)'var hexOut = rainbowDash outputFormat: 'hex'0: '#0000ff'0.5: 'rgb(0,255,0)'1: '#ff0000';hexOut0; // Returns '#0000ff'hexOut0.5; // Returns '#00ff00'hexOut1; // Returns '#ff0000'var uniformSpacing = rainbowDashuniformStops: trueinputRange: 0 5'#ff0000' '#ff9900' '#ffff00' '#00ff00' '#0000ff' '#ff00ff';uniformSpacing0; // Returns 'rgb(255,0,0)'uniformSpacing1; // Returns 'rgb(255,153,0)'uniformSpacing2; // Returns 'rgb(255,255,0)'uniformSpacing3; // Returns 'rgb(0,255,0)'uniformSpacing4; // Returns 'rgb(0,0,255)'uniformSpacing5; // Returns 'rgb(255,0,255)'
rainbow-dash precalculates interpolation constants for all specified ranges to make the interpolation calculation as fast as possible. This makes it faster than even using
<canvas> to calculate the desired values (in fact, if you don't want to be ridiculously wasteful with your memory, you need to scale the input, anyways, and lose any gain you could have had from that approach).
This means that rainbow-dash gets over 4.3 million samples/sec on Travis CI versus the less than 100k most commonly seen on jsperf for
<canvas> queries (with a maximum of 425k with Opera) and far less memory usage to do so.
Copyright (C) 2013 by Uber Technologies, Inc
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.