rainbow-dash

Very fast color interpolation in Javascript

npm install rainbow-dash
20 downloads in the last month

rainbow-dash NPM version Dependency Status Build Status Coverage Status

Rainbow Dash browser support

Very fast color interpolation in Javascript

Install

npm install rainbow-dash

In the browser, simply copy /lib/rainbow-dash.min.js and include it in your site:

<script type="text/javascript" src="rainbow-dash.min.js"></script>

Usage

var rainbowDash = require('rainbow-dash');

var gradeScale = rainbowDash({
    100: 'rgb(0,0,255)',
    90:  'rgb(0,255,0)',
    80:  'rgb(255,255,0)',
    60:  'rgb(255,0,0)'
});

gradeScale(100); // Returns 'rgb(0,0,255)'
gradeScale(95);  // Returns 'rgb(0,127,127)'
gradeScale(50);  // Returns 'rgb(255,0,0)'

var hexOut = rainbowDash({ outputFormat: 'hex' }, {
    0: '#0000ff',
    0.5: 'rgb(0,255,0)',
    1: '#ff0000'
});

hexOut(0); // Returns '#0000ff'
hexOut(0.5); // Returns '#00ff00'
hexOut(1); // Returns '#ff0000'

var uniformSpacing = rainbowDash({
    uniformStops: true,
    inputRange: [0, 5]
}, ['#ff0000', '#ff9900', '#ffff00', '#00ff00', '#0000ff', '#ff00ff']);

uniformSpacing(0); // Returns 'rgb(255,0,0)'
uniformSpacing(1); // Returns 'rgb(255,153,0)'
uniformSpacing(2); // Returns 'rgb(255,255,0)'
uniformSpacing(3); // Returns 'rgb(0,255,0)'
uniformSpacing(4); // Returns 'rgb(0,0,255)'
uniformSpacing(5); // Returns 'rgb(255,0,255)'

Performance

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.

License (MIT)

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.

npm loves you