|
| 1 | +package org.foresttech.colorAPI.api; |
| 2 | + |
| 3 | +import net.md_5.bungee.api.ChatColor; |
| 4 | +import org.foresttech.colorAPI.enums.ColorizeType; |
| 5 | + |
| 6 | +import java.awt.*; |
| 7 | +import java.util.Arrays; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +public class ColorAPI { |
| 14 | + |
| 15 | + private static final List<String> legacyColors = Arrays.asList("&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "§0", "§1", "§2", "§3", "§4", "§5", "§6", "§7", "§8", "§9", "§a", "§b", "§c", "§d", "§e"); |
| 16 | + private static final List<String> specialChars = Arrays.asList("&l", "&n", "&o", "&k", "&m", "§l", "§n", "§o", "§k", "§m"); |
| 17 | + private static final Pattern patternNormal = Pattern.compile("\\{#([0-9A-Fa-f]{6})\\}"); |
| 18 | + private static final Pattern patternGrad = Pattern.compile("\\{/#([0-9A-Fa-f]{6})\\}(.*?)\\{/#([0-9A-Fa-f]{6})\\}"); |
| 19 | + private static Matcher matcher; |
| 20 | + |
| 21 | + /** |
| 22 | + * |
| 23 | + * Here we can select the type of colorize, |
| 24 | + * soo if we only want RGB we do ColorizeType.RGB into input |
| 25 | + * |
| 26 | + * @param input message |
| 27 | + * @param type ColorizeType enum, for better selecting type |
| 28 | + * @return colored output, or null type |
| 29 | + */ |
| 30 | + public static String colorizeType(ColorizeType type, String input) { |
| 31 | + return switch (type) { |
| 32 | + case GRADIENT -> colorizeGradient(input); |
| 33 | + case RGB -> colorizeRGB(input); |
| 34 | + case CLASSIC -> colorizeClassic(input); |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * |
| 40 | + * This method clear whole string |
| 41 | + * |
| 42 | + * @return string without patterns, legacy colors, and special chars |
| 43 | + */ |
| 44 | + public static String clear(String input) { |
| 45 | + input = removePatterns(input); |
| 46 | + input = removeLegacyColors(input); |
| 47 | + input = removeSpecialChars(input); |
| 48 | + return input; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * |
| 53 | + * In this method we remove specialChars like "&n", "&r"... |
| 54 | + * Example: |
| 55 | + * "&kForestTech ❤" -> "ForestTech ❤" |
| 56 | + * |
| 57 | + * @param input message |
| 58 | + * @return output |
| 59 | + */ |
| 60 | + public static String removeSpecialChars(String input) { |
| 61 | + for (String chars : specialChars) { |
| 62 | + if (!input.contains(chars)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + input = input.replaceAll(chars, ""); |
| 66 | + } |
| 67 | + |
| 68 | + return input; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * |
| 73 | + * In this method we remove legacyColors like "&2", "&c"... |
| 74 | + * Example: |
| 75 | + * "&2ForestTech ❤" -> "ForestTech ❤" |
| 76 | + * |
| 77 | + * @param input message |
| 78 | + * @return output |
| 79 | + */ |
| 80 | + public static String removeLegacyColors(String input) { |
| 81 | + for (String color : legacyColors) { |
| 82 | + if (!input.contains(color)) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + input = input.replaceAll(color, ""); |
| 86 | + } |
| 87 | + return input; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * |
| 92 | + * If we want to remove patterns from message we can use this |
| 93 | + * Example: |
| 94 | + * "{#00e64e}ForestTech ❤" -> "ForestTech ❤" |
| 95 | + * |
| 96 | + * @param input message with patterns |
| 97 | + * @return string without patterns |
| 98 | + */ |
| 99 | + public static String removePatterns(String input) { |
| 100 | + input = input.replaceAll("\\{/#([0-9A-Fa-f]{6})\\}", ""); |
| 101 | + input = input.replaceAll("\\{#([0-9A-Fa-f]{6})\\}", ""); |
| 102 | + return input; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * |
| 107 | + * Universal colorize method for add colors into message |
| 108 | + * |
| 109 | + * @param input message |
| 110 | + * @return colored message |
| 111 | + */ |
| 112 | + public static String colorize(String input) { |
| 113 | + input = colorizeGradient(input); |
| 114 | + input = colorizeRGB(input); |
| 115 | + return input; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * |
| 120 | + * Method for normal translate colors from spigot |
| 121 | + * |
| 122 | + */ |
| 123 | + public static String colorizeClassic(String input) { |
| 124 | + input = ChatColor.translateAlternateColorCodes('&', input); |
| 125 | + return input; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + /** |
| 130 | + * |
| 131 | + * Here we can call for gradient colorize |
| 132 | + * |
| 133 | + * Group 1 = First gradient |
| 134 | + * Group 3 = Second gradient |
| 135 | + * Group 2 = content |
| 136 | + * |
| 137 | + * @param input message |
| 138 | + * @return colored output with gradient |
| 139 | + */ |
| 140 | + public static String colorizeGradient(String input) { |
| 141 | + matcher = patternGrad.matcher(input); |
| 142 | + while (matcher.find()) { |
| 143 | + input = input.replace(matcher.group(), color(matcher.group(2), new Color(Integer.parseInt(matcher.group(1), 16)), new Color(Integer.parseInt(matcher.group(3), 16)))); |
| 144 | + } |
| 145 | + return ChatColor.translateAlternateColorCodes('&', input); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * |
| 150 | + * This method only do normal RGB without gradient |
| 151 | + * |
| 152 | + * @param input message |
| 153 | + * @return colored rgb output |
| 154 | + */ |
| 155 | + public static String colorizeRGB(String input) { |
| 156 | + matcher = patternNormal.matcher(input); |
| 157 | + String color; |
| 158 | + while (matcher.find()) { |
| 159 | + color = matcher.group(1); |
| 160 | + if (color == null) { |
| 161 | + color = matcher.group(2); |
| 162 | + } |
| 163 | + input = input.replace(matcher.group(), getColor(color) + ""); |
| 164 | + } |
| 165 | + return input; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * |
| 170 | + * Color method for gradient for example this take |
| 171 | + * the first one gradient, and second one, and do gradient in the message |
| 172 | + * |
| 173 | + * @param input whole message |
| 174 | + * @param first first gradient |
| 175 | + * @param second second gradient |
| 176 | + */ |
| 177 | + public static String color(String input, Color first, Color second) { |
| 178 | + ChatColor[] colors = createGradient(first, second, removeSpecialChars(input).length()); |
| 179 | + return apply(input, colors); |
| 180 | + } |
| 181 | + |
| 182 | + private static String apply(String input, ChatColor[] colors) { |
| 183 | + StringBuilder specialColors = new StringBuilder(); |
| 184 | + StringBuilder stringBuilder = new StringBuilder(); |
| 185 | + String[] characters = input.split(""); |
| 186 | + int outIndex = 0; |
| 187 | + |
| 188 | + for (int i = 0; i < characters.length; i++) { |
| 189 | + if (!characters[i].equals("&") && !characters[i].equals("§")) { |
| 190 | + stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]); |
| 191 | + continue; |
| 192 | + } |
| 193 | + if (i + 1 >= characters.length) { |
| 194 | + stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]); |
| 195 | + continue; |
| 196 | + } |
| 197 | + if (characters[i + 1].equals("r")) { |
| 198 | + specialColors.setLength(0); |
| 199 | + } else { |
| 200 | + specialColors.append(characters[i]); |
| 201 | + specialColors.append(characters[i + 1]); |
| 202 | + } |
| 203 | + i++; |
| 204 | + } |
| 205 | + return stringBuilder.toString(); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * |
| 210 | + * @return colors for string |
| 211 | + * |
| 212 | + */ |
| 213 | + private static ChatColor[] createGradient(Color first, Color second, int amount) { |
| 214 | + ChatColor[] colors = new ChatColor[amount]; |
| 215 | + int amountR = Math.abs(first.getRed() - second.getRed()) / (amount - 1); |
| 216 | + int amountG = Math.abs(first.getGreen() - second.getGreen()) / (amount - 1); |
| 217 | + int amountB = Math.abs(first.getBlue() - second.getBlue()) / (amount - 1); |
| 218 | + int[] colorDir = new int[]{first.getRed() < second.getRed() ? +1 : -1, first.getGreen() < second.getGreen() ? +1 : -1, first.getBlue() < second.getBlue() ? +1 : -1}; |
| 219 | + |
| 220 | + for (int i = 0; i < amount; i++) { |
| 221 | + Color color = new Color(first.getRed() + ((amountR * i) * colorDir[0]), first.getGreen() + ((amountG * i) * colorDir[1]), first.getBlue() + ((amountB * i) * colorDir[2])); |
| 222 | + colors[i] = ChatColor.of(color); |
| 223 | + } |
| 224 | + return colors; |
| 225 | + } |
| 226 | + |
| 227 | + public static ChatColor getColor(String matcher) { |
| 228 | + return ChatColor.of(new Color(Integer.parseInt(matcher, 16))); |
| 229 | + } |
| 230 | + |
| 231 | +} |
0 commit comments