Skip to content

Instantly share code, notes, and snippets.

@JoseRivas1998
Last active July 27, 2017 15:59
Show Gist options
  • Select an option

  • Save JoseRivas1998/1063479af05aedad7d4d65b5b9a304aa to your computer and use it in GitHub Desktop.

Select an option

Save JoseRivas1998/1063479af05aedad7d4d65b5b9a304aa to your computer and use it in GitHub Desktop.
Makes gl color with a hex int.
public class Color {
float r, g, b;
public Color(int color) {
this.r = ((color >> 16) & 0xFF) / 255f;
this.g = ((color >> 8) & 0xFF) / 255f;
this.b = ((color >> 0) & 0xFF) / 255f;
}
@Override
public String toString() {
return String.format("rgb(%f, %f, %f)", r, g, b);
}
}
public class HelloWorld{
public static void main(String[] args) {
System.out.println(new Color(0xFFFFFF));
System.out.println(new Color(0xD2D2FF));
System.out.println(new Color(0x777777));
System.out.println(new Color(0x000000));
}
}
public class ParseLongHex {
public static void main(String []args){
String input = "#80a2d2ff";
String hex = input.replace("#", "");
int color = 0;
int alpha = 0xFF;
if(hex.length() == 6) {
color = parseHex(hex);
} else if(hex.length() == 8) {
color = parseHex(hex.substring(0,6));
alpha = parseHex(hex.substring(6));
} else {
throw new NumberFormatException("Hex number must have 6 or 8 characters (not including #)");
}
long l = Long.parseLong("80a2d277", 16);
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;
System.out.println(Integer.toHexString(color));
System.out.println(Integer.toHexString(alpha));
System.out.println(String.format("rgba(%d, %d, %d, %d)", r, g, b, alpha));
}
public static int parseHex(String hex) {
return Integer.parseInt(hex.replace("#", ""), 16);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment