472,353 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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?
How about:

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.

Similar topics

11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit...
0
by: Waran | last post by:
I need to create a Auto suggests Textboox like in http://www.google.com/webhp?complete=1&hl=en I have completed this using AJAX.NET for Framework...
10
by: BNicholeN | last post by:
Hi I need to know how to get a form to auto populate, i.e. When I pick a city from Center Location, I would like the Operations Manger and SVP to...
4
by: Mr. Wilson | last post by:
On this page (just a demo) I can’t get IE6 to auto-select the first item in the dynamically-generated drop-down menus. Well, that isn’t entirely...
4
by: scolivas | last post by:
I think this is a me thing. but can't remember how to do it. I have a form that I am using and would like for a txt box to automatically populate...
4
by: magmike | last post by:
I've got a control on my form that allows the user to select a record based on a form field (in this example, the drop down menu shows the company...
4
by: whamo | last post by:
I have the need to populate a field based on the selection in a combo box. Starting out simple. (2) tables tbl_OSE_Info and tbl_Input; ...
1
by: BSTAFFORD | last post by:
I need help. I have a form with a 5 comboboxes. the form also contains a subform query with the saved records from those 5 combobox categories....
2
by: Ronald | last post by:
I hope somebody can help. I can't get into the specifics of my project, but I'll try to create a simple example: tblVehicle * VIN (text box)...
6
by: dkyadav80 | last post by:
Hi sir, I'm new about xml, javascript. I have two selection field(html) first is city and second is state. the city and state values should be...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.