Colors for ID's

Discussion in 'Java' started by Karsten Wutzke, Apr 6, 2004.

  1. 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
     
    Karsten Wutzke, Apr 6, 2004
    #1
    1. Advertising

  2. A HashMap maybe with an iterator...

    HTH,
    - Manish

    "Karsten Wutzke" <> wrote in message
    news:c4uqmo$lqs$07$-online.com...
    > 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
    >
     
    Manish Hatwalne, Apr 6, 2004
    #2
    1. Advertising

  3. Karsten Wutzke

    Chris Smith Guest

    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
     
    Chris Smith, Apr 6, 2004
    #3
  4. 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
     
    Andrew Thompson, Apr 6, 2004
    #4
  5. Karsten Wutzke

    Chris Smith Guest

    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
     
    Chris Smith, Apr 6, 2004
    #5
  6. Karsten Wutzke

    Roedy Green Guest

    On Tue, 06 Apr 2004 19:51:20 +0200, Karsten Wutzke
    <> 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.
     
    Roedy Green, Apr 6, 2004
    #6
  7. Karsten Wutzke

    Roedy Green Guest

    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.
     
    Roedy Green, Apr 6, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Smith

    Colors

    Paul Smith, Aug 20, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    1,284
    Scott M.
    Aug 29, 2005
  2. Jerry Camel

    Conditional Colors in a DataList Control

    Jerry Camel, Feb 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    314
    =?Utf-8?B?U3VyZXNo?=
    Feb 12, 2004
  3. Mark

    display all colors

    Mark, Apr 15, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    516
    Ken Cox [Microsoft MVP]
    Apr 15, 2004
  4. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    545
    darrel
    Aug 7, 2008
  5. ZelluX
    Replies:
    3
    Views:
    1,906
    Peter Otten
    Dec 1, 2008
Loading...

Share This Page