@nqminds/colours
v0.2.10
Published
Colour utilities and functions for NQM projects
Downloads
16
Keywords
Readme
Colours
The colours module defines functions for creating consistent colour spaces across projects.
Colour cycles
Tools for creating uniform, or perceptually different colour for plot lines and charts. Functionality may need to be added to ensure colour contrast is preserved. i.e. a dark mode and light mode so that the mapping doesn't give a colour like light-yellow when the colours will be plotted on a white background.
Palette map
A class for keeping a consistent and distinct colour mappings between keys.
import { PaletteMap } from "@nqminds/colours";
const paletteMap = new PaletteMap(keys, options);
Parameters
keys: array of strings
options: object
- format: string. One of hex, rgb, cmyk, hsl.
- default = hex
- exclusions: array of colours to exclude. e.g. ["red", "green"].
- default = []
- format: string. One of hex, rgb, cmyk, hsl.
getColour()
const paletteMap = new PaletteMap(dataKeys, { format: "rgb" });
const colour = paletteMap.getColour("key");
get-palette
A function which returns an array of n distinct colour values in a specified format
import { getPalette } from "@nqminds/colours";
const palette = getPalette(paletteSize, options);
Parameters
paletteSize: number of colors to use
options: object
- format: string. One of hex, rgb, cmyk, hsl.
- default = hex
- exclusions: array of colours to exclude. e.g. ["red", "green"].
- default = []
- format: string. One of hex, rgb, cmyk, hsl.
Returns
Array of colour values of length paletteSize.
map-to-palette
A function which can be used to map data automatically to a color map.
It supports the following data types:
- Boolean - maps true/false to 2 bins
- Strings - maps each unique string to a bin (to be used on categorical variables with only a few values)
- Numbers - bins values into the specified number of intervals inferred from the values in the data, NaN, undefined and null values are ignored and linear or logarithmic intervals are supported. Interval bounds are rounded to the specified precision and if values of <= 0 are provided for a logarithmic scale colormap an extra interval from 0 to the minumum positive non-zero value is added to the generated intervals.
import { mapToPalette } from "@nqminds/colours";
const palette = getPalette(paletteSize, options);
const { paletteMap, getColour } = mapToPalette(
data,
"property.subProperty",
"number",
options
);
Parameters
- data: array of data objects
- field: path to the field in the data to map to a colormap
- type: string specifying data type
- options: object
- format: string. One of hex, rgb, cmyk, hsl.
- default = hex
- exclusions: array of colours to exclude. e.g. ["red", "green"].
- default = []
- paletteSize number of intervals to use for numeric values
- default = 10
- precision precision (significant figures) to use for interval boundries
- default = 2
- logarithmic whether to use a logarithmic color map (Linear is used by default)
- default = false
- colourMap an external colour map object (keys/values being keys/colours), entries from this colour map are only added to the palette map if data is present with the corresponding key. Can be used to ensure that particular keys always map to a particular colours
- format: string. One of hex, rgb, cmyk, hsl.
Returns
- Result
- Result.paletteMap - paletteMap object generated for supplied data
- Result.getColour - method which when passed a data object will return the mapped color value