Array of Colours

Started by
2 comments, last by TheAdmiral 17 years, 2 months ago
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
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.
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
Perhaps you'd have more luck displaying (and generating) the colours according to their layout in the HSL colour-space.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

This topic is closed to new replies.

Advertisement