Smooth color transition - rainbow$( ) function

Function rainbow$(x) returns smooth color transition which goes from red to violet
(rainbow order) and then again to red while x goes from 0 to 1.
After that it wraps around.
0 corresponds to red, 5/6 to violet (might be handy to drawing height maps).
Demo code:
'demo of a rainbow function
'by tsh73 Nov 2009
 
nomainwin
open "function" for graphics_nsb_nf as #grFunc
print #grFunc, "trapclose [trapclosehLabel]"
print #grFunc, "home ; down ; posxy x y"
w = 2*x: h = 2*y
 
'linear color spread
for x = 0 to w
    #grFunc, "color ";rainbow$(x/w)
    #grFunc, "place ";x;" ";y
    #grFunc, "box ";x+2;" ";y+20
next
'linear color spread. now from -1 to 2
for x = 0 to w
    #grFunc, "color ";rainbow$(x/w*3-1)
    #grFunc, "place ";x;" ";y+30
    #grFunc, "box ";x+2;" ";y+50
next
 
pi = acs(-1)
'curcular color spread
#grFunc, "size 10"
for a = 0 to 2*pi step 0.01
    x = w/2+w/3*cos(a)
    y = h/2+h/3*sin(a)
    #grFunc, "color ";rainbow$(a/(2*pi))
    #grFunc, "set ";x;" ";y
next
 
'height map, minimal violet, maximal red
#grFunc, "size 7"
for x = 0 to w
    xx = x/w*2*pi
    yy = (sin(xx)+1)/2    'function scaled to 0..1
    'in rainow$, 0 is red and 5/6 is violet. So we scale it more like this
    c=(1-yy)*5/6
    y = w/2-w/3*sin(xx)  'y is inverted hence '-'
    #grFunc, "color ";rainbow$(c)
    #grFunc, "set ";x;" ";y
next
#grFunc, "flush"
 
wait
 
[trapclosehLabel]
    close #grFunc
    end
 
'---------------------------------------------
' 0..1 into red-green-blue-red continuous colors
function rainbow$(x)
    hi = int((x*6) mod 6)+ 5*(x<0)  'fixed to 0..5
    f = (x*6) mod 1 + (x<0)         'frac, 0..1
    q = (1-f)
    select case hi
    case 0
        r = 1: g = f: b = 0
    case 1
        r = q: g = 1: b = 0
    case 2
        r = 0: g = 1: b = f
    case 3
        r = 0: g = q: b = 1
    case 4
        r = f: g = 0: b = 1
    case 5
        r = 1: g = 0: b = q
    end select
    R = int(r*255)
    G = int(g*255)
    B = int(b*255)
    rainbow$= R;" ";G;" ";B
end function