Skip to content

Commit

Permalink
Send tint as proper RGB color
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Sep 16, 2024
1 parent 08fc14c commit a0d2235
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/main/java/app/fyreplace/api/data/Color.java
Original file line number Diff line number Diff line change
@@ -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) {}
11 changes: 5 additions & 6 deletions src/main/java/app/fyreplace/api/data/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {}
}

0 comments on commit a0d2235

Please sign in to comment.