Skip to content

Instantly share code, notes, and snippets.

@ntlk
Created November 21, 2011 19:18
Show Gist options
  • Save ntlk/1383603 to your computer and use it in GitHub Desktop.
Save ntlk/1383603 to your computer and use it in GitHub Desktop.
converts RGB values to HSB
# Input rgb values 1...255
# based on http://forums.devshed.com/c-programming-42/rgb-to-hsv-conversion-rountine-162526.html
r = r / 255.0
g = g / 255.0
b = b / 255.0
max = [r, g, b].max
min = [r, g, b].min
delta = max - min
v = max * 100
if (max != 0.0)
s = delta / max *100
else
s = 0.0
end
if (s == 0.0)
h = 0.0
else
if (r == max)
h = (g - b) / delta
elsif (g == max)
h = 2 + (b - r) / delta
elsif (b == max)
h = 4 + (r - g) / delta
end
h *= 60.0
if (h < 0)
h += 360.0
end
end
# returns h in the range of 0..360 deg
# s 0...100
# v 0...100
@Alexx2
Copy link

Alexx2 commented Mar 7, 2014

Nice! I'm trying to write a HSV to RGB now... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment