0

I have around 1000 lines. I will make them as clusters upon receiving input from user how many clusters he wants. I want to give new color to each cluster. I do not want to specify color manually for each of these 1000 lines.

sample code:

var material = new THREE.LineBasicMaterial({ color: new THREE.Color("rgb(0, 102, 153)" // color: chroma.limits(data, 'e', 1)) });

I want to automatically choose new different rgb colors while I create material in a for loop for each lines in each cluster.

I have a solution using d3.js scalecategory. But this is confined to only 10 or 20 colors,

I tried with chroma.js too as above commented line. But of no use,

Edit1: Changed question to cluster of lines rather than each different lines.

user1
  • 391
  • 3
  • 27
  • `material.color.setRGB( Math.random(), Math.random(), Math.random() );` – WestLangley Mar 23 '17 at 18:50
  • can this work with cluster of lines. ? For instance, i want 2 or 3 lines have to follow same color. Actually, my goal is to give each cluster one different color. May be i should edit my question. Thank you for very quick comment. – user1 Mar 23 '17 at 18:52

1 Answers1

0

You could loan a function from this answer, and then it would be easy:

var usedColors = [];

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getNewColor() {
    var color;

    while(!color || usedColors.indexOf(color) !== false) {
        color = 'rgb(' + getRandomInt(0, 255) + ', ' + getRandomInt(0, 255) + ', ' + getRandomInt(0, 255) + ')';
    }

    usedColors.push(color);

    return color;
}

var newColor = new THREE.Color(getNewColor());

var material = new THREE.LineBasicMaterial({ color: newColor });

This guarantees you get a new color each time. Be warned though that with a lot of colors the process will get slower and slower, until you're finally in an infinite loop. With a thousand colors you should be safe though.

Community
  • 1
  • 1
Schlaus
  • 18,144
  • 10
  • 36
  • 64
  • Thank you for your answer. Actually i have to edit this question to support same color for each clusters of lines. I have 1000 lines but will make them as clusters in future. These lines might increase in future to even big number. – user1 Mar 23 '17 at 18:57
  • @krishnadamarla You can reuse a color if you'd like. I updated my answer to store the value into a variable you could use as much as you like. There are +16 million colors in rgb, so you should be safe with even quite a high amount of unique colors. – Schlaus Mar 23 '17 at 18:59