472,353 Members | 1,419 Online

# Auto color selection PIL

Hi,

I'm trying to plot some points in an image. Each point has an
associating type and I'd like to have different colors (preferrably of
high contrast) for different types. The number of types in the data
file is unknown a priori. Is there a way to do this at runtime?

The "solution" I have so far has been to manually create a list of
10-20 contrasting colors and just go off that list at runtime. This
works most of the time since the number of types is usually less than
10. But I'd like a general solution.

Thank you.

Sep 27 '06 #1
5 1723
Xiaolei wrote:
I'm trying to plot some points in an image. Each point has an
associating type and I'd like to have different colors (preferrably of
high contrast) for different types. The number of types in the data
file is unknown a priori. Is there a way to do this at runtime?

from colorsys import hsv_to_rgb

def colors(n):
incr = 1.0 / n
hue = 0.0
for i in xrange(n):
r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
yield int(r*255), int(g*255), int(b*255)
hue += incr
Sep 27 '06 #2
Xiaolei wrote:
I'm trying to plot some points in an image. Each point has an
associating type and I'd like to have different colors (preferrably of
high contrast) for different types. The number of types in the data
file is unknown a priori. Is there a way to do this at runtime?
If you don't know how many colors are needed even at run time, this code
might be helpful. But it generates colors that look similar pretty
quickly, so I wouldn't use it unless you have to. (Anyone know of a
better algorithm for this?)

from itertools import count
from colorsys import hsv_to_rgb

def hues():
yield 0.0
for i in count():
for j in xrange(2**i):
yield (1.0 / 2**(i+1)) + ((1.0 / 2**i) * j)

def colors():
for hue in hues():
r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
yield int(r*255), int(g*255), int(b*255)
Sep 27 '06 #3
At Wednesday 27/9/2006 20:43, Leif K-Brooks wrote:
I'm trying to plot some points in an image. Each point has an
associating type and I'd like to have different colors (preferrably of
high contrast) for different types. The number of types in the data
file is unknown a priori. Is there a way to do this at runtime?

If you don't know how many colors are needed even at run time, this code
might be helpful. But it generates colors that look similar pretty
quickly, so I wouldn't use it unless you have to. (Anyone know of a
better algorithm for this?)
Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
It's the best I could make if you don't know the number of colors
beforehand. If you *do* know how many colors, your previous response is OK.
from colorsys import hsv_to_rgb

def hues():
denom = 1
yield 0
while True:
num = 1
while num<denom:
yield float(num)/denom
num += 2
denom += denom

def colors():
for hue in hues():
r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
yield int(r*255), int(g*255), int(b*255)

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Sep 28 '06 #4
Gabriel Genellina wrote:
Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
It's the best I could make if you don't know the number of colors
beforehand. If you *do* know how many colors, your previous response is OK.
Um, that's the same thing my second suggestion does:
>>h = hues()
h.next()
0.0
>>h.next()
0.5
>>h.next()
0.25
>>h.next()
0.75
>>h.next()
0.125

Your implementation is less terse than mine, though. And I suspect it
runs faster, though I haven't checked that.
Sep 28 '06 #5
Leif K-Brooks wrote:
Gabriel Genellina wrote:
>Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
It's the best I could make if you don't know the number of colors
beforehand. If you *do* know how many colors, your previous response
is OK.
I've no better suggestion than either of you, _but_ note that in
choosing colors for keys it is generally considered better ergonomics
to vary more than simply the hue if you don't want to penalize the
colorblind. Consider varying the other two parameters simultaneously
(perhaps in restricted ranges and varying orders); the contrast may be
more substantial even to a viewer with full color vision.

--
--Scott David Daniels
sc***********@acm.org
Oct 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.