简体   繁体   中英

Color-coding numbers dynamically

I have a sequence of elements each with their own ID. The IDs are running from 1...n. I want all elements which share an ID to be displayed in the same color.

I know I can do something like define an array of colors for each number, but this will be static and I really don't know how many numbers I will have.

So far I have done this:

private object getIntColorString(int IDNum)
{
    return IDNum.ToString("X6");
}

But I didn't think it through, as the difference between colors is too small. So then I did this:

private object getIntColorString(int eventTrainingProgramID)
{
    int colorCode = eventTrainingProgramID * 100; 
    return colorCode.ToString("X6");
}

However, this produces to many recurrences of the same color.

Any suggestions?

Ok, colors can go from 000000 (black) to ffffff (white), so 16777215 different values. Let's assume you need to map an int in the range [0,2147483647] (0 to Int.MAX) to the range of colors [0,16777215].

First problem: if you have 16777217 or more elements at once, some color is going to be repeated. If you have 16777216 elements or less, you can map a different color to each ID.

If you know in advance the number of elements ( #ID = 100, for example), you can start like this:

  • col0 (color if ID0 ) could be (16777215/ #ID )*0
  • col1 (color of ID1 ) could be (16777215/ #ID )*1
  • col2 (color of ID2 ) could be (16777215/ #ID )*2

and so on, to guarantee the biggest possible difference between adjacent colors.

If you don't know in advance the number of elements, you could define colorJump as the smallest increment that makes two colors different to your eyes. In this way

  • col0 (color if ID0 ) could be 0
  • col1 (color of ID1 ) could be col0 + colorJump
  • col2 (color of ID2 ) could be col1 + colorJump

and so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM