I would like to know if there is a simple algorithm (or jQuery plugin) to select distinguishable colors, up to about 20 different colors. If there is not, where can I find an array of distinguishable colors that I could use directly ?
My use case is to generate different colors for pie charts.
There is no way to do this using CSS only. CSS doesn't support logical statements because CSS is purely deterministic (there is nothing like array etc in CSS). We can use client-side JavaScript to select a random color from the array.
Import: Use Javascript import method import randomColor package inside your React Component File.
My idea is to start from HSV color model and walk around the perimeter (hue) with maximal saturation and value:
function hsvToRgb(h, s, v) {
//... see e.g.: http://snipplr.com/view/14590
}
function distinctColors(count) {
var colors = [];
for(hue = 0; hue < 360; hue += 360 / count) {
colors.push(hsvToRgb(hue, 100, 100));
}
return colors;
}
The distinctColors(10)
produces:
[[255, 0, 0], [255, 153, 0], [204, 255, 0], [51, 255, 0], [0, 255, 102], [0, 255, 255], [0, 102, 255], [51, 0, 255], [204, 0, 255], [255, 0, 153]]
Hard to tell by only looking at RGB values, but they should be as different as possible. I have taken the hsvToRgb()
implementation from here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With