Archived
0

ColorTool -> Color4f

This commit is contained in:
2015-09-21 09:56:33 +03:00
parent a1a8d6bf74
commit 360fff6393
3 changed files with 147 additions and 74 deletions

View 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);
}
}

View File

@@ -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);
}
}

View File

@@ -71,6 +71,10 @@ public class Tessellator {
public Tessellator setColor(float red, float green, float blue) {
return setColor(red, green, blue, 1f);
}
public Tessellator setColor(Color4f color4f) {
return setColor(color4f.getRed(), color4f.getGreen(), color4f.getBlue(), color4f.getAlpha());
}
// DISPLAY LIST (DL) ======================================================