Suppose I have a class RainbowColorsMapper
with the constructor RainbowColorsMapper(int n)
, where n >= 2. Now I want to have continuous mapping of rainbow colors from red to violet which I get using the method mapper.getColor(int number)
where low values correspond to red end, and high near n
to violet end. If n = 2, mapper.getColor(0)
returns most left color of the spectrum (near red), and mapper.getColor(1)
returns the most right color. Same with bigger n
with automatic scaling.
My question: can this be done relatively easy, and if yes, what are suggestions on the algorithm?
The easiest way to do this will be to work in the HSL colourspace rather than RGB. Create colours where the saturation and lightness are fixed (to 100% and 50%, I would suggest), and the hue varies between suitable endpoints (which you might need to experiment to find). Convert the HSL values to RGB using Color.getHSBColor.