Skip to content
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
101 changes: 57 additions & 44 deletions src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class IridiumColorAPI {

/**
* The current version of the server in the a form of a major version.
* If the static initialization for this fails, you know something's wrong with the server software.
* If the static initialization for this fails, you know something's wrong with
* the server software.
*
* @since 1.0.0
*/
Expand All @@ -34,7 +35,7 @@ public class IridiumColorAPI {
*/
private static final boolean SUPPORTS_RGB = VERSION >= 16;

private static final List<String> SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m","§l", "§n", "§o", "§k", "§m");
private static final List<String> SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m", "§l", "§n", "§o", "§k", "§m");

/**
* Cached result of all legacy colors.
Expand Down Expand Up @@ -78,7 +79,7 @@ public static String process(@Nonnull String string) {
for (Pattern pattern : PATTERNS) {
string = pattern.process(string);
}

string = ChatColor.translateAlternateColorCodes('&', string);
return string;
}
Expand All @@ -93,8 +94,8 @@ public static String process(@Nonnull String string) {
@Nonnull
public static List<String> process(@Nonnull List<String> strings) {
return strings.stream()
.map(IridiumColorAPI::process)
.collect(Collectors.toList());
.map(IridiumColorAPI::process)
.collect(Collectors.toList());
}

/**
Expand All @@ -119,22 +120,10 @@ public static String color(@Nonnull String string, @Nonnull Color color) {
*/
@Nonnull
public static String color(@Nonnull String string, @Nonnull Color start, @Nonnull Color end) {
StringBuilder specialColors = new StringBuilder();
for (String color : SPECIAL_COLORS) {
if (string.contains(color)) {
specialColors.append(color);
string = string.replace(color, "");
}
}
String originalString = string;

StringBuilder stringBuilder = new StringBuilder();
ChatColor[] colors = createGradient(start, end, string.length());
String[] characters = string.split("");
for (int i = 0; i < string.length(); i++) {
stringBuilder.append(colors[i]).append(specialColors).append(characters[i]);
}

return stringBuilder.toString();
ChatColor[] colors = createGradient(start, end, withoutSpecialChar(string).length());
return apply(originalString, colors);
}

/**
Expand All @@ -146,22 +135,10 @@ public static String color(@Nonnull String string, @Nonnull Color start, @Nonnul
*/
@Nonnull
public static String rainbow(@Nonnull String string, float saturation) {
StringBuilder specialColors = new StringBuilder();
for (String color : SPECIAL_COLORS) {
if (string.contains(color)) {
specialColors.append(color);
string = string.replace(color, "");
}
}

StringBuilder stringBuilder = new StringBuilder();
ChatColor[] colors = createRainbow(string.length(), saturation);
String[] characters = string.split("");
for (int i = 0; i < string.length(); i++) {
stringBuilder.append(colors[i]).append(specialColors).append(characters[i]);
}

return stringBuilder.toString();
String originalString = string;

ChatColor[] colors = createRainbow(withoutSpecialChar(string).length(), saturation);
return apply(originalString, colors);
}

/**
Expand All @@ -172,21 +149,58 @@ public static String rainbow(@Nonnull String string, float saturation) {
*/
@Nonnull
public static ChatColor getColor(@Nonnull String string) {
return SUPPORTS_RGB ? ChatColor.of(new Color(Integer.parseInt(string, 16))) : getClosestColor(new Color(Integer.parseInt(string, 16)));
return SUPPORTS_RGB ? ChatColor.of(new Color(Integer.parseInt(string, 16)))
: getClosestColor(new Color(Integer.parseInt(string, 16)));
}

/**
* Removes all color codes from the provided String, including IridiumColorAPI patterns.
* Removes all color codes from the provided String, including IridiumColorAPI
* patterns.
*
* @param string The String which should be stripped
* @return The stripped string without color codes
* @param string The String which should be stripped
* @return The stripped string without color codes
* @since 1.0.5
*/
@Nonnull
public static String stripColorFormatting(@Nonnull String string) {
return string.replaceAll("<#[0-9A-F]{6}>|[&§][a-f0-9lnokm]|<[/]?[A-Z]{5,8}(:[0-9A-F]{6})?[0-9]*>", "");
}

@Nonnull
private static String apply(@Nonnull String source, ChatColor[] colors) {
StringBuilder specialColors = new StringBuilder();
StringBuilder stringBuilder = new StringBuilder();
String[] characters = source.split("");
int outIndex = 0;
for (int i = 0; i < characters.length; i++) {
if (characters[i].equals("&") || characters[i].equals("§")) {
if (i + 1 < characters.length) {
if (characters[i + 1].equals("r")) {
specialColors.setLength(0);
} else {
specialColors.append(characters[i]);
specialColors.append(characters[i + 1]);
}
i++;
} else
stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]);
} else
stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]);
}
return stringBuilder.toString();
}

@Nonnull
private static String withoutSpecialChar(@Nonnull String source) {
String workingString = source;
for (String color : SPECIAL_COLORS) {
if (workingString.contains(color)) {
workingString = workingString.replace(color, "");
}
}
return workingString;
}

/**
* Returns a rainbow array of chat colors.
*
Expand All @@ -199,7 +213,7 @@ public static String stripColorFormatting(@Nonnull String string) {
private static ChatColor[] createRainbow(int step, float saturation) {
ChatColor[] colors = new ChatColor[step];
double colorStep = (1.00 / step);

for (int i = 0; i < step; i++) {
Color color = Color.getHSBColor((float) (colorStep * i), saturation, saturation);
if (SUPPORTS_RGB) {
Expand All @@ -208,7 +222,7 @@ private static ChatColor[] createRainbow(int step, float saturation) {
colors[i] = getClosestColor(color);
}
}

return colors;
}

Expand Down Expand Up @@ -241,11 +255,10 @@ private static ChatColor[] createGradient(@Nonnull Color start, @Nonnull Color e
colors[i] = getClosestColor(color);
}
}

return colors;
}


/**
* Returns the closest legacy color from an rgb color
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ static void setup() {

@Test
void process() {

assertEquals("§x§1§2§3§4§5§6Test", IridiumColorAPI.process("<SOLID:123456>Test"));
assertEquals("§x§1§2§3§4§5§6Te§kst", IridiumColorAPI.process("<SOLID:123456>Te&kst"));
assertEquals("§x§2§c§0§8§b§a§lT§x§1§e§3§3§a§f§le§x§1§0§5§e§a§4§ls§x§0§2§8§9§9§9§lt", IridiumColorAPI.process("<GRADIENT:2C08BA>&lTest</GRADIENT:028A97>"));
assertEquals("§x§2§c§0§8§b§a§lT§x§1§e§3§3§a§f§le§x§1§0§5§e§a§4§ls§x§0§2§8§9§9§9§l§kt", IridiumColorAPI.process("<GRADIENT:2C08BA>&lTes§kt</GRADIENT:028A97>"));
assertEquals("§x§f§f§0§0§0§0§lT§x§8§0§f§f§0§0§le§x§0§0§f§f§f§f§ls§x§8§0§0§0§f§f§lt", IridiumColorAPI.process("<RAINBOW1>&lTest</RAINBOW>"));
assertEquals("§x§f§f§0§0§0§0§lT§x§8§0§f§f§0§0§le§x§0§0§f§f§f§f§ls§x§8§0§0§0§f§f§l§kt", IridiumColorAPI.process("<RAINBOW1>&lTes&kt</RAINBOW>"));
assertEquals("§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*", IridiumColorAPI.process("<GRADIENT:123456>******************************************************************************************************************************************************************************************************</GRADIENT:ABCDEF>"));
}

Expand Down