From 48f1500d4354c98feef89dabb8c0e74277c23d12 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 13 Apr 2021 23:02:28 +0200 Subject: [PATCH] [DEBUG] correct color parsing --- src/org/atriasoft/etk/Color.java | 44 ++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/org/atriasoft/etk/Color.java b/src/org/atriasoft/etk/Color.java index b1a4868..1b3eb68 100644 --- a/src/org/atriasoft/etk/Color.java +++ b/src/org/atriasoft/etk/Color.java @@ -335,29 +335,29 @@ public record Color( // #RRGGBBAA switch (color.length()) { case 4 -> { - final float r = Integer.parseInt(color.substring(1, 2), 16) * 255.0f * 16.0f; - final float g = Integer.parseInt(color.substring(2, 3), 16) * 255.0f * 16.0f; - final float b = Integer.parseInt(color.substring(3, 4), 16) * 255.0f * 16.0f; + final float r = Integer.parseInt(color.substring(1, 2), 16) / 15.0f; + final float g = Integer.parseInt(color.substring(2, 3), 16) / 15.0f; + final float b = Integer.parseInt(color.substring(3, 4), 16) / 15.0f; return new Color(r, g, b); } case 5 -> { - final float r = Integer.parseInt(color.substring(1, 2), 16) * 255.0f * 16.0f; - final float g = Integer.parseInt(color.substring(2, 3), 16) * 255.0f * 16.0f; - final float b = Integer.parseInt(color.substring(3, 4), 16) * 255.0f * 16.0f; - final float a = Integer.parseInt(color.substring(4, 5), 16) * 255.0f * 16.0f; + final float r = Integer.parseInt(color.substring(1, 2), 16) / 15.0f; + final float g = Integer.parseInt(color.substring(2, 3), 16) / 15.0f; + final float b = Integer.parseInt(color.substring(3, 4), 16) / 15.0f; + final float a = Integer.parseInt(color.substring(4, 5), 16) / 15.0f; return new Color(r, g, b, a); } case 7 -> { - final float r = Integer.parseInt(color.substring(1, 3), 16) * 255.0f; - final float g = Integer.parseInt(color.substring(3, 5), 16) * 255.0f; - final float b = Integer.parseInt(color.substring(5, 7), 16) * 255.0f; + final float r = Integer.parseInt(color.substring(1, 3), 16) / 255.0f; + final float g = Integer.parseInt(color.substring(3, 5), 16) / 255.0f; + final float b = Integer.parseInt(color.substring(5, 7), 16) / 255.0f; return new Color(r, g, b); } case 9 -> { - final float r = Integer.parseInt(color.substring(1, 3), 16) * 255.0f; - final float g = Integer.parseInt(color.substring(3, 5), 16) * 255.0f; - final float b = Integer.parseInt(color.substring(5, 7), 16) * 255.0f; - final float a = Integer.parseInt(color.substring(7, 9), 16) * 255.0f; + final float r = Integer.parseInt(color.substring(1, 3), 16) / 255.0f; + final float g = Integer.parseInt(color.substring(3, 5), 16) / 255.0f; + final float b = Integer.parseInt(color.substring(5, 7), 16) / 255.0f; + final float a = Integer.parseInt(color.substring(7, 9), 16) / 255.0f; return new Color(r, g, b, a); } default -> throw new Exception("Can not parse color ... '" + colorBase + "'"); @@ -531,4 +531,20 @@ public record Color( return new Color(this.r, this.g, this.b, value); } + public Color withR(final int value) { + return new Color(value / 255.0f, this.g, this.b, this.a); + } + + public Color withG(final int value) { + return new Color(this.r, value / 255.0f, this.b, this.a); + } + + public Color withB(final int value) { + return new Color(this.r, this.g, value / 255.0f, this.a); + } + + public Color withA(final int value) { + return new Color(this.r, this.g, this.b, value / 255.0f); + } + }