Skip to content

Instantly share code, notes, and snippets.

@ayan4m1
Created November 23, 2015 01:53
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 ayan4m1/ad593e6f21ea32bf74bc to your computer and use it in GitHub Desktop.
Save ayan4m1/ad593e6f21ea32bf74bc to your computer and use it in GitHub Desktop.
hsv2rgb for libGDX
private final static float ONE_THIRD = 1/3f;
private final static float TWO_THIRDS = 2/3f;
/**
* Converts a vector of hue, saturation, and value into a vector of red, green, and blue
*
* @param hsv {@code com.badlogic.gdx.Vector3}, X set to hue (0-360), Y to saturation (0-1), and Z to value (0-1)
* @return {@code com.badlogic.gdx.Color}
*/
private Color hsv2rgb(Vector3 hsv) {
final Vector3 precompute = new Vector3(1, 1, 1).lerp(new Vector3(
(float)Math.abs((((hsv.x + 1) - Math.floor(hsv.x + 1)) * 6) - 3) - 1,
(float)Math.abs((((hsv.x + TWO_THIRDS) - Math.floor(hsv.x + TWO_THIRDS)) * 6) - 3) - 1,
(float)Math.abs((((hsv.x + ONE_THIRD) - Math.floor(hsv.x + ONE_THIRD)) * 6) - 3) - 1
).clamp(0f, 1f), hsv.y).scl(hsv.z);
return new Color(precompute.x, precompute.y, precompute.z, 1f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment