From 360fff63939ea3c46e6abe1a2ca3759b378b2c2e Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Mon, 21 Sep 2015 09:56:33 +0300 Subject: [PATCH] ColorTool -> Color4f --- .../ru/dmitriymx/lwjgl/tools/Color4f.java | 143 ++++++++++++++++++ .../ru/dmitriymx/lwjgl/tools/ColorTool.java | 74 --------- .../ru/dmitriymx/lwjgl/tools/Tessellator.java | 4 + 3 files changed, 147 insertions(+), 74 deletions(-) create mode 100644 src/java/ru/dmitriymx/lwjgl/tools/Color4f.java delete mode 100644 src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java diff --git a/src/java/ru/dmitriymx/lwjgl/tools/Color4f.java b/src/java/ru/dmitriymx/lwjgl/tools/Color4f.java new file mode 100644 index 0000000..8e5f24a --- /dev/null +++ b/src/java/ru/dmitriymx/lwjgl/tools/Color4f.java @@ -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); + } +} diff --git a/src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java b/src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java deleted file mode 100644 index 55c8820..0000000 --- a/src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java +++ /dev/null @@ -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); - } -} diff --git a/src/java/ru/dmitriymx/lwjgl/tools/Tessellator.java b/src/java/ru/dmitriymx/lwjgl/tools/Tessellator.java index 54dc4f5..45cdc04 100644 --- a/src/java/ru/dmitriymx/lwjgl/tools/Tessellator.java +++ b/src/java/ru/dmitriymx/lwjgl/tools/Tessellator.java @@ -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) ======================================================