From c5046bbb4bdf9c04f1ca394d54017132bd49f9ec Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 26 May 2025 07:56:45 +0200 Subject: [PATCH] [FEAT] add generation color with sharp --- src/main/org/atriasoft/etk/Color.java | 37 +++++++++++++++++---------- src/main/org/atriasoft/etk/Uri.java | 10 +++++++- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/org/atriasoft/etk/Color.java b/src/main/org/atriasoft/etk/Color.java index 338924b..7cd2c5f 100644 --- a/src/main/org/atriasoft/etk/Color.java +++ b/src/main/org/atriasoft/etk/Color.java @@ -4,7 +4,6 @@ import java.util.Map; import org.atriasoft.etk.math.FMath; - //@formatter:off public record Color( float r, @@ -332,35 +331,35 @@ public record Color( // #RGBA // #RRGGBB // #RRGGBBAA - switch (color.length()) { + return switch (color.length()) { case 4 -> { 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); + yield new Color(r, g, b); } case 5 -> { 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); + yield 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; - return new Color(r, g, b); + yield 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; - return new Color(r, g, b, a); + yield new Color(r, g, b, a); } default -> throw new Exception("Can not parse color ... '" + colorBase + "'"); - } + }; } else { // Model: r.r,g.g,b.b // r.r,g.g,b.b,a.a @@ -416,35 +415,35 @@ public record Color( // #RGBA // #RRGGBB // #RRGGBBAA - switch (color.length()) { + return 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; - return new Color(r, g, b); + yield 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; - return new Color(r, g, b, a); + yield 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; - return new Color(r, g, b); + yield 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; - return new Color(r, g, b, a); + yield new Color(r, g, b, a); } default -> throw new Exception("Can not parse color ... '" + colorBase + "'"); - } + }; } else { // Model: r.r,g.g,b.b // r.r,g.g,b.b,a.a @@ -513,6 +512,18 @@ public record Color( public String toString() { return "rgba(" + this.r + ", " + this.g + ", " + this.b + ", " + this.a + ")"; } + + public String toStringSharp() { + final StringBuilder out = new StringBuilder(); + out.append("#"); + out.append(String.format("%02X", Math.round(this.r * 255))); + out.append(String.format("%02X", Math.round(this.g * 255))); + out.append(String.format("%02X", Math.round(this.b * 255))); + if (this.a <= 0.999999) { + out.append(String.format("%02X", Math.round(this.a * 255))); + } + return out.toString(); + } public Color withR(final float value) { return new Color(value, this.g, this.b, this.a); diff --git a/src/main/org/atriasoft/etk/Uri.java b/src/main/org/atriasoft/etk/Uri.java index 6f971a5..ef7736f 100644 --- a/src/main/org/atriasoft/etk/Uri.java +++ b/src/main/org/atriasoft/etk/Uri.java @@ -67,6 +67,14 @@ public class Uri { return data; } + public static String getAllDataString(final Uri resourceName) { + final byte[] data = getAllData(resourceName); + if (data == null) { + return null; + } + return new String(data); + } + private static List getResourceFiles(final Class clazz, final String path) throws IOException { final List filenames = new ArrayList<>(); @@ -121,7 +129,7 @@ public class Uri { out = Uri.applicationClass.getResourceAsStream("/" + tmpPath); if (out == null) { - LOGGER.error("(appl) ==> element does not exist ... {}", uri); + LOGGER.trace("(appl) ==> element does not exist ... {} => {}", uri, tmpPath); /* try { LOGGER.warn("elements: " + getResourceFiles(applicationClass,