Sped up HSV() slightly.

This commit is contained in:
Garth Minette 2021-07-30 13:38:25 -07:00
parent 7e75d03f6a
commit b5e6a08f51

View File

@ -824,13 +824,23 @@ module HSL(h,s=1,l=0.5,a=1) color(HSL(h,s,l),a) children();
// rgb = HSV(h=270,s=0.75,v=0.9);
// color(rgb) cube(60, center=true);
function HSV(h,s=1,v=1) =
assert(s>=0 && s<=1)
assert(v>=0 && v<=1)
let(
h=posmod(h,360),
v2=v*(1-s),
r=lookup(h,[[0,v], [60,v], [120,v2], [240,v2], [300,v], [360,v]]),
g=lookup(h,[[0,v2], [60,v], [180,v], [240,v2], [360,v2]]),
b=lookup(h,[[0,v2], [120,v2], [180,v], [300,v], [360,v2]])
) [r,g,b];
h = posmod(h,360),
c = v * s,
hprime = h/60,
x = c * (1- abs(hprime % 2 - 1)),
rgbprime = hprime <=1 ? [c,x,0]
: hprime <=2 ? [x,c,0]
: hprime <=3 ? [0,c,x]
: hprime <=4 ? [0,x,c]
: hprime <=5 ? [x,0,c]
: hprime <=6 ? [c,0,x]
: [0,0,0],
m=v-c
)
rgbprime+[m,m,m];
module HSV(h,s=1,v=1,a=1) color(HSV(h,s,v),a) children();