Skip to content

Instantly share code, notes, and snippets.

@wires
Created October 21, 2012 00:01
Show Gist options
  • Save wires/3925296 to your computer and use it in GitHub Desktop.
Save wires/3925296 to your computer and use it in GitHub Desktop.
(trivial) Java integer math
import static java.lang.Math.max;
import static java.lang.Math.min;
public class Math
{
// clamp(n) = n such that min <= final <= max
public static final int clamp(final int min, final int value, final int max)
{
return max(min, min(value, max));
}
// ARGB pack four values [0..255]
public int argb(final int r, final int g, final int b, final int a)
{
final int A = (a >> 24);
final int R = (r >> 16) & 0xff;
final int G = (g >> 8) & 0xff;
final int B = b & 0xff;
return A|R|G|B;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment