ColorTool -> Color4f
This commit is contained in:
143
src/java/ru/dmitriymx/lwjgl/tools/Color4f.java
Normal file
143
src/java/ru/dmitriymx/lwjgl/tools/Color4f.java
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
package ru.dmitriymx.lwjgl.tools;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
|
public class Color4f {
|
||||||
|
private float red;
|
||||||
|
private float green;
|
||||||
|
private float blue;
|
||||||
|
private float alpha;
|
||||||
|
|
||||||
|
public Color4f(float red, float green, float blue) {
|
||||||
|
this(red, green, blue, 1f);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color4f(float red, float green, float blue, float alpha) {
|
||||||
|
setRed(red);
|
||||||
|
setGreen(green);
|
||||||
|
setBlue(blue);
|
||||||
|
setAlpha(alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getRed() {
|
||||||
|
return red;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getGreen() {
|
||||||
|
return green;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getBlue() {
|
||||||
|
return blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getAlpha() {
|
||||||
|
return alpha;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRed(float value) {
|
||||||
|
if (value > 1f) {
|
||||||
|
red = 1f;
|
||||||
|
} else if (value < 0f) {
|
||||||
|
red = 0f;
|
||||||
|
} else {
|
||||||
|
red = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGreen(float value) {
|
||||||
|
if (value > 1f) {
|
||||||
|
green = 1f;
|
||||||
|
} else if (value < 0f) {
|
||||||
|
green = 0f;
|
||||||
|
} else {
|
||||||
|
green = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBlue(float value) {
|
||||||
|
if (value > 1f) {
|
||||||
|
blue = 1f;
|
||||||
|
} else if (value < 0f) {
|
||||||
|
blue = 0f;
|
||||||
|
} else {
|
||||||
|
blue = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAlpha(float value) {
|
||||||
|
if (value > 1f) {
|
||||||
|
alpha = 1f;
|
||||||
|
} else if (value < 0f) {
|
||||||
|
alpha = 0f;
|
||||||
|
} else {
|
||||||
|
alpha = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Color4f AwtColorToColor4f(Color awtColor) {
|
||||||
|
return new Color4f(
|
||||||
|
int_to_float(awtColor.getRed()),
|
||||||
|
int_to_float(awtColor.getGreen()),
|
||||||
|
int_to_float(awtColor.getBlue()),
|
||||||
|
int_to_float(awtColor.getAlpha())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Examples:
|
||||||
|
//#ffaabbcc = ARGB
|
||||||
|
//#aabbcc = RGB
|
||||||
|
//#fabc = ARGB
|
||||||
|
//#abc = RGB
|
||||||
|
public static Color4f HexColorToColor4f(String hexColor) {
|
||||||
|
if (hexColor.startsWith("#")) hexColor = hexColor.substring(1);
|
||||||
|
|
||||||
|
Color4f color4f; // RGBA
|
||||||
|
|
||||||
|
switch (hexColor.length()) {
|
||||||
|
case 8:
|
||||||
|
color4f = new Color4f(
|
||||||
|
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 Color4f(
|
||||||
|
hex_to_float(hexColor.substring(0, 2)),
|
||||||
|
hex_to_float(hexColor.substring(2, 4)),
|
||||||
|
hex_to_float(hexColor.substring(4))
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
color4f = new Color4f(
|
||||||
|
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 Color4f(
|
||||||
|
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))
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
color4f = new Color4f(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return color4f;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float int_to_float(int value) {
|
||||||
|
return (value / 256.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float hex_to_float(String hex) {
|
||||||
|
long value = Long.parseLong(hex, 16);
|
||||||
|
return ((float)value / 256.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -72,6 +72,10 @@ public class Tessellator {
|
|||||||
return setColor(red, green, blue, 1f);
|
return setColor(red, green, blue, 1f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Tessellator setColor(Color4f color4f) {
|
||||||
|
return setColor(color4f.getRed(), color4f.getGreen(), color4f.getBlue(), color4f.getAlpha());
|
||||||
|
}
|
||||||
|
|
||||||
// DISPLAY LIST (DL) ======================================================
|
// DISPLAY LIST (DL) ======================================================
|
||||||
|
|
||||||
public void startDrawingUseDL(int drawMode) {
|
public void startDrawingUseDL(int drawMode) {
|
||||||
|
|||||||
Reference in New Issue
Block a user