Created
November 21, 2011 19:18
-
-
Save ntlk/1383603 to your computer and use it in GitHub Desktop.
converts RGB values to HSB
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! I'm trying to write a HSV to RGB now... :)