Task

Write a function to draw a HSV color wheel completely with code.

This is strictly for learning purposes only. It's highly recommended that you use an image in an actual application to actually draw the color wheel   (as procedurally drawing is super slow). This does help you understand how color wheels work and this can easily be used to determine a color value based on a position within a circle.



' -----------------------------------
' color wheel
' -----------------------------------
global pi
pi	= 22 / 7
steps	= 1
 
graphic #g, 525, 525
 
 
for x =0 to 525 step steps
	for y =0 to 525 step steps
		angle   = atan2(y - 250, x - 250) * 360 / 2 / pi      '  full degrees....
		sector  = int(angle / 60)                             '    60 degree sectors (0 to 5)
		slope   = (angle mod 60) /60 * 255                    '     1 degree sectors.
 
		if  sector = 0 then  col$    = "255 ";                    str$( int( slope));      "   0"
		if  sector = 1 then  col$    = str$(int(256 - slope)); " 255                          0"
		if  sector = 2 then  col$    = "0                         255 ";                     str$( int( slope))
		if  sector = 3 then  col$    = "0 ";                      str$( int( 256 -slope)); " 255"
		if  sector = 4 then  col$    = str$(int(slope));    "     0                        255"
		if  sector = 5 then  col$    = "255                         0 ";                     str$( int( 256 -slope))
 
		red	= val( word$( col$, 1))
		grn	= val( word$( col$, 2))
		blu	= val( word$( col$, 3))
		p	= ((x -270)^2 +(y -270)^2)^0.5 / 250
		r	= min(255,p * red)
		g	= min(255,p * grn)
		b	= min(255,p * blu)
		if p > 1 then  #g "color white" else #g color(r,g,b)
		#g "set "; x; " "; y
	next y
next x
render #g
end
 
function atan2(y,x)
if (x = 0) and (y <> 0) then
	r$ = "Y"
	if y > 0 then atan2 = pi /2
	if y < 0 then atan2 = 3 * pi /2
end if
 
if y = 0 and (x <> 0) then
	r$ = "Y"
	if x > 0 then atan2 = 0
	if x < 0 then atan2 = pi
end if
 
If r$ <> "Y" then
	if x = 0 and y = 0 then
		atan2	= 0
		else
		baseAngle = atn(abs(y) / abs(x))
		if x > 0 then
			if y > 0 then atan2 = baseAngle
			If y < 0 then atan2 = 2 * pi - baseAngle
		end if
		if x < 0 then
			If y > 0 then atan2 = pi - baseAngle
			If y < 0 then atan2 = pi + baseAngle
		end if
	end if
end if
end function