Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an array of colors (about 100) in JavaScript, but the colors must be quite distinct

Tags:

javascript

I want to create an array of colors in javascript, for that, I used the js file rainbowvis.js here, I have 100 colors, but they are not really distinct.

I should be able to see the difference because I will use this table to a chart.

Is it possible with rainbowvis.js or is there an other solution?

like image 646
Jayce Avatar asked Mar 19 '14 10:03

Jayce


People also ask

How do you color an array in JavaScript?

style. backgroundColor = 'rgb('+ rgb. join(',') +')'; If you want to constrict it to known colors, you can create an array of the colors and randomly select it like so.

What is an array of color?

To define the color or shading of object displayed on screen or printed on paper, color values are used, which are typically based on color models. Models may include a single color (such as for the grayscale model) or multiple colors.

What do you call to the array of different colors of light?

The separation of visible light into its different colors is known as dispersion. Each color is characteristic of a distinct wavelength; and different wavelengths of light waves will bend varying amounts upon passage through a prism.


1 Answers

To generate 100 colors (each color from 1 to 1000000 - change it if you need other range):

var colors = [];
while (colors.length < 100) {
    do {
        var color = Math.floor((Math.random()*1000000)+1);
    } while (colors.indexOf(color) >= 0);
    colors.push("#" + ("000000" + color.toString(16)).slice(-6));
}
console.log(colors);

FIDDLE

like image 178
ndpu Avatar answered Oct 12 '22 23:10

ndpu