-- lua script for Hue Wheel
-- Fill page with a hue saturation span without adjusting the alpha channel
-- some basic constants
local w_mid, h_mid, r2d = width / 2, height / 2, 180 / 3.14159265
local maxradius = math.min( w_mid, h_mid)
local bwpivot = maxradius  / 2
local userrotation =  (sliderVal1 * 360)
-- allow the user to rotate the wheel  (note range is 0 .. 1.0
--@TBCONFIG SLIDER: Hue_Offset, 0.0

for y = 0, height - 1 do
  for x = 0, width - 1 do
    local angle, cx, cy, sat, lum
    cx = w_mid - x
    cy = h_mid - y
    -- want angle from centre need to coirrect for atan2 range first
    angle = r2d * math.atan2(cx, cy) + 270
    if angle > 360 then angle = angle - 360 end
    -- now add in user desired offset / rotation
    angle = angle + userrotation
    if angle > 360 then angle = angle - 360 end
    radius = math.sqrt((cx * cx) + (cy * cy));
    if radius < bwpivot then    
       lum = 1 - (radius / bwpivot) / 2
    else
	lum =   math.max(0, 1 - (radius  / maxradius))   
    end

    set_alpha(x, y, 1)
    set_hsl(x, y, angle, 1, lum)
  end
end
