create ColorTool
This commit is contained in:
74
src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java
Normal file
74
src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package ru.dmitriymx.lwjgl.tools;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public class ColorTool {
|
||||
|
||||
public static float IntToFloat(int value) {
|
||||
return (value / 256.0f);
|
||||
}
|
||||
|
||||
public static float[] AwtColorToColor4f(Color awtColor) {
|
||||
return new float[] {
|
||||
IntToFloat(awtColor.getRed()),
|
||||
IntToFloat(awtColor.getGreen()),
|
||||
IntToFloat(awtColor.getBlue()),
|
||||
IntToFloat(awtColor.getAlpha())
|
||||
};
|
||||
}
|
||||
|
||||
//Examples:
|
||||
//#ffaabbcc = ARGB
|
||||
//#aabbcc = RGB
|
||||
//#fabc = ARGB
|
||||
//#abc = RGB
|
||||
public static float[] HexColorToColor4f(String hexColor) {
|
||||
if (hexColor.startsWith("#")) hexColor = hexColor.substring(1);
|
||||
|
||||
float[] color4f; // RGBA
|
||||
|
||||
switch (hexColor.length()) {
|
||||
case 8:
|
||||
color4f = new float[]{
|
||||
hex_to_float(hexColor.substring(2, 4)),
|
||||
hex_to_float(hexColor.substring(4, 6)),
|
||||
hex_to_float(hexColor.substring(6)),
|
||||
hex_to_float(hexColor.substring(0, 2))
|
||||
};
|
||||
break;
|
||||
case 6:
|
||||
color4f = new float[]{
|
||||
hex_to_float(hexColor.substring(0, 2)),
|
||||
hex_to_float(hexColor.substring(2, 4)),
|
||||
hex_to_float(hexColor.substring(4)),
|
||||
1f
|
||||
};
|
||||
break;
|
||||
case 4:
|
||||
color4f = new float[]{
|
||||
hex_to_float(hexColor.charAt(1) + "" + hexColor.charAt(1)),
|
||||
hex_to_float(hexColor.charAt(2) + "" + hexColor.charAt(2)),
|
||||
hex_to_float(hexColor.charAt(3) + "" + hexColor.charAt(3)),
|
||||
hex_to_float(hexColor.charAt(0) + "" + hexColor.charAt(0))
|
||||
};
|
||||
break;
|
||||
case 3:
|
||||
color4f = new float[]{
|
||||
hex_to_float(hexColor.charAt(0) + "" + hexColor.charAt(0)),
|
||||
hex_to_float(hexColor.charAt(1) + "" + hexColor.charAt(1)),
|
||||
hex_to_float(hexColor.charAt(2) + "" + hexColor.charAt(2)),
|
||||
1f
|
||||
};
|
||||
break;
|
||||
default:
|
||||
color4f = new float[]{0f, 0f, 0f, 1f};
|
||||
}
|
||||
|
||||
return color4f;
|
||||
}
|
||||
|
||||
private static float hex_to_float(String hex) {
|
||||
long value = Long.parseLong(hex, 16);
|
||||
return ((float)value / 256.0f);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user