Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Color Wheel
#1
   
click the image to zoom in

Code:
'color wheel
'converted to N7
'https://rosettacode.org/wiki/Color_wheel#Go

#win32
set window "Color Wheel",500,500,false
set redraw off

visible tau = 2 * PI
visible r,g,b

'--------------
' main program
'--------------
set color 0,0,0; cls
colorWheel()
redraw
while not keydown(KEY_ESCAPE) fwait 60

'-----------
' functions
'------------
function hsb2rgb(hue, sat, bri)
    'convert hue,saturation,brightness to RGB value
    u = int(bri*255 + 0.5)
    if sat = 0 then
        r = u
        g = u
        b = u
    else
        h = (hue - floor(hue)) * 6
        f = h - floor(h)
        p = int(bri*(1-sat)*255 + 0.5)
        q = int(bri*(1-sat*f)*255 + 0.5)
        t = int(bri*(1-sat*(1-f))*255 + 0.5)
       
        select int(h)
        case 0
            r = u; g = t; b = p
        case 1
            r = q; g = u; b = p
        case 2
            r = p ; g = u ; b = t
        case 3
            r = p ; g = q ; b = u
        case 4
            r = t ; g = p ; b = u
        case 5
            r = u ; g = p ; b = q
        endsel
       
    endif
endfunc

function colorWheel()
    'calculate center and radius
    centerX = width()/2
    centerY = height()/2
    radius = centerX-20
    if centerY < radius then
        radius = centerY-20
    endif
   
    'draw pixels for each RGB value
    for y = 0 to height()
        dy = (y - centerY)
        for x = 0 to width()
            dx = (x - centerX)
            dist = sqr(dx*dx + dy*dy)
            if dist <= radius then
                theta = atan2(dy, dx)
                hue = (theta + PI) / tau
                hsb2rgb(hue, 1, 1)
               
                set color r,g,b
                set pixel x,y
            endif
        next
    next
   
endfunc
Reply
#2
Very good - thanks for sharing.
Reply
#3
Cool... Well done.
Logic is the beginning of wisdom.
Reply
#4
Very nice!

I might add an alternative "color picker" to ngui using that for hue selection. The current one uses a rectangle where the horizontal axis represents hue and the vertical saturation. With a hue wheel you'd need two sliders to control the saturation and brightness, right? But I think it would be easier to use than the current color picker:

Code:
include "ngui.n7"

set window "Color picker", 640, 480
set redraw off

root = VBox(SIZE_EXPAND, SIZE_EXPAND)
root.SetHalign(ALIGN_CENTER)
root.SetSpacing(8)

colorPicker = ColorPicker(function(wdg, r, g, b)
        pln "Selected color: " + r + ", " + g + ", " + b
    endfunc)
   
root.Add(Header("Color picker", SIZE_AUTO, SIZE_AUTO))
root.Add(colorPicker)

EnterMainLoop(root)
Reply
#5
That is so user friendly, and the code is so compact. Very impressive......
Reply
#6
Impressive. Most impressive. Obi-wan has taught you well...
Logic is the beginning of wisdom.
Reply
#7
(03-01-2025, 07:38 AM)Marcus Wrote: ...
"color picker" to ngui
...

   
click the image to zoom in

Oh, color picker, help me see,
The perfect shades to set games free
For NAALAA devs, you light the way
With colors bright, no more delay
Reply


Forum Jump:


Users browsing this thread: 8 Guest(s)