I have written the following code for the color wheel using the separate function. The problem I am getting is that half of the color wheel is the green color. I believe that there is some error in the function angleToColor(). Here is my code please tell me the error in my code.
float angleBetweenPoints(float fromX, float fromY, float toX, float toY)//This function gives the angle between two points.
{
return atan2(toY-fromY,toX-fromX);
}
float distanceBetweenPoints(float fromX, float fromY, float toX, float toY)//This gives the distance between two points.
{
return sqrt(sq(toX-fromX)+sq(toY-fromY));
}
float linearScale(float x, float low, float hi, float newLow, float newHi)//Function that converts one range value to another.
{
return newLow+((x-low)/(hi-low))*(newHi-newLow);//The formula to convert the one range value to another.
}
int angleToColor(float theta)//Function that converts the angle to colour.
{
float r=0;//Red colour
float g=0;//Green colour
float b=0;//Blue colour
if(theta>0 && theta<2*PI/3)//If the angle is in the first third of the circle then the colour changes between the red and blue
{
g=0;//Green is then set to zero
b=linearScale(theta,0,2*PI/3,0,255);//The blue colour changes from 0 to 255 as the angle goes from 0 to 2*PI/3
r=255-b;//The red colour decreases as blue colour increases.
}
else if(theta>2*PI/3 && theta<4*PI/3)//If the angle is in the second third of the circle thenthe colour changes between
{
r=0;//Red is then set to zero
g=linearScale(theta,2*PI/3,4*PI/3,0,255);//The green colour changes from 0 to 255 as the angle goes from 2*PI/3 to 4*PI/3
b=255-g;//The blue colour decreases as green colour increases.
}
else // if the conditions are false or if the angle lies in the last third of the circle then the red colour changes from 0 to 255 as the angle changes from 4*PI/3 to 2*PI.
{
b=0;//Blue is then set to zero
r=linearScale(theta,4*PI/3,2*PI,0,255);//The red colour changes from 0 to 255 as the angle goes from 4*PI/3 to 2*PI
g=255-r;//The green colour decreases as red colour increases.
}
return color(r,g,b);//Returns the color() function
}
float distanceToBrightness(float distance, float maxDistance)//This function converts the distance to brightness in the range of 0 to 1 inclusive.
{
float brightnessValue=0;//The brightness value is initialised to 0
if(distance<=maxDistance)//If the distance is smaller or equal to max distance then distance is converted to brightness in the range of 0 to 1 inclusive.
{
brightnessValue=linearScale(distance,0,maxDistance,0,1);//Converts the distance to brightness using linearScale() function.
}
else if(distance>maxDistance)//If the distance is greater than the maxDistace then the brightness will always be 0.
{
brightnessValue=0;//Brightness gets zero.
}
return brightnessValue;//Returns the brightness value in the range of 0 to 1 inclusive.
}
int changeColorBrightness(float bright, int c)
{
int colorBrightness=color(red(c)*bright,green(c)*bright,blue(c)*bright);
return colorBrightness;
}
void setup()//For testing
{
size(500,500);
noLoop();
int test=angleToColor(PI/3);
fill(test);
rect(30,30,30,30);
}
void draw()
{
drawWheel();
}
void drawWheel()
{
for(int i=0; i<=width; i++)
{
for(int j=0;j<=height;j++)
{
float angle=angleBetweenPoints(width/2, height/2, i,j);
float distance=distanceBetweenPoints(width/2,height/2,i,j);
int colour=angleToColor(angle);
float brightness=distanceToBrightness(distance,250);
int colorBrightness=changeColorBrightness(brightness,colour);
stroke(colorBrightness);
point(i,j);
}
}
}