From 00ebda0f17151bf9cdf2eb41cc58386733e9a3de Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Fri, 18 Sep 2015 14:38:10 +0300 Subject: [PATCH] create ColorTool --- .../ru/dmitriymx/lwjgl/tools/ColorTool.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java diff --git a/src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java b/src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java new file mode 100644 index 0000000..55c8820 --- /dev/null +++ b/src/java/ru/dmitriymx/lwjgl/tools/ColorTool.java @@ -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); + } +}