paint-by-the-numbers
v1.1.0
Published
Based on Microsoft Excel's "Color Scales" conditional formatting, returns a color from the linear gradient between 2 or more color endpoints depending on the value.
Downloads
2
Readme
paint-by-the-numbers
A utility based on Microsoft Excel's "Color Scales" conditional formatter for number cells.
Usage
Constructor
You can use the Painter
class like this:
const Painter = require("paint-by-the-numbers");
let p = new Painter(min, max, ...colorStops);
where:
min
: the minimum numeric value of the rangemax
: the maximum numeric value of the rangecolorStops
: a comma-separated list of colors (min. 2). May be any value that can be parsed by Qix-'s color package as a color.
For example:
const Painter = require("paint-by-the-numbers");
let p = new Painter(0, 100, "#F00", "#0F0");
getRGBA(value)
Accepts a number as an argument, and outputs the color as an rgba()
string:
e.g.
let color = p.getRGBA(50);
console.log(color); // Prints 'rgba(128, 128, 0, 1)'
Values less than min
are set to min
, and values greater than max
are set to max
.
Adding more color stops
You can add more than 2 colors in the Painter
constructor. The first and last colors define the colors for the minimum and maximum values respectively. Any other colors in between are used to divide the range in equal segments.
For example, if you write:
let p = new Painter(0, 100, "#F00", "#0F0", "#00F");
then any value between 0 (or lower) and 50 will be in the linear gradient range between #F00 and #0F0. Any value between 50 and 100 will be in the linear gradient range between #0F0 and #00F.