aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig
diff options
context:
space:
mode:
authorDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-05-16 20:32:02 +0200
committerDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-05-16 20:32:02 +0200
commit9f342f89ba2427ed9f154a1e504b81a757ef8e72 (patch)
treed8eb8fe5b74610d4e4800423331c87b0d973b7bb /src/main/java/cc/polyfrost/oneconfig
parent634121e8d953a999ab4d27bd4ebe66977e6b16ba (diff)
downloadOneConfig-9f342f89ba2427ed9f154a1e504b81a757ef8e72.tar.gz
OneConfig-9f342f89ba2427ed9f154a1e504b81a757ef8e72.tar.bz2
OneConfig-9f342f89ba2427ed9f154a1e504b81a757ef8e72.zip
finish small color thing
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java51
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/lwjgl/OneColor.java9
2 files changed, 34 insertions, 26 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java
index fb63ca1..7986598 100644
--- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java
+++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java
@@ -11,9 +11,7 @@ import cc.polyfrost.oneconfig.lwjgl.RenderManager;
import cc.polyfrost.oneconfig.lwjgl.font.Fonts;
import cc.polyfrost.oneconfig.lwjgl.image.Images;
import cc.polyfrost.oneconfig.utils.InputUtils;
-import gg.essential.universal.UResolution;
-import java.awt.*;
import java.lang.reflect.Field;
public class ConfigColorElement extends BasicOption {
@@ -37,17 +35,26 @@ public class ConfigColorElement extends BasicOption {
} catch (IllegalAccessException e) {
return;
}
- String buf1 = Integer.toHexString(color.getRGB());
- String hex = "#" + buf1.substring(buf1.length() - 6);
- hexField.setInput(hex);
RenderManager.drawString(vg, name, x, y + 16, OneConfigConfig.WHITE_90, 14f, Fonts.MEDIUM);
+ if (!hexField.isToggled()) hexField.setInput("#" + color.getHex());
+ hexField.setErrored(false);
+ if (hexField.isToggled()) {
+ try {
+ int alpha = color.getAlpha();
+ color.setColorFromHex(hexField.getInput());
+ color.setAlpha(alpha);
+ setColor(color);
+ } catch (NumberFormatException e) {
+ hexField.setErrored(true);
+ }
+ }
hexField.draw(vg, x1 + 224, y);
- if (!alphaField.isToggled()) alphaField.setInput(String.valueOf((color.getAlpha())));
+ if (!alphaField.isToggled()) alphaField.setInput(Math.round(color.getAlpha() / 2.55f) + "%");
alphaField.setErrored(false);
if (alphaField.isToggled()) {
try {
- float input = Float.parseFloat(alphaField.getInput());
+ float input = Float.parseFloat(alphaField.getInput().replace("%", ""));
if (input < 0f) {
alphaField.setErrored(true);
input = 100f;
@@ -56,6 +63,8 @@ public class ConfigColorElement extends BasicOption {
alphaField.setErrored(true);
input = 100f;
}
+ color = new OneColor((float) color.getHue(), color.getSaturation(), color.getBrightness(), Math.round(input * 2.55f));
+ setColor(color);
} catch (NumberFormatException e) {
alphaField.setErrored(true);
}
@@ -65,29 +74,23 @@ public class ConfigColorElement extends BasicOption {
element.update(x1 + 416, y);
RenderManager.drawRoundImage(vg, Images.ALPHA_GRID, x1 + 416, y, 64, 32, 12f);
RenderManager.drawRoundedRect(vg, x1 + 416, y, 64, 32, color.getRGB(), 12f);
- RenderManager.drawHollowRoundRect(vg, x1 + 416, y, 64, 32, OneConfigConfig.GRAY_300, 12f, 2f);
+ RenderManager.drawHollowRoundRect(vg, x1 + 415, y - 1, 64, 32, OneConfigConfig.GRAY_300, 12f, 2f);
if (element.isClicked() && !element.isToggled()) {
OneConfigGui.INSTANCE.initColorSelector(new ColorSelector(new OneColor(40, 30, 20), InputUtils.mouseX(), InputUtils.mouseY()));
}
}
- // thanks stack overflow
- public static OneColor HexToColor(String hex) throws NumberFormatException {
- hex = hex.replace("#", "");
- switch (hex.length()) {
- case 6:
- return new OneColor(
- Integer.valueOf(hex.substring(0, 2), 16),
- Integer.valueOf(hex.substring(2, 4), 16),
- Integer.valueOf(hex.substring(4, 6), 16));
- case 8:
- return new OneColor(
- Integer.valueOf(hex.substring(0, 2), 16),
- Integer.valueOf(hex.substring(2, 4), 16),
- Integer.valueOf(hex.substring(4, 6), 16),
- Integer.valueOf(hex.substring(6, 8), 16));
+ @Override
+ public void keyTyped(char key, int keyCode) {
+ alphaField.keyTyped(key, keyCode);
+ hexField.keyTyped(key, keyCode);
+ }
+
+ private void setColor(OneColor color) {
+ try {
+ set(color);
+ } catch (IllegalAccessException ignored) {
}
- throw new NumberFormatException("Invalid hex string: " + hex);
}
@Override
diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/OneColor.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/OneColor.java
index 0e72fd4..d598e07 100644
--- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/OneColor.java
+++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/OneColor.java
@@ -11,7 +11,7 @@ import java.awt.*;
* short[0] = hue (0-360)
* short[1] = saturation (0-100)
* short[2] = brightness (0-100)
- * short[3] = alpha (0-100)
+ * short[3] = alpha (0-255)
* </code>
*/
@SuppressWarnings("unused")
@@ -215,7 +215,7 @@ public class OneColor {
}
public void setColorFromHex(String hex) {
- hex = hex.replaceAll("#", "");
+ hex = hex.replace("#", "");
if(hex.length() == 3) {
hex = charsToString(hex.charAt(0), hex.charAt(0), hex.charAt(1), hex.charAt(1), hex.charAt(2), hex.charAt(2));
}
@@ -229,6 +229,11 @@ public class OneColor {
hsba = RGBAtoHSBA(0xff000000 | rgba);
}
+ public void setAlpha(int alpha) {
+ this.hsba[3] = (short) alpha;
+ rgba = HSBAtoRGBA(this.hsba[0], this.hsba[1], this.hsba[2], this.hsba[3]);
+ }
+
private String charsToString(char... chars) {
StringBuilder sb = new StringBuilder();
for(char c : chars) {