Colors for ID's

K

Karsten Wutzke

Hello all!

I need some way to assign a color to an ID. There are two approaches
that I can implement:

1. Randomize the references to Color in the color array on application
startup and use an index into that shuffled array. This will always make
different colors for the ID=0 and different program starts.

2. Use some other more static way, so that id=0 will always be red, id=1
will always be green, id=2 will always be orange etc. on every program
startup. This could be a look up table. However, this will only be a
backup solution, if I don't find a good algorithm to do a nice randomize
of the colors so that similar colors are pretty much scattered linearly
in the array: All red-like colors, if there are 6, appear at indices 0,
6, 12, 18, 24, 30.

Does anyone have an idea of how one of these algorithms might look like?

BTW: I only need 36 colors. These are the hues in a 360 deg color circle.

Thanks for your help!

Karsten
 
C

Chris Smith

Karsten said:
if I don't find a good algorithm to do a nice randomize
of the colors so that similar colors are pretty much scattered linearly
in the array: All red-like colors, if there are 6, appear at indices 0,
6, 12, 18, 24, 30.

Does anyone have an idea of how one of these algorithms might look like?

BTW: I only need 36 colors. These are the hues in a 360 deg color circle.

Okay, so if you want pure colors, look into the HSV color space, and fix
V and S at 100%. Now you can vary hue, which varies from 0 to 360
degrees in a circle.

At this point, your problem description breaks down, and there are
multiple solutions depending on your definition of "similar".
Specifically, you can make any number (up to one less than the number of
colors) of revolutions around the circle. With only one revolution,
you'll get colors that are evenly spaced in progression around the
circle so that consecutive colors are similar. With 31 revolutions, I
think you'll you get the greatest possible distance between consecutive
colors while still spacing the colors evenly. In your example of having
6 "red-like" colors that you'd like to space evenly, you'd choose to
make 6 revolutions.

In any case, for n colors and r revolutions around the color circle, the
distance in degrees of hue between consecutive colors is given by:

360 * r / (n - r + 1)

That formula is designed to just miss a making a complete 360 degree
revolution by enough to ensure that your final color distribution is
evenly spaced.

From there, you probably need to convert the HSV color space to RGB.
You should be able to find sample code for that from Google without
difficulty. If you can't, I've got some code around somewhere that I
could look up; some students of mine used HSV some time ago to choose
random brown-like colors for asteroids in an Asteroids game clone during
a middle-school class I taught; I'm sure they wouldn't mind my sharing
the code.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andrew Thompson

I need some way to assign a color to an ID.

ID?? I don't see ID or Id class
defined anywhere in the JDK 1.4.
Does anyone have an idea of how one of these algorithms might look like?

Since this could just as easily be JS
(or COBOL, C++, .NET) as Java, I suggest
a group on algorithms.
 
C

Chris Smith

Chris said:
In any case, for n colors and r revolutions around the color circle, the
distance in degrees of hue between consecutive colors is given by:

360 * r / (n - r + 1)

That formula is designed to just miss a making a complete 360 degree
revolution by enough to ensure that your final color distribution is
evenly spaced.

Except it's wrong. The actual formula I meant to post is simpler: (360
* r / n). However, it comes with the additional constraint that r and n
must be relatively prime so as to avoid duplicating colors. Since your
n=32 is a power of 2, that simply means that r should be an odd number.
Everything else stays the same.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

Since this could just as easily be JS
(or COBOL, C++, .NET) as Java, I suggest
a group on algorithms.

He is hoping there is something built into that giant Java class
library that will do most of the work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top