Colour choosing algorithm (C#)

Associate
Joined
14 Apr 2003
Posts
1,101
Hi,

Im working on a project that requires me to display database records on a map. You can group the records by different fields and I have a different colour for each type. Currently I just choose the colours randomly from the Colors class. However, this chooses identical colours sometimes and it also chooses poor colours (such as white, cream etc) what I want is some sort of maths algorigthm that will generate a range of colours from red to blue (say) then i can take colours at set intervals to give a nice spread.

I know it must be possible but maths isnt my strong point!

any ideas?

Thanks,

Matt
 
yes, I have now got a system set up that chooses colours at random, but it still has the same problems as before, choosing white-cream or generally pale colours as well as selecting colours that are almost identical which doesnt help differentiate between the fields.
 
Hi,

Thanks for the idea, ideally no more than say 10 colours would be needed, but seeing as life is unfair the maximum number of colours needed could be as much as 350+ (highly unlikely someone would produce a query that returned so many results though!). I think ill try that algo you suggested, see what happens!

Matt
 
matja said:
Inquisitor was spot-on.
A very simple hue-to-RGB conversion involves using trig functions :

red = cos(angle) * 0.5 + 0.5
green = cos(angle+((2/3)*pi)) * 0.5 + 0.5;
blue = cos(angle+((4/3)*pi)) * 0.5 + 0.5;

will give you a spectrum for values of 'angle' from 0 to 2*pi.

so if you wanted four distinct and hue-opposed colours, you'd divide 2*pi by 5 and use the first 4 values

Hi, sorry to bring this up again, but i was busy implementing this algorithm and its returning colours taht are identical? Ive got some code from my program:

Code:
double angle = (2 * Math.PI) / (_Colors.Length + 1);   
            double step = angle;
            for (int i = 0; i < _Colors.Length; i++)
            {
                double red = (Math.Cos(angle) * 0.5 + 0.5);
                double green = Math.Cos(angle + ((2 / 3) * Math.PI)) * 0.5 + 0.5;
                double blue = Math.Cos(angle + ((4 / 3) * Math.PI)) * 0.5 + 0.5;
                Color c = Color.FromArgb((int)(red*255), (int)(green*255), (int)(blue*255));
                _Colors[i] = c;
                angle = angle + step;
            }

now if i try to generate 5 colours it comes up with the following numbers for the angle, red, green and blue:

Code:
Angle: 1.0471975511966
Red: 0.75
Green: 0.75
Blue: 0.25
Angle: 2.0943951023932
Red: 0.25
Green: 0.25
Blue: 0.75
Angle: 3.14159265358979
Red: 0
Green: 0
Blue: 1
Angle: 4.18879020478639
Red: 0.25
Green: 0.25
Blue: 0.75
Angle: 5.23598775598299
Red: 0.75
Green: 0.75
Blue: 0.25

I dont know if im using the formula correctly, from what i understood I took the number of colours i required and diveded 2*pi by that number, then i stuck it in the formula and then added the initial angle by itself and repeated the process? To calculate the colour i just multiplied those numbers by 255. if ive got this completely wrong please tell me!!

thanks,

matt

edit: note the first two colours are fine i get a blue and a mustard colour, but after that they get really similar and then eventually the same.
 
Back
Top Bottom