Color wheel: Difference between revisions

Content added Content deleted
m (used better phrasing in the "recommendation" sentence.)
(→‎{{header|zkl}}: re-invent the wheel)
Line 76: Line 76:


=={{header|zkl}}==
=={{header|zkl}}==
Each point in the square is converted to polar coordinates, the angle is hue and the radius is saturation (which is scaled by the distance from the pole). If the radius/saturation is in the circle, render it.
A color square - I just fill a square with the color wheel, it could be clipped to a circle if needed.
Each point in the square is converted to polar coordinates, the angle is hue and the radius is saturation (which is scaled by the distance from the pole).


Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
Line 85: Line 84:
fcn colorWheel(ppm){
fcn colorWheel(ppm){
centerX,centerY:=ppm.w/2,ppm.h/2;
zero,R:=ppm.w/2, zero;
d:=centerX.toFloat().hypot(centerY);
foreach x,y in (w,h){
foreach x,y in (w,h){
v,hue:=(x - centerX).toFloat().toPolar(y - centerY);
v,hue:=(x - zero).toFloat().toPolar(y - zero);
if((hue = hue.toDeg())<0) hue+=360; // (-pi..pi] to [0..2pi)
if(v<=R){ // only render in the circle
if((hue = hue.toDeg())<0) hue+=360; // (-pi..pi] to [0..2pi)
s:=v/d; // scale saturation zero at center to 1 at edge
s:=v/R; // scale saturation zero at center to 1 at edge
ppm[x,y]=hsv2rgb(hue,1.0,s);
ppm[x,y]=hsv2rgb(hue,1.0,s);
}
}
}
}
}