From a0d2235c86baa501fccaee434d97ecbb9bbff7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Tr=C3=A9guier?= Date: Mon, 16 Sep 2024 17:05:01 +0200 Subject: [PATCH] Send tint as proper RGB color --- src/main/java/app/fyreplace/api/data/Color.java | 8 ++++++++ src/main/java/app/fyreplace/api/data/User.java | 11 +++++------ 2 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 src/main/java/app/fyreplace/api/data/Color.java diff --git a/src/main/java/app/fyreplace/api/data/Color.java b/src/main/java/app/fyreplace/api/data/Color.java new file mode 100644 index 0000000..6708dd3 --- /dev/null +++ b/src/main/java/app/fyreplace/api/data/Color.java @@ -0,0 +1,8 @@ +package app.fyreplace.api.data; + +import org.eclipse.microprofile.openapi.annotations.media.Schema; + +public record Color( + @Schema(required = true, minimum = "0", maximum = "255", format = "uint8") int r, + @Schema(required = true, minimum = "0", maximum = "255", format = "uint8") int g, + @Schema(required = true, minimum = "0", maximum = "255", format = "uint8") int b) {} diff --git a/src/main/java/app/fyreplace/api/data/User.java b/src/main/java/app/fyreplace/api/data/User.java index 6269452..8a31c6c 100644 --- a/src/main/java/app/fyreplace/api/data/User.java +++ b/src/main/java/app/fyreplace/api/data/User.java @@ -14,7 +14,6 @@ import jakarta.persistence.Table; import jakarta.ws.rs.NotAuthorizedException; import jakarta.ws.rs.core.SecurityContext; -import java.awt.Color; import java.security.MessageDigest; import java.time.Duration; import java.time.Instant; @@ -156,16 +155,16 @@ public boolean getBlocked() { } @SneakyThrows - @Schema(required = true, pattern = "^#[A-F0-9]{6}$") - public String getTint() { + @Schema(required = true) + public Color getTint() { final var md5 = MessageDigest.getInstance("MD5"); final var digest = md5.digest(username.getBytes()); final var hue = bytesToFloat(digest); final var h = hue * 6; final var variance = Math.abs(h - (float) Math.round(h)) * 0.15f; final var brightness = Math.round(h) % 2 == 0 ? 0.75f - variance : 0.6f + variance; - final var color = Color.HSBtoRGB(hue, 0.5f, brightness); - return "#%02X%02X%02X".formatted((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF); + final var color = java.awt.Color.HSBtoRGB(hue, 0.5f, brightness); + return new Color((color >> 2 * Byte.SIZE) & 0xFF, (color >> Byte.SIZE) & 0xFF, color & 0xFF); } @Override @@ -290,5 +289,5 @@ public record Profile( @Schema(required = true) UUID id, @Schema(required = true) String username, @Schema(required = true) String avatar, - @Schema(required = true) String tint) {} + @Schema(required = true) Color tint) {} }