Color wheel: Difference between revisions

m (→‎{{header|Perl 6}}: Better variable naming, minor style tweaks)
Line 72:
 
Until local image uploading is re-enabled, see [https://github.com/thundergnat/rc/blob/master/img/Color-wheel-perl6.png Color-wheel-perl6.png]
 
=={{header|zkl}}==
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).
<lang zkl>var w=300,h=300,out=PPM(w,h);
colorWheel(out);
out.writeJPGFile("colorWheel.zkl.jpg");
fcn colorWheel(ppm){
centerX,centerY:=ppm.w/2,ppm.h/2;
d:=centerX.toFloat().hypot(centerY);
foreach x,y in (w,h){
v,hue:=(x - centerX).toFloat().toPolar(y - centerY);
if((hue = hue.toDeg())<0) hue+=360; // (-pi..pi] to [0..2pi)
s:=v/d; // scale saturation zero at center to 1 at edge
ppm[x,y]=hsv2rgb(hue,1.0,s);
}
}
 
fcn hsv2rgb(hue,v,s){ // 0<=H<360, 0<=v(brightness)<=1, 0<=saturation<=1
// --> 24 bit RGB each R,G,B in [0..255]
to24bit:=fcn(r,g,b,m){
r,g,b=((r+m)*255).toInt(),((g+m)*255).toInt(),((b+m)*255).toInt();
r*0x10000 + g*0x100 + b
};
c:=v*s;
x:=c*(1.0 - (hue.toFloat()/60%2 - 1).abs());
m:=v - c;
if (0 <=hue< 60) return(to24bit(c, x, 0.0,m));
else if(60 <=hue<120) return(to24bit(x, c, 0.0,m));
else if(120<=hue<180) return(to24bit(0.0,c, x, m));
else if(180<=hue<240) return(to24bit(0.0,x, c, m));
else if(240<=hue<300) return(to24bit(x, 0.0,c, m));
else return(to24bit(c, 0.0,x, m));
}</lang>
Until local image uploading is re-enabled, see [http://www.zenkinetic.com/Documents/colorWheel.zkl.jpg this image].
Anonymous user