Jump to content
  • Advertisement
Sign in to follow this  
Silent Dragon

Array of Colours

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm not very good at colours/graphics and the like, but I'm trying to get an array of oolours that could be nicely displayed in a colour palette. I can't figure out how to get a decent range of varied colours. The code I'm using:
private Color[] colours;
    
    private static final int NUM_COLOURS = 1024;
    
    /** Creates a new instance of ColourPalette */
    public ColourPalette() {
// Set up the colours array
       colours = new Color[NUM_COLOURS];
       int i = 0;
       
       for(int r = 0; r < 255; r+=25)
       {
           for(int g = 0; g < 255; g+=25)
           {
               for(int b = 0; b < 255; b+=25)
               {
                    if(i < NUM_COLOURS)
                        colours = new Color(r, g, b);
                    
                    i++;
               }
           }
       }
}


Thats just an extract, but this gives an array, but not the one i was hoping for. Any feedback appreciated, SD

Share this post


Link to post
Share on other sites
Advertisement
Quote:
but not the one i was hoping for.

What were you hoping for? Just taking all colors of the form (64a, 64b, 64c) where a, b and c are integers usually give decent results and AFAIK there are some drawing programs only having those as default (actually a subset of them only). Using that will give you 53 = 125 different colors. If you just want a uniform distribution of n colors then that can easily be done too.

Your current method has some flaws. It only works decently (and still not all that well) with exactly a 1000 colors. If you have more then it will still only fill 1000 spots and if you have less you won't get a proper distribution. Something like:
private Color[] colours;

private static final int NUM_COLOURS_PER_CHANNEL = 8;

// You might want to enforce that NUM_COLOURS_PER_CHANNEL is divisble by 256.
// Though you could round it.

// Does this language (C# I assume) have a power operator?
//That would look better than my manual power of 3.
private static final int NUM_COLOURS = NUM_COLOURS_PER_CHANNEL*NUM_COLOURS_PER_CHANNEL*NUM_COLOURS_PER_CHANNEL;

/** Creates a new instance of ColourPalette */
public ColourPalette() {
// Set up the colours array
colours = new Color[NUM_COLOURS];
int i = 0;

int stepsize = 256/NUM_COLOURS_PER_CHANNEL;
for(int r = 0; r < 256; r+=stepsize)
{
for(int g = 0; g < 256; g+=stepsize)
{
for(int b = 0; b < 256; b+=stepsize)
{
colours[i++] = new Color(r, g, b);
}
}
}
}

Would probably give better distribution.

Share this post


Link to post
Share on other sites
Thanks for the reply, thats cleaned it up somewhat. I guess my main problem wasn't the range of colours, just the order I displayed them in. The colour palette looks like this:

Image Hosted by ImageShack.us

What I was trying to get was a smooth transition, with all blues through green through red. I don't know if thats possible or worthwhile, but I appreciate the help!

Thanks,
SD

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!