Created
October 21, 2012 00:01
-
-
Save wires/3925296 to your computer and use it in GitHub Desktop.
(trivial) Java integer math
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
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