Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use constants for hex character #929

Merged
merged 1 commit into from
Jun 7, 2023
Merged
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
19 changes: 16 additions & 3 deletions api/src/main/java/net/kyori/adventure/text/format/TextColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@
* @since 4.0.0
*/
public interface TextColor extends Comparable<TextColor>, Examinable, RGBLike, StyleBuilderApplicable, TextFormat {
/**
* The hex character.
*
* @since 4.14.0
*/
char HEX_CHARACTER = '#';
/**
* The hex character, in {@code String} format.
*
* @since 4.14.0
*/
String HEX_PREFIX = "#";

/**
* Creates a new text colour.
*
Expand Down Expand Up @@ -144,7 +157,7 @@ public interface TextColor extends Comparable<TextColor>, Examinable, RGBLike, S
* @since 4.0.0
*/
static @Nullable TextColor fromHexString(final @NotNull String string) {
if (string.startsWith("#")) {
if (string.startsWith(HEX_PREFIX)) {
try {
final int hex = Integer.parseInt(string.substring(1), 16);
return color(hex);
Expand All @@ -163,7 +176,7 @@ public interface TextColor extends Comparable<TextColor>, Examinable, RGBLike, S
* @since 4.0.0
*/
static @Nullable TextColor fromCSSHexString(final @NotNull String string) {
if (string.startsWith("#")) {
if (string.startsWith(HEX_PREFIX)) {
final String hexString = string.substring(1);
if (hexString.length() != 3 && hexString.length() != 6) {
return null;
Expand Down Expand Up @@ -202,7 +215,7 @@ public interface TextColor extends Comparable<TextColor>, Examinable, RGBLike, S
* @since 4.0.0
*/
default @NotNull String asHexString() {
return String.format("#%06x", this.value());
return String.format("%c%06x", HEX_CHARACTER, this.value());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
@SuppressWarnings("UnstableApiUsage") // TypeToken
final class TextColorSerializer extends ScalarSerializer<TextColor> {
static final TextColorSerializer INSTANCE = new TextColorSerializer();
private static final String HEX_PREFIX = "#";

private TextColorSerializer() {
super(TextColor.class);
Expand All @@ -49,7 +48,7 @@ public TextColor deserialize(final @NotNull TypeToken<?> type, final @NotNull Ob
}
final String value = obj.toString();
final TextColor result;
if (value.startsWith(HEX_PREFIX)) {
if (value.startsWith(TextColor.HEX_PREFIX)) {
result = TextColor.fromHexString(value);
} else {
result = NamedTextColor.NAMES.value(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

final class TextColorSerializer extends ScalarSerializer<TextColor> {
static final TextColorSerializer INSTANCE = new TextColorSerializer();
private static final String HEX_PREFIX = "#";

private TextColorSerializer() {
super(TextColor.class);
Expand All @@ -48,7 +47,7 @@ public TextColor deserialize(final @NotNull Type type, final @NotNull Object obj
}
final String value = obj.toString();
final TextColor result;
if (value.startsWith(HEX_PREFIX)) {
if (value.startsWith(TextColor.HEX_PREFIX)) {
result = TextColor.fromHexString(value);
} else {
result = NamedTextColor.NAMES.value(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
* @since 4.10.0
*/
final class ColorTagResolver implements TagResolver, SerializableResolver.Single {
private static final char HEX = '#';
private static final String COLOR_3 = "c";
private static final String COLOR_2 = "colour";
private static final String COLOR = "color";
Expand Down Expand Up @@ -95,7 +94,7 @@ private static boolean isColorOrAbbreviation(final String name) {
final TextColor color;
if (COLOR_ALIASES.containsKey(colorName)) {
color = COLOR_ALIASES.get(colorName);
} else if (colorName.charAt(0) == HEX) {
} else if (colorName.charAt(0) == TextColor.HEX_CHARACTER) {
color = TextColor.fromHexString(colorName);
} else {
color = NamedTextColor.NAMES.value(colorName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static Tag create(final ArgumentQueue args, final Context ctx) {

final String argValue = arg.value();
final TextColor parsedColor;
if (argValue.charAt(0) == '#') {
if (argValue.charAt(0) == TextColor.HEX_CHARACTER) {
parsedColor = TextColor.fromHexString(argValue);
} else {
parsedColor = NamedTextColor.NAMES.value(arg.lowerValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void write(final JsonWriter out, final TextColor value) throws IOExceptio
}

private static String asUpperCaseHexString(final TextColor color) {
return String.format(Locale.ROOT, "#%06X", color.value()); // to be consistent with vanilla
return String.format(Locale.ROOT, "%c%06X", TextColor.HEX_CHARACTER, color.value()); // to be consistent with vanilla
}

@Override
Expand All @@ -67,7 +67,7 @@ private static String asUpperCaseHexString(final TextColor color) {
}

static @Nullable TextColor fromString(final @NotNull String value) {
if (value.startsWith("#")) {
if (value.startsWith(TextColor.HEX_PREFIX)) {
return TextColor.fromHexString(value);
} else {
return NamedTextColor.NAMES.value(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.util.Buildable;
import net.kyori.adventure.util.PlatformAPI;
Expand Down Expand Up @@ -135,7 +136,7 @@ public interface LegacyComponentSerializer extends ComponentSerializer<Component
*
* @since 4.0.0
*/
char HEX_CHAR = '#';
char HEX_CHAR = TextColor.HEX_CHARACTER;

/**
* Deserialize a component from a legacy {@link String}.
Expand Down