Created
October 13, 2014 20:27
-
-
Save rev22/008d8868565e19d222d1 to your computer and use it in GitHub Desktop.
hsv2rgb rgb2hsv conversion (the hue value may have a different scale)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hsv2rgb = ([h,s,v])-> | |
return [v,v,v] if s is 0 | |
h = if h >= 1 then h - 1 else h | |
hh = h * 6 | |
hhh = Math.floor hh | |
f = hh - hhh | |
a = v * (1.0 - s) | |
b = v * (1.0 - s * f) | |
c = v * (1.0 - s * (1 - f)) | |
return switch hhh | |
when 0 then [ Math.round(v * 255), Math.round(c * 255), Math.round(a * 255) ] | |
when 1 then [ Math.round(b * 255), Math.round(v * 255), Math.round(a * 255) ] | |
when 2 then [ Math.round(a * 255), Math.round(v * 255), Math.round(c * 255) ] | |
when 3 then [ Math.round(a * 255), Math.round(b * 255), Math.round(v * 255) ] | |
when 4 then [ Math.round(c * 255), Math.round(a * 255), Math.round(v * 255) ] | |
when 5 then [ Math.round(v * 255), Math.round(a * 255), Math.round(b * 255) ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rgb2hsv = ([r,g,b])-> | |
r=r/255 | |
g=g/255 | |
b=b/255; | |
v = Math.max(r, Math.max(g,b)); | |
min = Math.min(r, Math.min(g,b)); | |
return [0,0,0] if v is 0 | |
d = v - min | |
s = d / v | |
if r is v | |
h = (g - b) / d | |
else if g is v | |
h = ((b - r) / d) + 2 | |
else | |
h = ((r - g) / d) + 4 | |
h /= 6 | |
h += 1 if h < 0 | |
[h,s,v] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment