简体   繁体   English

Python中的颜色处理

[英]Color handling in Python

For my clustering gui, I am currently using random colors for the clusters, since I won't know before hand how many clusters I will end up with. 对于我的聚类gui,我目前正在使用随机颜色进行聚类,因为我不知道手头会有多少个聚类。

In Python, this looks like: 在Python中,这看起来像:

import random
def randomColor():
    return (random.random(),random.random(),random.random())

However, when I update things, the colors change. 但是,当我更新东西时,颜色会发生变化。

So what I would favor is to have a function which has an input argument I such as 所以我倾向于拥有一个具有输入参数I的函数,例如

def nonrandomColor(i):
   ...
   return color

would always return the same color for the same I, while keeping the ability to generate arbitrarily many colors. 总是会为同一个I返回相同的颜色,同时保持生成任意多种颜色的能力。

Answer does not have to be formulated in Python, it's more the general layout I'm interested in. 答案不一定要用Python来表达,它更像是我感兴趣的总体布局。

One way is to use caching. 一种方法是使用缓存。 Use a defaultdict : 使用defaultdict

>>> import random
>>> def randomColor():
...    return (random.random(),random.random(),random.random())
... 
>>> from collections import defaultdict
>>> colors = defaultdict(randomColor)
>>> colors[3]
(0.10726172906719755, 0.97327604757295705, 0.58935794305308264)
>>> colors[1]
(0.48991106537516382, 0.77039712435566876, 0.73707003166893892)
>>> colors[3]
(0.10726172906719755, 0.97327604757295705, 0.58935794305308264)

Just set the seed of the random generator to the index, this might be cheaper than storing the colors. 只需将随机生成器的种子设置为索引,这可能比存储颜色便宜。

random.seed(i)

Note that this will make random numbers way less random than before. 请注意,这将使随机数的方法比以前少了随机的。 If that is a problem, eg if your application uses random numbers elsewhere, you might want to look into the caching options suggested by other answers. 如果这是一个问题,例如,如果您的应用程序在其他地方使用随机数,您可能需要查看其他答案建议的缓存选项。

You want to store the colors in a dictionary or a list: 您想要将颜色存储在字典或列表中:

colors = {} # int -> color
def nonrandomColor(i):
   if i not in colors:
      colors[i] = randomColor()
   return colors[i] 

If you want repeatable non colliding colors then you could use something like the function below. 如果你想要可重复的非碰撞颜色,那么你可以使用类似下面的功能。 It sections the number into 1, 10, 100 and then uses them as the RGB parts of the color. 它将数字分为1,10,100,然后将它们用作颜色的RGB部分。

def color(i):
  r = i % 10
  g = (i//10) % 10
  b = (i//100) % 10
  return(r*25, g*25, b*25)

For example: 例如:

color(1) == (25,0,0)
color(10) == (0,25,0)
color(999) = (225,225,255)

You can use i to seed the random number generator. 您可以使用i为随机数生成器播种。 So, as long as the seed remains the same, you get the same value. 因此,只要种子保持不变,就可以得到相同的值。

>>> import random
>>> random.seed(12)
>>> random.randint(0,255), random.randint(0,255), random.randint(0,255)
(121, 168, 170)
>>> random.seed(12)
>>> random.randint(0,255), random.randint(0,255), random.randint(0,255)
(121, 168, 170)
>>> random.seed(10)
>>> random.randint(0,255), random.randint(0,255), random.randint(0,255)
(146, 109, 147)
>>> random.seed(10)
>>> random.randint(0,255), random.randint(0,255), random.randint(0,255)
(146, 109, 147)

Depending on the number of colours you're likely to generate (ie, 10 or a million), the caching method might be better than the seed() method. 根据您可能生成的颜色数量(即10或100万),缓存方法可能比seed()方法更好。

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

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