Giter VIP home page Giter VIP logo

dicopal.js's Introduction

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.

dicopal.js's People

Contributors

mthh avatar

Stargazers

Askar Yusupov avatar Jichen Li avatar Dom Charlesworth avatar Leo avatar Sylvain PULICANI avatar  avatar Alper Dincer avatar Benjamin Gudehus avatar vincjo avatar Saiful Azfar Ramlee avatar Qwerty avatar Jeff Heuer avatar  avatar Nick Forbes-Smith avatar  avatar Joe Davies avatar chris willis avatar Alberto avatar Simon Georget avatar antx avatar Kamil Slowikowski avatar Simon MELLERIN avatar Timothée Giraud avatar Julien Gaffuri avatar Graham McNeill avatar thomas avatar SHUKE avatar Thomas Ansart avatar wandergis avatar Nicolas Lambert avatar  avatar

Watchers

Simon Georget avatar Timothée Giraud avatar  avatar  avatar

dicopal.js's Issues

Possibility to add custom palettes

Bonjour!

In our app we combine custom colour palettes with Dicopal. I was wondering if there is a method available to add palettes to Dicopal, or if it could be added if it isn't too much trouble?

For example:

let myPalette = {
  id: "myCustomPalettes_6",
  name: "myCustomPalettes",
  number: 6,
  type: "sequential",
  colors: ["#ffeb99","#c7e9b4","#7fcdbb","#41b6c4","#104f99","#17256b"],
  provider: "Eurostat",
  url: "https://github.com/myPalette"
}
dicopal.addPalette(myPalette)

That way we could let dicopal handle all of the colour palette logic in our app!

Cheers,
Joe

Reverse asymmetric diverging colors

Good morning!

Me again :)

Would it be possible to add an option to getAsymmetricDivergingColors() which allows you to reverse the colour scheme?

For example, like the reverse argument that getColors() recieves.

Something like getAsymmetricDivergingColors('RdYlBu', 7, 2, true, true, reverse);

Thanks!
Joe

Angular compile Error: Module not found: Error: Default condition should be last one

Hi!

Once again, thank you for your work.

Just wanted to let you know about an error I found when installing the latest version of Dicopal in my Angular project:

From version 0.5.0 I am getting the following error after installation, when compiling our Angular 17 app:

Error: Module not found: Error: Default condition should be last one

Version 0.4.0 works perfectly, but 0.5.0 and 0.6.0 both return the same error.

I have found a workaround by importing Dicopal as a script in our index.html instead of npm/yarn.

Thanks!
Joe

Generate unbalanced diverging schemes

Is it possible to generate an unbalanced diverging colour scheme?

For example: say I have a diverging scheme with 6 classes but instead of it diverging at the middle (between classes 3 and 4) I want it to diverge between classes 2 and 3.

We are using dicopal in our map making application IMAGE: https://gisco-services.ec.europa.eu/image/screen/home (once again, thank you for your work!)

And we often encounter use cases where unbalanced diverging schemes are required (for example a dataset with a vast majority of positive values and only a few negative values).

Sequential palettes should progress from light to dark by default

Hi! Love the library by the way

I noticed that some sequential schemes dont progress from light to dark by default. I am aware of the 'reverse' option but I feel that light to dark should be the default for sequential schemes.

For example colorBrewer 'Greys' scheme is ordered light to dark but the cmocean 'Gray' palette is not.

Here are some resources to back this up:

https://web.natur.cuni.cz/~langhamr/lectures/vtfg1/mapinfo_2/barvy/colors.html#:~:text=Sequential%20Color%20Schemes,-Sequential%20data%20classes&text=Low%20data%20values%20are%20usually,progression%20should%20dominate%20the%20scheme.

https://colorbrewer2.org/learnmore/schemes_full.html

https://academy.datawrapper.de/article/140-what-to-consider-when-choosing-colors-for-data-visualization (no.7)

Thanks for your work!

Reverse option for asymmetric diverging colors not respecting left and right

Hi again!

I've been testing the latest release in our app and it works like a charm! our custom palettes are fully integrated with Dicopal :)

I noticed a minor issue when reversing unbalanced diverging schemes:

If you reverse, the left and right parameters are not reflected in the colors returned.

image

I suppose that the expected behavior in this example would be that there are 4 reds to the left and 2 blues to the right.

Screenshot taken from this notebook: https://observablehq.com/d/7a162d4ac1f308bb

Thanks again for your marvelous work!

Joe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.