Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Colors for ID's

Reply
Thread Tools

Colors for ID's

 
 
Karsten Wutzke
Guest
Posts: n/a
 
      04-06-2004
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

 
Reply With Quote
 
 
 
 
Manish Hatwalne
Guest
Posts: n/a
 
      04-06-2004
A HashMap maybe with an iterator...

HTH,
- Manish

"Karsten Wutzke" <kwutzke-> wrote in message
news:c4uqmo$lqs$07$...
> 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
>



 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      04-06-2004
Karsten Wutzke wrote:
> 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
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      04-06-2004
On Tue, 06 Apr 2004 19:51:20 +0200, Karsten Wutzke wrote:

> 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.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      04-06-2004
Chris Smith wrote:
> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-06-2004
On Tue, 06 Apr 2004 19:51:20 +0200, Karsten Wutzke
<kwutzke-> wrote or quoted :

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


Try Random.shuffle after you build your equispaced colours.

To pick some pleasing colors to start see
http://mindprod.com/jgloss/colour.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-06-2004
On Tue, 06 Apr 2004 18:54:45 GMT, Andrew Thompson
<> wrote or quoted :

>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any library that can convert RGB colors to ANSI colors? ZelluX Python 3 12-01-2008 11:08 AM
TreeNode colors come from anchor colors AAaron123 ASP .Net 1 08-07-2008 07:56 PM
background colors dimestore Firefox 10 07-16-2005 09:35 AM
Problem with backgrounds &colors balado Firefox 1 02-28-2005 09:04 PM
Background colors Steve IA Firefox 0 11-06-2004 07:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57