在图形中生成明显不同的 RGB 颜色
Posted
技术标签:
【中文标题】在图形中生成明显不同的 RGB 颜色【英文标题】:Generate distinctly different RGB colors in graphs 【发布时间】:2010-09-23 11:24:28 【问题描述】:在生成图表并显示不同的数据集时,通常最好按颜色区分数据集。所以一条线是红色的,下一条是绿色的,依此类推。那么问题是,当数据集的数量未知时,需要随机生成这些颜色,并且它们通常最终彼此非常接近(例如绿色、浅绿色)。
关于如何解决这个问题以及如何生成明显不同的颜色有什么想法吗?
如果有任何示例(如果您觉得更容易,请随意讨论问题和解决方案,如果您觉得更容易,请随意讨论问题和解决方案)是基于 C# 和 RGB 的颜色。
【问题讨论】:
见***.com/questions/470690/… 【参考方案1】:您有 0 到 255 R、G 和 B 三个颜色通道。
先过
0, 0, 255
0, 255, 0
255, 0, 0
然后通过
0, 255, 255
255, 0, 255
255, 255, 0
然后除以 2 => 128 并重新开始:
0, 0, 128
0, 128, 0
128, 0, 0
0, 128, 128
128, 0, 128
128, 128, 0
除以 2 => 64
下次将 64 添加到 128 => 192
遵循模式。
简单的编程并为您提供相当独特的颜色。
编辑:请求代码示例
另外 - 如果灰色是可接受的颜色,则添加如下附加图案:
255, 255, 255
128, 128, 128
您可以通过多种方式在代码中生成这些内容。
简单的方法
如果您可以保证您永远不需要超过固定数量的颜色,只需按照此模式生成一个颜色数组并使用它们:
static string[] ColourValues = new string[]
"FF0000", "00FF00", "0000FF", "FFFF00", "FF00FF", "00FFFF", "000000",
"800000", "008000", "000080", "808000", "800080", "008080", "808080",
"C00000", "00C000", "0000C0", "C0C000", "C000C0", "00C0C0", "C0C0C0",
"400000", "004000", "000040", "404000", "400040", "004040", "404040",
"200000", "002000", "000020", "202000", "200020", "002020", "202020",
"600000", "006000", "000060", "606000", "600060", "006060", "606060",
"A00000", "00A000", "0000A0", "A0A000", "A000A0", "00A0A0", "A0A0A0",
"E00000", "00E000", "0000E0", "E0E000", "E000E0", "00E0E0", "E0E0E0",
;
艰难的道路
如果您不知道需要多少种颜色,下面的代码将使用此模式生成多达 896 种颜色。 (896 = 256 * 7 / 2) 256 是每个通道的颜色空间,我们有 7 种模式,在我们得到仅由 1 个颜色值分隔的颜色之前停止。
我可能比我需要的更努力地处理这段代码。首先,有一个从 255 开始的强度生成器,然后根据上述模式生成值。模式生成器只是循环通过七种颜色模式。
using System;
class Program
static void Main(string[] args)
ColourGenerator generator = new ColourGenerator();
for (int i = 0; i < 896; i++)
Console.WriteLine(string.Format("0: 1", i, generator.NextColour()));
public class ColourGenerator
private int index = 0;
private IntensityGenerator intensityGenerator = new IntensityGenerator();
public string NextColour()
string colour = string.Format(PatternGenerator.NextPattern(index),
intensityGenerator.NextIntensity(index));
index++;
return colour;
public class PatternGenerator
public static string NextPattern(int index)
switch (index % 7)
case 0: return "00000";
case 1: return "00000";
case 2: return "00000";
case 3: return "0000";
case 4: return "0000";
case 5: return "0000";
case 6: return "000";
default: throw new Exception("Math error");
public class IntensityGenerator
private IntensityValueWalker walker;
private int current;
public string NextIntensity(int index)
if (index == 0)
current = 255;
else if (index % 7 == 0)
if (walker == null)
walker = new IntensityValueWalker();
else
walker.MoveNext();
current = walker.Current.Value;
string currentText = current.ToString("X");
if (currentText.Length == 1) currentText = "0" + currentText;
return currentText;
public class IntensityValue
private IntensityValue mChildA;
private IntensityValue mChildB;
public IntensityValue(IntensityValue parent, int value, int level)
if (level > 7) throw new Exception("There are no more colours left");
Value = value;
Parent = parent;
Level = level;
public int Level get; set;
public int Value get; set;
public IntensityValue Parent get; set;
public IntensityValue ChildA
get
return mChildA ?? (mChildA = new IntensityValue(this, this.Value - (1<<(7-Level)), Level+1));
public IntensityValue ChildB
get
return mChildB ?? (mChildB = new IntensityValue(this, Value + (1<<(7-Level)), Level+1));
public class IntensityValueWalker
public IntensityValueWalker()
Current = new IntensityValue(null, 1<<7, 1);
public IntensityValue Current get; set;
public void MoveNext()
if (Current.Parent == null)
Current = Current.ChildA;
else if (Current.Parent.ChildA == Current)
Current = Current.Parent.ChildB;
else
int levelsUp = 1;
Current = Current.Parent;
while (Current.Parent != null && Current == Current.Parent.ChildB)
Current = Current.Parent;
levelsUp++;
if (Current.Parent != null)
Current = Current.Parent.ChildB;
else
levelsUp++;
for (int i = 0; i < levelsUp; i++)
Current = Current.ChildA;
【讨论】:
我没有完全按照这个例子。谁能为此提供一个 C# 示例? 希望代码示例有所帮助 - 可能有一种更清洁的方式来遍历强度值树,但这是第一次尝试,效果很好。干杯。 请注意,该算法会产生一些非常相似的颜色对(特别是在非常暗或亮、低饱和度的区域)。它在从高饱和度和亮度区域开始时做得很好,但会遗漏许多视觉上仍然明显不同的微妙颜色。 我最终在 javascript 中做了一些类似的事情——似乎在 rgb 上构建了一个精神拐杖/限制试剂。如果我们有 4 个 256 种颜色通道,我们会用 (* n) 更多颜色编写公式吗?即便如此,@Phrogz 和 @dean 的批评仍然有效(这就是我搜索 SO 以获得更好答案的原因)。必须有一种方法可以在每个强度步骤中捕捉明显不同的色调。 Phrogz 的回答,below 是在正确的轨道上,但如果我想通过一些int
计数器获得数百种颜色,像我这样的 peon 无法快速访问。
我添加了一个以编程方式解决问题的答案。这里的答案实际上是错误的。当您将 128 添加到混合物中时。你不只是用 0 来图案化它。你用 0 和 255 来图案化它。“Easy Way”颜色列表在这方面同样被打破。这些基本上是白色、黑色、R、G、B、C、Y、M 逐渐变暗。【参考方案2】:
要实现一个变体列表,你的颜色去哪里,255 然后使用它的所有可能性,然后添加 0 和具有这两个值的所有 RGB 模式。然后添加 128 和所有 RGB 组合。然后是 64. 然后是 192. 等等。
在 Java 中,
public Color getColor(int i)
return new Color(getRGB(i));
public int getRGB(int index)
int[] p = getPattern(index);
return getElement(p[0]) << 16 | getElement(p[1]) << 8 | getElement(p[2]);
public int getElement(int index)
int value = index - 1;
int v = 0;
for (int i = 0; i < 8; i++)
v = v | (value & 1);
v <<= 1;
value >>= 1;
v >>= 1;
return v & 0xFF;
public int[] getPattern(int index)
int n = (int)Math.cbrt(index);
index -= (n*n*n);
int[] p = new int[3];
Arrays.fill(p,n);
if (index == 0)
return p;
index--;
int v = index % 3;
index = index / 3;
if (index < n)
p[v] = index % n;
return p;
index -= n;
p[v ] = index / n;
p[++v % 3] = index % n;
return p;
这将在未来无限地 (2^24) 产生该类型的模式。但是,在大约一百个点之后,您可能不会看到蓝色位置为 0 或 32 的颜色之间有太大差异。
您最好将其标准化为不同的颜色空间。 LAB 颜色空间,例如 L、A、B 值标准化和转换。所以颜色的独特性是通过更类似于人眼的东西来推动的。
getElement() 反转 8 位数字的字节序,并从 -1 而不是 0 开始计数(用 255 屏蔽)。所以它变成 255,0,127,192,64,... 随着数字的增长,它移动的有效位越来越少,细分数字。
getPattern() 确定模式中最重要的元素应该是什么(它是立方根)。然后继续分解涉及该最重要元素的 3N²+3N+1 个不同模式。
此算法将产生(前 128 个值):
#FFFFFF
#000000
#FF0000
#00FF00
#0000FF
#FFFF00
#00FFFF
#FF00FF
#808080
#FF8080
#80FF80
#8080FF
#008080
#800080
#808000
#FFFF80
#80FFFF
#FF80FF
#FF0080
#80FF00
#0080FF
#00FF80
#8000FF
#FF8000
#000080
#800000
#008000
#404040
#FF4040
#40FF40
#4040FF
#004040
#400040
#404000
#804040
#408040
#404080
#FFFF40
#40FFFF
#FF40FF
#FF0040
#40FF00
#0040FF
#FF8040
#40FF80
#8040FF
#00FF40
#4000FF
#FF4000
#000040
#400000
#004000
#008040
#400080
#804000
#80FF40
#4080FF
#FF4080
#800040
#408000
#004080
#808040
#408080
#804080
#C0C0C0
#FFC0C0
#C0FFC0
#C0C0FF
#00C0C0
#C000C0
#C0C000
#80C0C0
#C080C0
#C0C080
#40C0C0
#C040C0
#C0C040
#FFFFC0
#C0FFFF
#FFC0FF
#FF00C0
#C0FF00
#00C0FF
#FF80C0
#C0FF80
#80C0FF
#FF40C0
#C0FF40
#40C0FF
#00FFC0
#C000FF
#FFC000
#0000C0
#C00000
#00C000
#0080C0
#C00080
#80C000
#0040C0
#C00040
#40C000
#80FFC0
#C080FF
#FFC080
#8000C0
#C08000
#00C080
#8080C0
#C08080
#80C080
#8040C0
#C08040
#40C080
#40FFC0
#C040FF
#FFC040
#4000C0
#C04000
#00C040
#4080C0
#C04080
#80C040
#4040C0
#C04040
#40C040
#202020
#FF2020
#20FF20
从左到右,从上到下阅读。 729 种颜色 (9³)。所以直到 n = 9 的所有模式。你会注意到它们开始发生冲突的速度。只有这么多 WRGBCYMK 变体。而这个解决方案虽然聪明,但基本上只做不同深浅的原色。
大部分冲突是由于果岭以及大多数果岭在大多数人眼中的相似程度。要求每个人在开始时都有最大的不同,而不是仅仅不同到不同的颜色。这个想法的基本缺陷导致了原色图案和相同的色调。
使用 CIELab2000 颜色空间和距离例程随机选择并尝试 10k 种不同的颜色,并找到与先前颜色的最大距离最小距离,(几乎是请求的定义)避免了比上述解决方案更长的冲突:
这可以称为 Easy Way 的静态列表。耗时一个半小时,生成了 729 个条目:
#9BC4E5
#310106
#04640D
#FEFB0A
#FB5514
#E115C0
#00587F
#0BC582
#FEB8C8
#9E8317
#01190F
#847D81
#58018B
#B70639
#703B01
#F7F1DF
#118B8A
#4AFEFA
#FCB164
#796EE6
#000D2C
#53495F
#F95475
#61FC03
#5D9608
#DE98FD
#98A088
#4F584E
#248AD0
#5C5300
#9F6551
#BCFEC6
#932C70
#2B1B04
#B5AFC4
#D4C67A
#AE7AA1
#C2A393
#0232FD
#6A3A35
#BA6801
#168E5C
#16C0D0
#C62100
#014347
#233809
#42083B
#82785D
#023087
#B7DAD2
#196956
#8C41BB
#ECEDFE
#2B2D32
#94C661
#F8907D
#895E6B
#788E95
#FB6AB8
#576094
#DB1474
#8489AE
#860E04
#FBC206
#6EAB9B
#F2CDFE
#645341
#760035
#647A41
#496E76
#E3F894
#F9D7CD
#876128
#A1A711
#01FB92
#FD0F31
#BE8485
#C660FB
#120104
#D48958
#05AEE8
#C3C1BE
#9F98F8
#1167D9
#D19012
#B7D802
#826392
#5E7A6A
#B29869
#1D0051
#8BE7FC
#76E0C1
#BACFA7
#11BA09
#462C36
#65407D
#491803
#F5D2A8
#03422C
#72A46E
#128EAC
#47545E
#B95C69
#A14D12
#C4C8FA
#372A55
#3F3610
#D3A2C6
#719FFA
#0D841A
#4C5B32
#9DB3B7
#B14F8F
#747103
#9F816D
#D26A5B
#8B934B
#F98500
#002935
#D7F3FE
#FCB899
#1C0720
#6B5F61
#F98A9D
#9B72C2
#A6919D
#2C3729
#D7C70B
#9F9992
#EFFBD0
#FDE2F1
#923A52
#5140A7
#BC14FD
#6D706C
#0007C4
#C6A62F
#000C14
#904431
#600013
#1C1B08
#693955
#5E7C99
#6C6E82
#D0AFB3
#493B36
#AC93CE
#C4BA9C
#09C4B8
#69A5B8
#374869
#F868ED
#E70850
#C04841
#C36333
#700366
#8A7A93
#52351D
#B503A2
#D17190
#A0F086
#7B41FC
#0EA64F
#017499
#08A882
#7300CD
#A9B074
#4E6301
#AB7E41
#547FF4
#134DAC
#FDEC87
#056164
#FE12A0
#C264BA
#939DAD
#0BCDFA
#277442
#1BDE4A
#826958
#977678
#BAFCE8
#7D8475
#8CCF95
#726638
#FEA8EB
#EAFEF0
#6B9279
#C2FE4B
#304041
#1EA6A7
#022403
#062A47
#054B17
#F4C673
#02FEC7
#9DBAA8
#775551
#835536
#565BCC
#80D7D2
#7AD607
#696F54
#87089A
#664B19
#242235
#7DB00D
#BFC7D6
#D5A97E
#433F31
#311A18
#FDB2AB
#D586C9
#7A5FB1
#32544A
#EFE3AF
#859D96
#2B8570
#8B282D
#E16A07
#4B0125
#021083
#114558
#F707F9
#C78571
#7FB9BC
#FC7F4B
#8D4A92
#6B3119
#884F74
#994E4F
#9DA9D3
#867B40
#CED5C4
#1CA2FE
#D9C5B4
#FEAA00
#507B01
#A7D0DB
#53858D
#588F4A
#FBEEEC
#FC93C1
#D7CCD4
#3E4A02
#C8B1E2
#7A8B62
#9A5AE2
#896C04
#B1121C
#402D7D
#858701
#D498A6
#B484EF
#5C474C
#067881
#C0F9FC
#726075
#8D3101
#6C93B2
#A26B3F
#AA6582
#4F4C4F
#5A563D
#E83005
#32492D
#FC7272
#B9C457
#552A5B
#B50464
#616E79
#DCE2E4
#CF8028
#0AE2F0
#4F1E24
#FD5E46
#4B694E
#C5DEFC
#5DC262
#022D26
#7776B8
#FD9F66
#B049B8
#988F73
#BE385A
#2B2126
#54805A
#141B55
#67C09B
#456989
#DDC1D9
#166175
#C1E29C
#A397B5
#2E2922
#ABDBBE
#B4A6A8
#A06B07
#A99949
#0A0618
#B14E2E
#60557D
#D4A556
#82A752
#4A005B
#3C404F
#6E6657
#7E8BD5
#1275B8
#D79E92
#230735
#661849
#7A8391
#FE0F7B
#B0B6A9
#629591
#D05591
#97B68A
#97939A
#035E38
#53E19E
#DFD7F9
#02436C
#525A72
#059A0E
#3E736C
#AC8E87
#D10C92
#B9906E
#66BDFD
#C0ABFD
#0734BC
#341224
#8AAAC1
#0E0B03
#414522
#6A2F3E
#2D9A8A
#4568FD
#FDE6D2
#FEE007
#9A003C
#AC8190
#DCDD58
#B7903D
#1F2927
#9B02E6
#827A71
#878B8A
#8F724F
#AC4B70
#37233B
#385559
#F347C7
#9DB4FE
#D57179
#DE505A
#37F7DD
#503500
#1C2401
#DD0323
#00A4BA
#955602
#FA5B94
#AA766C
#B8E067
#6A807E
#4D2E27
#73BED7
#D7BC8A
#614539
#526861
#716D96
#829A17
#210109
#436C2D
#784955
#987BAB
#8F0152
#0452FA
#B67757
#A1659F
#D4F8D8
#48416F
#DEBAAF
#A5A9AA
#8C6B83
#403740
#70872B
#D9744D
#151E2C
#5C5E5E
#B47C02
#F4CBD0
#E49D7D
#DD9954
#B0A18B
#2B5308
#EDFD64
#9D72FC
#2A3351
#68496C
#C94801
#EED05E
#826F6D
#E0D6BB
#5B6DB4
#662F98
#0C97CA
#C1CA89
#755A03
#DFA619
#CD70A8
#BBC9C7
#F6BCE3
#A16462
#01D0AA
#87C6B3
#E7B2FA
#D85379
#643AD5
#D18AAE
#13FD5E
#B3E3FD
#C977DB
#C1A7BB
#9286CB
#A19B6A
#8FFED7
#6B1F17
#DF503A
#10DDD7
#9A8457
#60672F
#7D327D
#DD8782
#59AC42
#82FDB8
#FC8AE7
#909F6F
#B691AE
#B811CD
#BCB24E
#CB4BD9
#2B2304
#AA9501
#5D5096
#403221
#F9FAB4
#3990FC
#70DE7F
#95857F
#84A385
#50996F
#797B53
#7B6142
#81D5FE
#9CC428
#0B0438
#3E2005
#4B7C91
#523854
#005EA9
#F0C7AD
#ACB799
#FAC08E
#502239
#BFAB6A
#2B3C48
#0EB5D8
#8A5647
#49AF74
#067AE9
#F19509
#554628
#4426A4
#7352C9
#3F4287
#8B655E
#B480BF
#9BA74C
#5F514C
#CC9BDC
#BA7942
#1C4138
#3C3C3A
#29B09C
#02923F
#701D2B
#36577C
#3F00EA
#3D959E
#440601
#8AEFF3
#6D442A
#BEB1A8
#A11C02
#8383FE
#A73839
#DBDE8A
#0283B3
#888597
#32592E
#F5FDFA
#01191B
#AC707A
#B6BD03
#027B59
#7B4F08
#957737
#83727D
#035543
#6F7E64
#C39999
#52847A
#925AAC
#77CEDA
#516369
#E0D7D0
#FCDD97
#555424
#96E6B6
#85BB74
#5E2074
#BD5E48
#9BEE53
#1A351E
#3148CD
#71575F
#69A6D0
#391A62
#E79EA0
#1C0F03
#1B1636
#D20C39
#765396
#7402FE
#447F3E
#CFD0A8
#3A2600
#685AFC
#A4B3C6
#534302
#9AA097
#FD5154
#9B0085
#403956
#80A1A7
#6E7A9A
#605E6A
#86F0E2
#5A2B01
#7E3D43
#ED823B
#32331B
#424837
#40755E
#524F48
#B75807
#B40080
#5B8CA1
#FDCFE5
#CCFEAC
#755847
#CAB296
#C0D6E3
#2D7100
#D5E4DE
#362823
#69C63C
#AC3801
#163132
#4750A6
#61B8B2
#FCC4B5
#DEBA2E
#FE0449
#737930
#8470AB
#687D87
#D7B760
#6AAB86
#8398B8
#B7B6BF
#92C4A1
#B6084F
#853B5E
#D0BCBA
#92826D
#C6DDC6
#BE5F5A
#280021
#435743
#874514
#63675A
#E97963
#8F9C9E
#985262
#909081
#023508
#DDADBF
#D78493
#363900
#5B0120
#603C47
#C3955D
#AC61CB
#FD7BA7
#716C74
#8D895B
#071001
#82B4F2
#B6BBD8
#71887A
#8B9FE3
#997158
#65A6AB
#2E3067
#321301
#FEECCB
#3B5E72
#C8FE85
#A1DCDF
#CB49A6
#B1C5E4
#3E5EB0
#88AEA7
#04504C
#975232
#6786B9
#068797
#9A98C4
#A1C3C2
#1C3967
#DBEA07
#789658
#E7E7C6
#A6C886
#957F89
#752E62
#171518
#A75648
#01D26F
#0F535D
#047E76
#C54754
#5D6E88
#AB9483
#803B99
#FA9C48
#4A8A22
#654A5C
#965F86
#9D0CBB
#A0E8A0
#D3DBFA
#FD908F
#AEAB85
#A13B89
#F1B350
#066898
#948A42
#C8BEDE
#19252C
#7046AA
#E1EEFC
#3E6557
#CD3F26
#2B1925
#DDAD94
#C0B109
#37DFFE
#039676
#907468
#9E86A5
#3A1B49
#BEE5B7
#C29501
#9E3645
#DC580A
#645631
#444B4B
#FD1A63
#DDE5AE
#887800
#36006F
#3A6260
#784637
#FEA0B7
#A3E0D2
#6D6316
#5F7172
#B99EC7
#777A7E
#E0FEFD
#E16DC5
#01344B
#F8F8FC
#9F9FB5
#182617
#FE3D21
#7D0017
#822F21
#EFD9DC
#6E68C4
#35473E
#007523
#767667
#A6825D
#83DC5F
#227285
#A95E34
#526172
#979730
#756F6D
#716259
#E8B2B5
#B6C9BB
#9078DA
#4F326E
#B2387B
#888C6F
#314B5F
#E5B678
#38A3C6
#586148
#5C515B
#CDCCE1
#C8977F
使用蛮力(通过 CIELab Delta2000 测试所有 16,777,216 种 RGB 颜色/从黑色开始)产生一个系列。在 26 左右开始发生冲突,但可以通过目视检查和手动下降(计算机无法完成)使其达到 30 或 40。因此,以编程方式进行绝对最大值只能生成几十种不同的颜色。离散列表是您最好的选择。与以编程方式相比,您将通过列表获得更多离散的颜色。简单的方法是最好的解决方案,开始混合和匹配其他方法来改变你的数据而不是颜色。
#000000
#00FF00
#0000FF
#FF0000
#01FFFE
#FFA6FE
#FFDB66
#006401
#010067
#95003A
#007DB5
#FF00F6
#FFEEE8
#774D00
#90FB92
#0076FF
#D5FF00
#FF937E
#6A826C
#FF029D
#FE8900
#7A4782
#7E2DD2
#85A900
#FF0056
#A42400
#00AE7E
#683D3B
#BDC6FF
#263400
#BDD393
#00B917
#9E008E
#001544
#C28C9F
#FF74A3
#01D0FF
#004754
#E56FFE
#788231
#0E4CA1
#91D0CB
#BE9970
#968AE8
#BB8800
#43002C
#DEFF74
#00FFC6
#FFE502
#620E00
#008F9C
#98FF52
#7544B1
#B500FF
#00FF78
#FF6E41
#005F39
#6B6882
#5FAD4E
#A75740
#A5FFD2
#FFB167
#009BFF
#E85EBE
更新: 我以 1024 蛮力持续了大约一个月。
public static final String[] indexcolors = new String[]
"#000000", "#FFFF00", "#1CE6FF", "#FF34FF", "#FF4A46", "#008941", "#006FA6", "#A30059",
"#FFDBE5", "#7A4900", "#0000A6", "#63FFAC", "#B79762", "#004D43", "#8FB0FF", "#997D87",
"#5A0007", "#809693", "#FEFFE6", "#1B4400", "#4FC601", "#3B5DFF", "#4A3B53", "#FF2F80",
"#61615A", "#BA0900", "#6B7900", "#00C2A0", "#FFAA92", "#FF90C9", "#B903AA", "#D16100",
"#DDEFFF", "#000035", "#7B4F4B", "#A1C299", "#300018", "#0AA6D8", "#013349", "#00846F",
"#372101", "#FFB500", "#C2FFED", "#A079BF", "#CC0744", "#C0B9B2", "#C2FF99", "#001E09",
"#00489C", "#6F0062", "#0CBD66", "#EEC3FF", "#456D75", "#B77B68", "#7A87A1", "#788D66",
"#885578", "#FAD09F", "#FF8A9A", "#D157A0", "#BEC459", "#456648", "#0086ED", "#886F4C",
"#34362D", "#B4A8BD", "#00A6AA", "#452C2C", "#636375", "#A3C8C9", "#FF913F", "#938A81",
"#575329", "#00FECF", "#B05B6F", "#8CD0FF", "#3B9700", "#04F757", "#C8A1A1", "#1E6E00",
"#7900D7", "#A77500", "#6367A9", "#A05837", "#6B002C", "#772600", "#D790FF", "#9B9700",
"#549E79", "#FFF69F", "#201625", "#72418F", "#BC23FF", "#99ADC0", "#3A2465", "#922329",
"#5B4534", "#FDE8DC", "#404E55", "#0089A3", "#CB7E98", "#A4E804", "#324E72", "#6A3A4C",
"#83AB58", "#001C1E", "#D1F7CE", "#004B28", "#C8D0F6", "#A3A489", "#806C66", "#222800",
"#BF5650", "#E83000", "#66796D", "#DA007C", "#FF1A59", "#8ADBB4", "#1E0200", "#5B4E51",
"#C895C5", "#320033", "#FF6832", "#66E1D3", "#CFCDAC", "#D0AC94", "#7ED379", "#012C58",
"#7A7BFF", "#D68E01", "#353339", "#78AFA1", "#FEB2C6", "#75797C", "#837393", "#943A4D",
"#B5F4FF", "#D2DCD5", "#9556BD", "#6A714A", "#001325", "#02525F", "#0AA3F7", "#E98176",
"#DBD5DD", "#5EBCD1", "#3D4F44", "#7E6405", "#02684E", "#962B75", "#8D8546", "#9695C5",
"#E773CE", "#D86A78", "#3E89BE", "#CA834E", "#518A87", "#5B113C", "#55813B", "#E704C4",
"#00005F", "#A97399", "#4B8160", "#59738A", "#FF5DA7", "#F7C9BF", "#643127", "#513A01",
"#6B94AA", "#51A058", "#A45B02", "#1D1702", "#E20027", "#E7AB63", "#4C6001", "#9C6966",
"#64547B", "#97979E", "#006A66", "#391406", "#F4D749", "#0045D2", "#006C31", "#DDB6D0",
"#7C6571", "#9FB2A4", "#00D891", "#15A08A", "#BC65E9", "#FFFFFE", "#C6DC99", "#203B3C",
"#671190", "#6B3A64", "#F5E1FF", "#FFA0F2", "#CCAA35", "#374527", "#8BB400", "#797868",
"#C6005A", "#3B000A", "#C86240", "#29607C", "#402334", "#7D5A44", "#CCB87C", "#B88183",
"#AA5199", "#B5D6C3", "#A38469", "#9F94F0", "#A74571", "#B894A6", "#71BB8C", "#00B433",
"#789EC9", "#6D80BA", "#953F00", "#5EFF03", "#E4FFFC", "#1BE177", "#BCB1E5", "#76912F",
"#003109", "#0060CD", "#D20096", "#895563", "#29201D", "#5B3213", "#A76F42", "#89412E",
"#1A3A2A", "#494B5A", "#A88C85", "#F4ABAA", "#A3F3AB", "#00C6C8", "#EA8B66", "#958A9F",
"#BDC9D2", "#9FA064", "#BE4700", "#658188", "#83A485", "#453C23", "#47675D", "#3A3F00",
"#061203", "#DFFB71", "#868E7E", "#98D058", "#6C8F7D", "#D7BFC2", "#3C3E6E", "#D83D66",
"#2F5D9B", "#6C5E46", "#D25B88", "#5B656C", "#00B57F", "#545C46", "#866097", "#365D25",
"#252F99", "#00CCFF", "#674E60", "#FC009C", "#92896B", "#1E2324", "#DEC9B2", "#9D4948",
"#85ABB4", "#342142", "#D09685", "#A4ACAC", "#00FFFF", "#AE9C86", "#742A33", "#0E72C5",
"#AFD8EC", "#C064B9", "#91028C", "#FEEDBF", "#FFB789", "#9CB8E4", "#AFFFD1", "#2A364C",
"#4F4A43", "#647095", "#34BBFF", "#807781", "#920003", "#B3A5A7", "#018615", "#F1FFC8",
"#976F5C", "#FF3BC1", "#FF5F6B", "#077D84", "#F56D93", "#5771DA", "#4E1E2A", "#830055",
"#02D346", "#BE452D", "#00905E", "#BE0028", "#6E96E3", "#007699", "#FEC96D", "#9C6A7D",
"#3FA1B8", "#893DE3", "#79B4D6", "#7FD4D9", "#6751BB", "#B28D2D", "#E27A05", "#DD9CB8",
"#AABC7A", "#980034", "#561A02", "#8F7F00", "#635000", "#CD7DAE", "#8A5E2D", "#FFB3E1",
"#6B6466", "#C6D300", "#0100E2", "#88EC69", "#8FCCBE", "#21001C", "#511F4D", "#E3F6E3",
"#FF8EB1", "#6B4F29", "#A37F46", "#6A5950", "#1F2A1A", "#04784D", "#101835", "#E6E0D0",
"#FF74FE", "#00A45F", "#8F5DF8", "#4B0059", "#412F23", "#D8939E", "#DB9D72", "#604143",
"#B5BACE", "#989EB7", "#D2C4DB", "#A587AF", "#77D796", "#7F8C94", "#FF9B03", "#555196",
"#31DDAE", "#74B671", "#802647", "#2A373F", "#014A68", "#696628", "#4C7B6D", "#002C27",
"#7A4522", "#3B5859", "#E5D381", "#FFF3FF", "#679FA0", "#261300", "#2C5742", "#9131AF",
"#AF5D88", "#C7706A", "#61AB1F", "#8CF2D4", "#C5D9B8", "#9FFFFB", "#BF45CC", "#493941",
"#863B60", "#B90076", "#003177", "#C582D2", "#C1B394", "#602B70", "#887868", "#BABFB0",
"#030012", "#D1ACFE", "#7FDEFE", "#4B5C71", "#A3A097", "#E66D53", "#637B5D", "#92BEA5",
"#00F8B3", "#BEDDFF", "#3DB5A7", "#DD3248", "#B6E4DE", "#427745", "#598C5A", "#B94C59",
"#8181D5", "#94888B", "#FED6BD", "#536D31", "#6EFF92", "#E4E8FF", "#20E200", "#FFD0F2",
"#4C83A1", "#BD7322", "#915C4E", "#8C4787", "#025117", "#A2AA45", "#2D1B21", "#A9DDB0",
"#FF4F78", "#528500", "#009A2E", "#17FCE4", "#71555A", "#525D82", "#00195A", "#967874",
"#555558", "#0B212C", "#1E202B", "#EFBFC4", "#6F9755", "#6F7586", "#501D1D", "#372D00",
"#741D16", "#5EB393", "#B5B400", "#DD4A38", "#363DFF", "#AD6552", "#6635AF", "#836BBA",
"#98AA7F", "#464836", "#322C3E", "#7CB9BA", "#5B6965", "#707D3D", "#7A001D", "#6E4636",
"#443A38", "#AE81FF", "#489079", "#897334", "#009087", "#DA713C", "#361618", "#FF6F01",
"#006679", "#370E77", "#4B3A83", "#C9E2E6", "#C44170", "#FF4526", "#73BE54", "#C4DF72",
"#ADFF60", "#00447D", "#DCCEC9", "#BD9479", "#656E5B", "#EC5200", "#FF6EC2", "#7A617E",
"#DDAEA2", "#77837F", "#A53327", "#608EFF", "#B599D7", "#A50149", "#4E0025", "#C9B1A9",
"#03919A", "#1B2A25", "#E500F1", "#982E0B", "#B67180", "#E05859", "#006039", "#578F9B",
"#305230", "#CE934C", "#B3C2BE", "#C0BAC0", "#B506D3", "#170C10", "#4C534F", "#224451",
"#3E4141", "#78726D", "#B6602B", "#200441", "#DDB588", "#497200", "#C5AAB6", "#033C61",
"#71B2F5", "#A9E088", "#4979B0", "#A2C3DF", "#784149", "#2D2B17", "#3E0E2F", "#57344C",
"#0091BE", "#E451D1", "#4B4B6A", "#5C011A", "#7C8060", "#FF9491", "#4C325D", "#005C8B",
"#E5FDA4", "#68D1B6", "#032641", "#140023", "#8683A9", "#CFFF00", "#A72C3E", "#34475A",
"#B1BB9A", "#B4A04F", "#8D918E", "#A168A6", "#813D3A", "#425218", "#DA8386", "#776133",
"#563930", "#8498AE", "#90C1D3", "#B5666B", "#9B585E", "#856465", "#AD7C90", "#E2BC00",
"#E3AAE0", "#B2C2FE", "#FD0039", "#009B75", "#FFF46D", "#E87EAC", "#DFE3E6", "#848590",
"#AA9297", "#83A193", "#577977", "#3E7158", "#C64289", "#EA0072", "#C4A8CB", "#55C899",
"#E78FCF", "#004547", "#F6E2E3", "#966716", "#378FDB", "#435E6A", "#DA0004", "#1B000F",
"#5B9C8F", "#6E2B52", "#011115", "#E3E8C4", "#AE3B85", "#EA1CA9", "#FF9E6B", "#457D8B",
"#92678B", "#00CDBB", "#9CCC04", "#002E38", "#96C57F", "#CFF6B4", "#492818", "#766E52",
"#20370E", "#E3D19F", "#2E3C30", "#B2EACE", "#F3BDA4", "#A24E3D", "#976FD9", "#8C9FA8",
"#7C2B73", "#4E5F37", "#5D5462", "#90956F", "#6AA776", "#DBCBF6", "#DA71FF", "#987C95",
"#52323C", "#BB3C42", "#584D39", "#4FC15F", "#A2B9C1", "#79DB21", "#1D5958", "#BD744E",
"#160B00", "#20221A", "#6B8295", "#00E0E4", "#102401", "#1B782A", "#DAA9B5", "#B0415D",
"#859253", "#97A094", "#06E3C4", "#47688C", "#7C6755", "#075C00", "#7560D5", "#7D9F00",
"#C36D96", "#4D913E", "#5F4276", "#FCE4C8", "#303052", "#4F381B", "#E5A532", "#706690",
"#AA9A92", "#237363", "#73013E", "#FF9079", "#A79A74", "#029BDB", "#FF0169", "#C7D2E7",
"#CA8869", "#80FFCD", "#BB1F69", "#90B0AB", "#7D74A9", "#FCC7DB", "#99375B", "#00AB4D",
"#ABAED1", "#BE9D91", "#E6E5A7", "#332C22", "#DD587B", "#F5FFF7", "#5D3033", "#6D3800",
"#FF0020", "#B57BB3", "#D7FFE6", "#C535A9", "#260009", "#6A8781", "#A8ABB4", "#D45262",
"#794B61", "#4621B2", "#8DA4DB", "#C7C890", "#6FE9AD", "#A243A7", "#B2B081", "#181B00",
"#286154", "#4CA43B", "#6A9573", "#A8441D", "#5C727B", "#738671", "#D0CFCB", "#897B77",
"#1F3F22", "#4145A7", "#DA9894", "#A1757A", "#63243C", "#ADAAFF", "#00CDE2", "#DDBC62",
"#698EB1", "#208462", "#00B7E0", "#614A44", "#9BBB57", "#7A5C54", "#857A50", "#766B7E",
"#014833", "#FF8347", "#7A8EBA", "#274740", "#946444", "#EBD8E6", "#646241", "#373917",
"#6AD450", "#81817B", "#D499E3", "#979440", "#011A12", "#526554", "#B5885C", "#A499A5",
"#03AD89", "#B3008B", "#E3C4B5", "#96531F", "#867175", "#74569E", "#617D9F", "#E70452",
"#067EAF", "#A697B6", "#B787A8", "#9CFF93", "#311D19", "#3A9459", "#6E746E", "#B0C5AE",
"#84EDF7", "#ED3488", "#754C78", "#384644", "#C7847B", "#00B6C5", "#7FA670", "#C1AF9E",
"#2A7FFF", "#72A58C", "#FFC07F", "#9DEBDD", "#D97C8E", "#7E7C93", "#62E674", "#B5639E",
"#FFA861", "#C2A580", "#8D9C83", "#B70546", "#372B2E", "#0098FF", "#985975", "#20204C",
"#FF6C60", "#445083", "#8502AA", "#72361F", "#9676A3", "#484449", "#CED6C2", "#3B164A",
"#CCA763", "#2C7F77", "#02227B", "#A37E6F", "#CDE6DC", "#CDFFFB", "#BE811A", "#F77183",
"#EDE6E2", "#CDC6B4", "#FFE09E", "#3A7271", "#FF7B59", "#4E4E01", "#4AC684", "#8BC891",
"#BC8A96", "#CF6353", "#DCDE5C", "#5EAADD", "#F6A0AD", "#E269AA", "#A3DAE4", "#436E83",
"#002E17", "#ECFBFF", "#A1C2B6", "#50003F", "#71695B", "#67C4BB", "#536EFF", "#5D5A48",
"#890039", "#969381", "#371521", "#5E4665", "#AA62C3", "#8D6F81", "#2C6135", "#410601",
"#564620", "#E69034", "#6DA6BD", "#E58E56", "#E3A68B", "#48B176", "#D27D67", "#B5B268",
"#7F8427", "#FF84E6", "#435740", "#EAE408", "#F4F5FF", "#325800", "#4B6BA5", "#ADCEFF",
"#9B8ACC", "#885138", "#5875C1", "#7E7311", "#FEA5CA", "#9F8B5B", "#A55B54", "#89006A",
"#AF756F", "#2A2000", "#576E4A", "#7F9EFF", "#7499A1", "#FFB550", "#00011E", "#D1511C",
"#688151", "#BC908A", "#78C8EB", "#8502FF", "#483D30", "#C42221", "#5EA7FF", "#785715",
"#0CEA91", "#FFFAED", "#B3AF9D", "#3E3D52", "#5A9BC2", "#9C2F90", "#8D5700", "#ADD79C",
"#00768B", "#337D00", "#C59700", "#3156DC", "#944575", "#ECFFDC", "#D24CB2", "#97703C",
"#4C257F", "#9E0366", "#88FFEC", "#B56481", "#396D2B", "#56735F", "#988376", "#9BB195",
"#A9795C", "#E4C5D3", "#9F4F67", "#1E2B39", "#664327", "#AFCE78", "#322EDF", "#86B487",
"#C23000", "#ABE86B", "#96656D", "#250E35", "#A60019", "#0080CF", "#CAEFFF", "#323F61",
"#A449DC", "#6A9D3B", "#FF5AE4", "#636A01", "#D16CDA", "#736060", "#FFBAAD", "#D369B4",
"#FFDED6", "#6C6D74", "#927D5E", "#845D70", "#5B62C1", "#2F4A36", "#E45F35", "#FF3B53",
"#AC84DD", "#762988", "#70EC98", "#408543", "#2C3533", "#2E182D", "#323925", "#19181B",
"#2F2E2C", "#023C32", "#9B9EE2", "#58AFAD", "#5C424D", "#7AC5A6", "#685D75", "#B9BCBD",
"#834357", "#1A7B42", "#2E57AA", "#E55199", "#316E47", "#CD00C5", "#6A004D", "#7FBBEC",
"#F35691", "#D7C54A", "#62ACB7", "#CBA1BC", "#A28A9A", "#6C3F3B", "#FFE47D", "#DCBAE3",
"#5F816D", "#3A404A", "#7DBF32", "#E6ECDC", "#852C19", "#285366", "#B8CB9C", "#0E0D00",
"#4B5D56", "#6B543F", "#E27172", "#0568EC", "#2EB500", "#D21656", "#EFAFFF", "#682021",
"#2D2011", "#DA4CFF", "#70968E", "#FF7B7D", "#4A1930", "#E8C282", "#E7DBBC", "#A68486",
"#1F263C", "#36574E", "#52CE79", "#ADAAA9", "#8A9F45", "#6542D2", "#00FB8C", "#5D697B",
"#CCD27F", "#94A5A1", "#790229", "#E383E6", "#7EA4C1", "#4E4452", "#4B2C00", "#620B70",
"#314C1E", "#874AA6", "#E30091", "#66460A", "#EB9A8B", "#EAC3A3", "#98EAB3", "#AB9180",
"#B8552F", "#1A2B2F", "#94DDC5", "#9D8C76", "#9C8333", "#94A9C9", "#392935", "#8C675E",
"#CCE93A", "#917100", "#01400B", "#449896", "#1CA370", "#E08DA7", "#8B4A4E", "#667776",
"#4692AD", "#67BDA8", "#69255C", "#D3BFFF", "#4A5132", "#7E9285", "#77733C", "#E7A0CC",
"#51A288", "#2C656A", "#4D5C5E", "#C9403A", "#DDD7F3", "#005844", "#B4A200", "#488F69",
"#858182", "#D4E9B9", "#3D7397", "#CAE8CE", "#D60034", "#AA6746", "#9E5585", "#BA6200"
;
【讨论】:
恕我直言,比公认的答案好得多。并且 +1 用于视觉示例和预先计算的列表! 我还进行了详尽的搜索,以在添加的颜色和集合中已有的颜色之间最大化 CIEDE2000,黑色和白色作为预定义颜色。和你一样,我很早就开始使用两种“肤色”:#ff9d25(趋向于橙色)和#ffb46c(趋向于粉红色)。我认为它们看起来非常相似,因此 CIEDE2000 可能不是很好的色差测量。不过,目前没有比这更好的了。我很想开始做我自己的明显差异实验,可能首先使用 16x16x16 sRGB 网格...... 我升到了 1024,但花了我一个多月的时间。您可以同样使用其他颜色集来运行它,我有各种各样的色域。而真正的 CIEDE2000 实际上是最好的。 dE2k 中的一项修正是肤色,它们对我们来说看起来更不同,并且对许多功能更重要。标准 dE 使它们与实际应有的不同。杏色和暗黄色看起来很不一样。 godsnotwheregodsnot.blogspot.com/2012/09/… 我能看到的唯一主要改进是静态列表。如果您只需要恰好 20 种颜色,那么找到与所有其他颜色最远的颜色实际上可能不是最佳的。如果您进行聚类并找到集合中所有颜色之间颜色距离最大的 20 种颜色,您可能可以获得更好的结果。这实际上可能会变成旅行推销员,并且通过非常昂贵的颜色距离算法强制 (2^24)^20 可能需要很长时间。不过,一个好的聚类算法可以很快给你一个好的结果。 实际上,经过检查,我什至可能没有为发布的图形中的最后两个做过。它每次都在制作它并制作一个新图像。但是,那时对于每种新颜色来说,基本上都是一段很长的时间。并且完全理解它们并没有太大帮助。【参考方案3】:我已经在网上建立了一个页面,用于在程序上生成视觉上不同的颜色:http://phrogz.net/css/distinct-colors.html
与此处均匀跨过 RGB 或 HSV 空间(有a nonlinear relationship between the axis values and the perceptual differences)的其他答案不同,我的页面使用标准的CMI(I:c) 颜色距离算法来防止两种颜色在视觉上过于接近。
页面的最后一个选项卡允许您以多种方式对值进行排序,然后将它们交错排列(有序随机播放),这样您就可以将非常不同的颜色彼此相邻放置。
在撰写本文时,它仅在 Chrome 和 Safari 中运行良好,并为 Firefox 提供了 shim;它在界面中使用 HTML5 范围输入滑块,IE9 和 Firefox 尚不原生支持。
【讨论】:
这是一个很棒的工具,感谢您创建它。我用它生成了 145 种不同的颜色,我对您的不同颜色工具创建的结果感到非常满意。 这个想法听起来不错,但我不明白界面是如何工作的。假设我想在 Lab 空间中生成 64 种颜色,我应该使用哪个设置?我不能得到超过 50 种颜色。 @wil Lab 页面上的默认设置为您提供了 480 种颜色可供选择。当您转到“优化”选项卡时,调整阈值以查看更多或更少的样本。 虽然有 36 种颜色,但我仍然可以得到几种非常相似的颜色。【参考方案4】:我认为HSV(或HSL)空间在这里有更多机会。如果您不介意额外的转换,只需旋转色调值即可轻松浏览所有颜色。如果这还不够,您可以更改饱和度/值/亮度值并再次进行旋转。或者,您可以随时更改色调值或更改“步进”角度并旋转更多次。
【讨论】:
请注意,即使stepping evenly across hue 也会产生次理想的感知分离。【参考方案5】:以前的 RGB 解决方案存在缺陷。它们没有利用整个色彩空间,因为它们使用颜色值和 0 作为通道:
#006600
#330000
#FF00FF
相反,他们应该使用所有可能的颜色值来生成在颜色通道中最多可以有 3 个不同值的混合颜色:
#336600
#FF0066
#33FF66
使用完整的色彩空间,您可以生成更多不同的颜色。例如,如果每个通道有 4 个值,则可以生成 4*4*4=64 种颜色。使用其他方案,只能生成 4*7+1=29 种颜色。
如果您想要 N 种颜色,则每个通道所需的值数为:ceil(cube_root(N))
然后,您可以确定可能的(0-255 范围)值(python):
max = 255
segs = int(num**(Decimal("1.0")/3))
step = int(max/segs)
p = [(i*step) for i in xrange(segs)]
values = [max]
values.extend(p)
然后你可以遍历 RGB 颜色(不推荐这样做):
total = 0
for red in values:
for green in values:
for blue in values:
if total <= N:
print color(red, green, blue)
total += 1
嵌套循环可以工作,但不推荐使用,因为它有利于蓝色通道,并且生成的颜色不会有足够的红色(N 很可能小于所有可能颜色值的数量)。
您可以为循环创建更好的算法,其中每个通道都被平等对待,并且更多不同的颜色值比小的颜色值更受青睐。
我有一个解决方案,但不想发布它,因为它不是最容易理解或高效的。但是,如果你真的想要,你可以查看solution。
这里是 64 种生成颜色的示例:64 colors
【讨论】:
【参考方案6】:我需要相同的功能,但形式很简单。
我需要的是从不断增加的索引值中生成尽可能独特的颜色。
这是代码,用 C# 编写(任何其他语言的实现都应该非常相似)
机制很简单
从 0 到 7 的 indexA 值生成 color_writers 模式。
对于
对于 8 到 15 之间的索引,这些颜色为 = color_writer[indexA] * 255 + (color_writer[indexA+1]) * 127
对于 16 到 23 之间的索引,这些颜色为 = color_writer[indexA] * 255 + (color_writer[indexA+1]) * 127 + (color_writer[indexA+2]) * 63
等等:
private System.Drawing.Color GetRandColor(int index)
byte red = 0;
byte green = 0;
byte blue = 0;
for (int t = 0; t <= index / 8; t++)
int index_a = (index+t) % 8;
int index_b = index_a / 2;
//Color writers, take on values of 0 and 1
int color_red = index_a % 2;
int color_blue = index_b % 2;
int color_green = ((index_b + 1) % 3) % 2;
int add = 255 / (t + 1);
red = (byte)(red+color_red * add);
green = (byte)(green + color_green * add);
blue = (byte)(blue + color_blue * add);
Color color = Color.FromArgb(red, green, blue);
return color;
注意:为避免生成明亮且难以看到的颜色(在本示例中:白色背景上的黄色),您可以使用递归循环对其进行修改:
int skip_index = 0;
private System.Drawing.Color GetRandColor(int index)
index += skip_index;
byte red = 0;
byte green = 0;
byte blue = 0;
for (int t = 0; t <= index / 8; t++)
int index_a = (index+t) % 8;
int index_b = index_a / 2;
//Color writers, take on values of 0 and 1
int color_red = index_a % 2;
int color_blue = index_b % 2;
int color_green = ((index_b + 1) % 3) % 2;
int add = 255 / (t + 1);
red = (byte)(red + color_red * add);
green = (byte)(green + color_green * add);
blue = (byte)(blue + color_blue * add);
if(red > 200 && green > 200)
skip_index++;
return GetRandColor(index);
Color color = Color.FromArgb(red, green, blue);
return color;
【讨论】:
【参考方案7】:我会从设定的 100% 亮度开始,然后首先使用原色:
FF0000, 00FF00, 0000FF
然后是组合
FFFF00, FF00FF, 00FFFF
接下来例如将亮度减半并进行相同的循环。没有太多真正明显不同的颜色,在这些之后我会开始改变线宽并做点/虚线等。
【讨论】:
+1 建议使用不同的线条样式而不是完全使用颜色。【参考方案8】:我以更短的方式实现了这个算法
void ColorValue::SetColorValue( double r, double g, double b, ColorType myType )
this->c[0] = r;
this->c[1] = g;
this->c[2] = b;
this->type = myType;
DistinctColorGenerator::DistinctColorGenerator()
mFactor = 255;
mColorsGenerated = 0;
mpColorCycle = new ColorValue[6];
mpColorCycle[0].SetColorValue( 1.0, 0.0, 0.0, TYPE_RGB);
mpColorCycle[1].SetColorValue( 0.0, 1.0, 0.0, TYPE_RGB);
mpColorCycle[2].SetColorValue( 0.0, 0.0, 1.0, TYPE_RGB);
mpColorCycle[3].SetColorValue( 1.0, 1.0, 0.0, TYPE_RGB);
mpColorCycle[4].SetColorValue( 1.0, 0.0, 1.0, TYPE_RGB);
mpColorCycle[5].SetColorValue( 0.0, 1.0, 1.0, TYPE_RGB);
//----------------------------------------------------------
ColorValue DistinctColorGenerator::GenerateNewColor()
int innerCycleNr = mColorsGenerated % 6;
int outerCycleNr = mColorsGenerated / 6;
int cycleSize = pow( 2, (int)(log((double)(outerCycleNr)) / log( 2.0 ) ) );
int insideCycleCounter = outerCycleNr % cyclesize;
if ( outerCycleNr == 0)
mFactor = 255;
else
mFactor = ( 256 / ( 2 * cycleSize ) ) + ( insideCycleCounter * ( 256 / cycleSize ) );
ColorValue newColor = mpColorCycle[innerCycleNr] * mFactor;
mColorsGenerated++;
return newColor;
【讨论】:
【参考方案9】:如果有人需要在 C# 中为白色前景生成随机的中到高深色,这里是代码。
[DllImport("shlwapi.dll")]
public static extern int ColorHLSToRGB(int H, int L, int S);
public static string GetRandomDarkColor()
int h = 0, s = 0, l = 0;
h = (RandomObject.Next(1, 2) % 2 == 0) ? RandomObject.Next(0, 180) : iApp.RandomObject.Next(181, 360);
s = RandomObject.Next(90, 160);
l = RandomObject.Next(80, 130);
return System.Drawing.ColorTranslator.FromWin32(ColorHLSToRGB(h, l, s)).ToHex();
private static string ToHex(this System.Drawing.Color c)
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
您可以将RandomObject
替换为您自己的Random
类对象。
【讨论】:
【参考方案10】:您也可以将色彩空间视为从 0 到 255(包括 0 到 255)三个数字的所有组合。这是 0 到 255^3 之间数字的 base-255 表示,强制保留三位小数(如果需要,在末尾添加零。)
因此,要生成 x 种颜色,您需要计算 x 均匀分布的百分比,从 0 到 100。通过将这些百分比乘以 255^3 来获取数字,将这些数字转换为以 255 为基数,然后如前所述添加零。
基础转换算法,供参考(在非常接近 C# 的伪代码中):
int num = (number to convert);
int baseConvert = (desired base, 255 in this case);
(array of ints) nums = new (array of ints);
int x = num;
double digits = Math.Log(num, baseConvert); //or ln(num) / ln(baseConvert)
int numDigits = (digits - Math.Ceiling(digits) == 0 ? (int)(digits + 1) : (int)Math.Ceiling(digits)); //go up one if it turns out even
for (int i = 0; i < numDigits; i++)
int toAdd = ((int)Math.Floor(x / Math.Pow((double)convertBase, (double)(numDigits - i - 1))));
//Formula for 0th digit: d = num / (convertBase^(numDigits - 1))
//Then subtract (d * convertBase^(numDigits - 1)) from the num and continue
nums.Add(toAdd);
x -= toAdd * (int)Math.Pow((double)convertBase, (double)(numDigits - i - 1));
return nums;
如果您愿意,您可能还需要做一些事情来稍微缩小范围,以避免出现白色和黑色。这些数字实际上并不是一个平滑的色标,但如果没有太多,它们会生成单独的颜色。
This question 在 .NET 中有更多关于基本转换的内容。
【讨论】:
【参考方案11】:用于获得第 n 种颜色。只需这种代码就足够了。我在我的opencv聚类问题中使用了这个。随着 col 的变化,这将创建不同的颜色。
for(int col=1;col<CLUSTER_COUNT+1;col++)
switch(col%6)
case 1:cout<<Scalar(0,0,(int)(255/(int)(col/6+1)))<<endl;break;
case 2:cout<<Scalar(0,(int)(255/(int)(col/6+1)),0)<<endl;break;
case 3:cout<<Scalar((int)(255/(int)(col/6+1)),0,0)<<endl;break;
case 4:cout<<Scalar(0,(int)(255/(int)(col/6+1)),(int)(255/(int)(col/6+1)))<<endl;break;
case 5:cout<<Scalar((int)(255/(int)(col/6+1)),0,(int)(255/(int)(col/6+1)))<<endl;break;
case 0:cout<<Scalar((int)(255/(int)(col/6)),(int)(255/(int)(col/6)),0)<<endl;break;
【讨论】:
【参考方案12】:您可以获得一组随机的 3255 个值,并与最后一组 3 个值进行检查,确保它们在使用前与旧值至少相差 X。
旧:190、120、100
新:180、200、30
如果 X = 20,那么新的集合会重新生成。
【讨论】:
我几乎好奇地做数学计算并计算在没有进一步解决方案时该算法进入无限循环之前平均需要多长时间。 嗯。奇怪的是,您的回答说 any r 值太接近另一个 R 值会导致再生,它最多小于 12。虽然奇怪的是红色和蓝色颜色太接近了,因为两者的绿色都在 0 以内,在 20 以内。我的意思是你的例子说:colorcodehex.com/be7864colorcodehex.com/b4c81e 靠得太近,应该重新生成。以上是关于在图形中生成明显不同的 RGB 颜色的主要内容,如果未能解决你的问题,请参考以下文章