简体   繁体   English

动态对数字进行颜色编码

[英]Color-coding numbers dynamically

I have a sequence of elements each with their own ID. 我有一系列元素,每个元素都有自己的ID。 The IDs are running from 1...n. ID从1 ... n开始运行。 I want all elements which share an ID to be displayed in the same color. 我希望所有共享ID的元素都以相同的颜色显示。

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. 好的,颜色可以从000000 (黑色)变为ffffff (白色),因此16777215的值不同。 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]. 假设您需要将[0,2147483647]范围(0到Int.MAX)内的一个int映射到颜色[0,16777215]的范围内。

First problem: if you have 16777217 or more elements at once, some color is going to be repeated. 第一个问题:如果一次有16777217个或更多元素,则将要重复某些颜色。 If you have 16777216 elements or less, you can map a different color to each ID. 如果您具有16777216个或更少的元素,则可以将不同的颜色映射到每个ID。

If you know in advance the number of elements ( #ID = 100, for example), you can start like this: 如果预先知道元素的数量(例如#ID = 100),则可以这样开始:

  • col0 (color if ID0 ) could be (16777215/ #ID )*0 col0 (如果ID0彩色)可以是(16777215 / #ID )* 0
  • col1 (color of ID1 ) could be (16777215/ #ID )*1 col1ID1颜色)可能是(16777215 / #ID )* 1
  • col2 (color of ID2 ) could be (16777215/ #ID )*2 col2ID2颜色)可能是(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. 如果您事先不知道元素的数量,则可以将colorJump定义为使两种颜色与您的眼睛不同的最小增量。 In this way 通过这种方式

  • col0 (color if ID0 ) could be 0 col0 (如果ID0 col0彩色)可以为0
  • col1 (color of ID1 ) could be col0 + colorJump col1ID1颜色)可以是col0 + colorJump
  • col2 (color of ID2 ) could be col1 + colorJump col2ID2颜色)可以是col1 + colorJump

and so on 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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