Skip to content

Instantly share code, notes, and snippets.

@kmark
Created March 9, 2015 20:26
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 kmark/5b9a8116705e9004fd79 to your computer and use it in GitHub Desktop.
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
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