Created
March 9, 2015 20:26
-
-
Save kmark/5b9a8116705e9004fd79 to your computer and use it in GitHub Desktop.
Retrieves the hue value in degrees from a packed (A)RGB color. Adapted from a C algorithm by Eugene Vishnevsky. http://www.cs.rit.edu/~ncs/color/t_convert.html
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
public class HueFromRgb { | |
// Retrieves the hue value in degrees from a packed (A)RGB color. | |
// Adapted from a C algorithm by Eugene Vishnevsky. | |
// http://www.cs.rit.edu/~ncs/color/t_convert.html | |
public static float hueFromRgb(int rgb) { | |
int r = (rgb >> 16) & 0xFF; | |
int g = (rgb >> 8) & 0xFF; | |
int b = rgb & 0xFF; | |
float min = Math.min(Math.min(r, g), b); | |
float max = Math.max(Math.max(r, g), b); | |
float delta = max - min; | |
if(max == 0x00 || min == 0xFF) { | |
// Black or white | |
return 0; | |
} | |
float h; | |
if(r == max) { | |
// Between yellow and magenta | |
h = (g - b) / delta; | |
} else if(g == max) { | |
// Between cyan and yellow | |
h = 2 + (b - r) / delta; | |
} else { | |
// Between magenta and cyan | |
h = 4 + (r - g) / delta; | |
} | |
// Degrees | |
h *= 60; | |
if(h < 0) { | |
h += 360; | |
} | |
return h; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment