Skip to content

Instantly share code, notes, and snippets.

@ibaaj
Created February 6, 2014 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibaaj/8842347 to your computer and use it in GitHub Desktop.
Save ibaaj/8842347 to your computer and use it in GitHub Desktop.
Rgb2hsv in Coffeescript
rgb2hsv (r,g,b) ->
r=r/255
g=g/255
b=b/255
max = Math.max(r,g,b)
min = Math.min(r,g,b)
v = max
d = max - min
s = if max == 0 then 0 else d/max
h = if max == min then 0
else
switch max
when r then h = (g-b) / (d + ( if g < b then 6 else 0))
when g then h = (b-r) / d + 2
when b then h = (r-g) / d + 4
h / 6
[h,s,v]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment