Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,7 @@ static ShadowColor shadowColor(final ARGBLike argb) {
* @since 4.18.0
*/
default String asHexString() {
final int argb = this.value();
final int a = (argb >> 24) & 0xFF;
final int r = (argb >> 16) & 0xFF;
final int g = (argb >> 8) & 0xFF;
final int b = argb & 0xFF;
return String.format("#%02X%02X%02X%02X", r, g, b, a);
return ShadowColorImpl.asHexString(this.value());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@
*/
package net.kyori.adventure.text.format;

import java.util.HexFormat;

// value is argb
record ShadowColorImpl(int value) implements ShadowColor {
static final int NONE_VALUE = 0;
static final ShadowColorImpl NONE = new ShadowColorImpl(NONE_VALUE);
static final HexFormat HEX_FORMATTER = HexFormat.of().withUpperCase();

/**
* Convert an ARGB value to a hex string in the format of #RRGGBBAA.
*
* @param argb the ARGB value
* @return the uppercase hex string
*/
static String asHexString(final int argb) {
final int rgba = Integer.rotateLeft(argb, Byte.SIZE);
return '#' + HEX_FORMATTER.toHexDigits(rgba, 8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package net.kyori.adventure.text.format;

import java.util.List;
import java.util.Locale;
import net.kyori.adventure.util.HSVLike;
import net.kyori.adventure.util.RGBLike;
import org.jetbrains.annotations.Range;
Expand Down Expand Up @@ -212,14 +211,7 @@ static TextColor color(final float r, final float g, final float b) {
* @since 4.0.0
*/
default String asHexString() {
final StringBuilder result = new StringBuilder();
result.append(HEX_PREFIX);
final String hex = Integer.toHexString(this.value());
for (int i = 0; i < 6 - hex.length(); i++) {
result.append('0');
}
result.append(hex);
return result.toString().toUpperCase(Locale.ROOT);
return TextColorImpl.hexString(this.value());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
*/
package net.kyori.adventure.text.format;

import java.util.HexFormat;
import net.kyori.adventure.util.HSVLike;
import org.jetbrains.annotations.Debug;

@Debug.Renderer(text = "asHexString()")
record TextColorImpl(int value) implements TextColor {
private static final HexFormat HEX_FORMATTER = HexFormat.of().withUpperCase();

@Override
public String toString() {
Expand All @@ -50,4 +52,14 @@ static float distance(final HSVLike self, final HSVLike other) {
final float valueDiff = self.v() - other.v();
return hueDistance * hueDistance + saturationDiff * saturationDiff + valueDiff * valueDiff;
}

/**
* Parses a value into an RGB hex string using the format #RRGGBB.
*
* @param value the value in RRGGBB in the range of 0x0 to 0xffffff
* @return an uppercase hex string
*/
public static String hexString(final int value) {
return HEX_CHARACTER + HEX_FORMATTER.toHexDigits(value, 6);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ void testSimple() {
assertNearest(NamedTextColor.DARK_GRAY, 0x4c4c4c);
}

@Test
void testAsHexString() {
assertEquals("#FF5555", NamedTextColor.RED.asHexString());
}

private static void assertNearest(final NamedTextColor expected, final int value) {
final NamedTextColor nearest = NamedTextColor.nearestTo(TextColor.color(value));
assertEquals(expected, nearest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,10 @@ void testEquality() {
)
.testEquals();
}

@Test
void testAsHexString() {
assertEquals("#AABBCCDD", ShadowColor.shadowColor(0xDDAABBCC).asHexString());
assertEquals("#FFFFFFFF", ShadowColor.shadowColor(0xFFFFFFFF).asHexString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ void testCSSHexStringThreeDigit() {
final TextColor original = TextColor.color(0x77ff11);
assertEquals(original, TextColor.fromCSSHexString("#7f1"));
}

@Test
void testAsHexString() {
assertEquals("#7F1E2D", TextColor.color(0x7f1e2d).asHexString());
}
}