I'm trying to blend all the colors into a circle using arcs. However, the arc comes as one solid color and not a blend of color as I thought. Is it possible to?
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(512,512);
Graphics g = panel.getGraphics();
int width = 100;
int height = 100;
g.drawOval(0,0,width, height);
//yellow
for( int i = 0; i < 100 ; i++){
Color c = new Color(255/100*i,255,0);
g.setColor(c);
g.fillArc(0,0,width,height,95,11);
}
Along time ago, I stumbled across a
ConicalGradientPaint
from Harmonic Code (there source is there somewhere, I just can't seem to find it again, but I've included my copy of it in the example).To try and do what you're doing with a pie slices, you need to slow alter the color from one point to another, using very small slices. So, while you might start with
Color.RED
and want to blend toColor.YELLOW
, you would actually need to generate each color between them (based on the distance)For example...
I know, it's note a wheel, but i was demonstrating the blending algorithm, I'll leave the pie slicing up to you ;)
Just proving to myself it could work...
You need to change the arc angle for every iteration and the arc size should be fixed at a certain value. I'm not sure what the value would be because I would expect you should iterate 360 times (in which case the size would be 1), not 100.
You can use the HSL Color class to do this simply.
An HSL color allows you to change the "hue" of the color in degrees. So you just need a simple loop to set/paint the color in a 1 degree arc:
I used the
HSL Color
because it has a simple API to change the hue.If you don't want to use that class then you can use
Color.getHSBColor(...)
method to get the color for each degree of change. Again the saturation and brightness would be fixed values and then you just change the hue.