Skip to content

riatelab/dicopal.js

Repository files navigation

Dicopal

Discrete color palettes (hundreds of them!) for JavaScript.

palettes

Dicopal offers color palettes from:

⏩ Browse all the available palettes
⏩ Example about the creation of asymmetric diverging palettes

Installation

NPM

Add the package to your project:

npm install dicopal

CDN

Add the script to your HTML:

<script src="https://unpkg.com/dicopal"></script>

Usage

Get a palette, by name and number of colors

const pal = getPalette('Pastel', 4); // Returns the "Pastel" palette with 4 colors
// {
//   "number": 4,
//   "type": "qualitative",
//   "name": "Pastel",
//   "id": "Pastel_4",
//   "colors": ["#66C5CC","#F6CF71","#F89C74","#DCB0F2"],
//   "provider": "cartocolors",
//   "url": "https://github.com/CartoDB/CartoColor/wiki/CARTOColor-Scheme-Names"
// }

Get a palette colors, by name and number of colors

const cols = getColors('Pastel', 4); // Returns the "Pastel" palette with 4 colors
// ["#66C5CC","#F6CF71","#F89C74","#DCB0F2"]

Colors can also be reversed:

const cols = getColors('Pastel', 4, true);
// ['#DCB0F2', '#F89C74', '#F6CF71', '#66C5CC']

List the existing palettes for a given number of colors

// Returns 135 instances of palette with 3 colors
const palettes = getPalettes({ number: 3 });

List the existing palettes for a given type (sequential, diverging, qualitative)

// Returns 160 instances of qualitative palettes
const palettes = getPalettes({ type: 'qualitative' });

List the existing palettes for a given provider (ColorBrewer, Tableau, etc.)

// Returns 265 instances of colorbrewer palettes
const palettes = getPalettes({ provider: 'colorbrewer' });

List the existing palettes for a given name (for example, 'Accent')

// Returns the 6 instances of the "Accent" palette
const palettes = getPalettes({ name: 'Accent' });

List the existing palettes that match a set of criteria

// Returns the 12 instances of the palettes that are qualitative and have 10 colors
const palettes = getPalettes({ type: 'qualitative', number: 10 });

All the palettes or more criteria

When no argument is provided, the getPalettes function returns all the palettes:

// Returns the 1600 instances of palettes
const allPalettes = getPalettes();

You can then filter the palettes yourself by any combination of criteria:

// Only sequential and diverging palettes from all providers except colorbrewer
// with between 3 and 12 colors
const palettes = allPalettes
  .filter((p) => (
    ['sequential', 'diverging'].includes(p.type)
    && p.provider !== 'colorbrewer'
    && p.number >= 3
    && p.number <= 12)
  );

List the existing providers

const providers = getPaletteProviders(); // Returns the 10 providers

List the existing types

const providers = getPaletteTypes(); // Returns the 3 types

List the existing palette names

// Returns the 179 names ('ArmyRose', 'BrBg', 'Accent', etc.)
const providers = getPaletteNames();
// Returns the 35 names ('BrBg', 'PRGn', etc.)
const providers = getPaletteNames('colorbrewer');

Get the number of classes for a given palette

// Returns an array of numbers, like [3, 4, 5, 6, 7, 8]
const numClasses = getPaletteNumbers('Pastel2');

Generating colors for asymmetric diverging palettes

The getAsymmetricDivergingPalette function enables the creation of asymmetric diverging palettes (e.g. 3 colors for the left side and 4 colors for the right side), either balanced (i.e. the perceptual distance between the colors is the same on both sides) or not.

It takes the following arguments:

  • divergingSchemeName (string): the name of the diverging scheme to use (e.g. 'RdYlBu')
  • leftNumber (number): the number of colors to use on the left side
  • rightNumber (number): the number of colors to use on the right side
  • centralClass (boolean - optional): whether to include a central class (default: true)
  • balanced (boolean - optional): whether to balance the colors on both sides (default: false)
  • reverse (boolean - optional): whether to reverse the palette (default: false)

Balanced

const pal = getAsymmetricDivergingColors('RdYlBu', 7, 2, true, true);

Unbalanced

const pal = getAsymmetricDivergingColors('RdYlBu', 7, 2, true, false);

Generating colors for (interpolated) sequential palettes

Sometimes, a palette exists only in a limited number of colors (e.g. 3-to-9 colors) but you need a palette with a different number of colors (e.g. 12 colors).

The getSequentialColors function enables the creation of interpolated sequential palettes with a custom number of colors.

It takes the following arguments:

  • sequentialSchemeName (string): the name of the sequential scheme to use (e.g. 'Blues')
  • classNumber (number): the number of colors to use
  • reverse (boolean - optional): whether to reverse the palette (default: false)
const pal = getSequentialColors('Blues', 12);

Add your palettes to Dicopal to benefit from the API

You can add your own palettes to Dicopal at runtime:

// Add a qualitative palette
addPalette({
  type: 'qualitative', // Mandatory, amongst ('diverging', 'qualitative', 'sequential')
  name: 'MyPalette', // Mandatory, string
  colors: ['#FF0000', '#00FF00', '#0000FF'], // Mandatory, array of HEX colors as string
  provider: 'MyOrganisation', // Mandatory, string
  url: 'https://example.com' // Optional, string
});

// Add a sequential palette
addPalette({
  type: 'sequential',
  name: 'MySequentialPalette',
  colors: ['#FF0000', '#FF3300', '#FF6600', '#FF9900', '#FFCC00', '#FFFF00', '#FFFF33'],
  provider: 'MyOrganisation',
  url: 'https://example.com'
});

// Note that for the 'getAsymmetricDivergingColors' function to work correctly
// on the added palette, you must add at least two variations of the palette,
// one with a central class (and at least a total of 5 colors) and one without
// (and at least a total of 4 colors).
addPalette({
  name: 'NewDivergingPalette',
  type: 'diverging',
  colors: ['#D7191C', '#FDAE61', '#d7d7d7', '#ABDDA4', '#35AF24'],
  provider: 'MyOrg',
  url: 'https://example.com',
});

addPalette({
  name: 'NewDivergingPalette',
  type: 'diverging',
  colors: ['#D7191C', '#efc091', '#b8e1b2', '#35AF24'],
  provider: 'MyOrg',
  url: 'https://example.com',
});

You can then use the getPalette, getColors, getPalettes, getPaletteProviders, getPaletteTypes, getPaletteNames, getPaletteNumbers, getAsymmetricDivergingColors and getSequentialColors functions as usual.

Not a fan of the proposed API ? Just get the raw description of the palettes and use them as you wish

For a given provider:

getRawData('colorbrewer');

For all the provider (default):

getRawData();

Contributing

Contributions of all kinds are welcome, through issues and pull requests.

If you use the library and feel that the API could be improved / simplified / enriched / etc., don't hesitate to come and discuss it in the issues!

Other information

Palette information is stored in the src/palette.json file. It is generated in Python from various sources, notably the palettable Python library (authored by Matt Davis) and the dicopal RDF vocabulary which both provide a list of palettes with their colors and metadata as well as from Joshua Stevens' palettes.

License

Apache-2.0. See LICENSE for details.