Auto color selection PIL

Gabriel Genellina gagsl-py at yahoo.com.ar
Thu Sep 28 01:38:24 EDT 2006


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




More information about the Python-list mailing list