-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.asintoto.colorlib</groupId> | ||
<artifactId>ColorLib</artifactId> | ||
<version>1.0.0</version> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<exec.mainClass>com.asintoto.colorlib.ColorLib</exec.mainClass> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spigot-repo</id> | ||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> | ||
</repository> | ||
<repository> | ||
<id>placeholderapi</id> | ||
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot-api</artifactId> | ||
<version>1.20.1-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>me.clip</groupId> | ||
<artifactId>placeholderapi</artifactId> | ||
<version>2.11.5</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.12.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
|
||
package com.asintoto.colorlib; | ||
|
||
import com.asintoto.colorlib.rgb.RGBColor; | ||
import me.clip.placeholderapi.PlaceholderAPI; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
import org.bukkit.entity.Player; | ||
|
||
/** | ||
* | ||
* @author Asintoto | ||
*/ | ||
public class ColorLib { | ||
|
||
public static String setColors(String text) { | ||
return RGBColor.process(text); | ||
} | ||
|
||
public static String setColorsAndPlaceholders(Player p, String text) { | ||
String processed = PlaceholderAPI.setPlaceholders(p, text); | ||
return setColors(processed); | ||
} | ||
|
||
public static String setColorsAndPlaceholders(OfflinePlayer p, String text) { | ||
String processed = PlaceholderAPI.setPlaceholders(p, text); | ||
return setColors(processed); | ||
} | ||
|
||
public static String getColoredStringFromConfig(YamlConfiguration config, String path) { | ||
String text = config.getString(path); | ||
return setColors(text); | ||
} | ||
|
||
public static String getColoredStringFromConfigWithPlaceholders(YamlConfiguration config, Player p, String path) { | ||
String text = config.getString(path); | ||
return setColorsAndPlaceholders(p, text); | ||
} | ||
|
||
public static String getColoredStringFromConfigWithPlaceholders(YamlConfiguration config, OfflinePlayer p, String path) { | ||
String text = config.getString(path); | ||
return setColorsAndPlaceholders(p, text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
|
||
package com.asintoto.colorlib.rgb; | ||
|
||
import com.asintoto.colorlib.rgb.patterns.Gradient; | ||
import com.asintoto.colorlib.rgb.patterns.Pattern; | ||
import com.asintoto.colorlib.rgb.patterns.Rainbow; | ||
import com.asintoto.colorlib.rgb.patterns.Solid; | ||
import com.google.common.collect.ImmutableMap; | ||
|
||
import java.awt.*; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import net.md_5.bungee.api.ChatColor; | ||
import org.apache.commons.lang3.Validate; | ||
import org.bukkit.Bukkit; | ||
|
||
/** | ||
* | ||
* @author Asintoto | ||
*/ | ||
public class RGBColor { | ||
private static final int VERSION = getVersion(); | ||
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 Map<Color, ChatColor> COLORS = ImmutableMap.<Color, ChatColor>builder() | ||
.put(new Color(0), ChatColor.getByChar('0')) | ||
.put(new Color(170), ChatColor.getByChar('1')) | ||
.put(new Color(43520), ChatColor.getByChar('2')) | ||
.put(new Color(43690), ChatColor.getByChar('3')) | ||
.put(new Color(11141120), ChatColor.getByChar('4')) | ||
.put(new Color(11141290), ChatColor.getByChar('5')) | ||
.put(new Color(16755200), ChatColor.getByChar('6')) | ||
.put(new Color(11184810), ChatColor.getByChar('7')) | ||
.put(new Color(5592405), ChatColor.getByChar('8')) | ||
.put(new Color(5592575), ChatColor.getByChar('9')) | ||
.put(new Color(5635925), ChatColor.getByChar('a')) | ||
.put(new Color(5636095), ChatColor.getByChar('b')) | ||
.put(new Color(16733525), ChatColor.getByChar('c')) | ||
.put(new Color(16733695), ChatColor.getByChar('d')) | ||
.put(new Color(16777045), ChatColor.getByChar('e')) | ||
.put(new Color(16777215), ChatColor.getByChar('f')).build(); | ||
|
||
private static final List<Pattern> PATTERNS = Arrays.asList(new Gradient(), new Solid(), new Rainbow()); | ||
|
||
public RGBColor() { | ||
} | ||
|
||
public static String process(String string) { | ||
for (Pattern pattern : PATTERNS) { | ||
string = pattern.process(string); | ||
} | ||
string = ChatColor.translateAlternateColorCodes('&', string); | ||
return string; | ||
} | ||
|
||
public static List<String> process(Collection<String> strings) { | ||
return strings.stream() | ||
.map(RGBColor::process) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static String color(String string, Color color) { | ||
return (SUPPORTS_RGB ? ChatColor.of(color) : getClosestColor(color)) + string; | ||
} | ||
|
||
|
||
public static String color(String string, Color start, Color end) { | ||
String original = string; | ||
|
||
ChatColor[] colors = createGradient(start, end, withoutSpecialChar(string).length()); | ||
return apply(original, colors); | ||
} | ||
|
||
|
||
public static String rainbow( String string, float saturation) { | ||
String original = string; | ||
|
||
ChatColor[] colors = createRainbow(withoutSpecialChar(string).length(), saturation); | ||
return apply(original, colors); | ||
} | ||
|
||
|
||
public static ChatColor getColor( String string) { | ||
return SUPPORTS_RGB ? ChatColor.of(new Color(Integer.parseInt(string, 16))) | ||
: getClosestColor(new Color(Integer.parseInt(string, 16))); | ||
} | ||
|
||
|
||
public static String stripColorFormatting( String string) { | ||
return string.replaceAll("<#[0-9A-F]{6}>|[&§][a-f0-9lnokm]|<[/]?[A-Z]{5,8}(:[0-9A-F]{6})?[0-9]*>", ""); | ||
} | ||
|
||
|
||
private static String apply( 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(); | ||
} | ||
|
||
|
||
private static String withoutSpecialChar( String source) { | ||
String workingString = source; | ||
for (String color : SPECIAL_COLORS) { | ||
if (workingString.contains(color)) { | ||
workingString = workingString.replace(color, ""); | ||
} | ||
} | ||
return workingString; | ||
} | ||
|
||
|
||
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) { | ||
colors[i] = ChatColor.of(color); | ||
} else { | ||
colors[i] = getClosestColor(color); | ||
} | ||
} | ||
return colors; | ||
} | ||
|
||
|
||
private static ChatColor[] createGradient( Color start, Color end, int step) { | ||
ChatColor[] colors = new ChatColor[step]; | ||
int stepR = Math.abs(start.getRed() - end.getRed()) / (step - 1); | ||
int stepG = Math.abs(start.getGreen() - end.getGreen()) / (step - 1); | ||
int stepB = Math.abs(start.getBlue() - end.getBlue()) / (step - 1); | ||
int[] direction = new int[] { | ||
start.getRed() < end.getRed() ? +1 : -1, | ||
start.getGreen() < end.getGreen() ? +1 : -1, | ||
start.getBlue() < end.getBlue() ? +1 : -1, | ||
}; | ||
|
||
for (int i=0; i<step; i++) { | ||
Color color = new Color(start.getRed() + ((stepR * i) * direction[0]), start.getGreen() + ((stepG * i) * direction[1]), start.getBlue() + ((stepB * i) * direction[2])); | ||
if (SUPPORTS_RGB) { | ||
colors[i] = ChatColor.of(color); | ||
} else { | ||
colors[i] = getClosestColor(color); | ||
} | ||
} | ||
|
||
return colors; | ||
} | ||
|
||
|
||
private static ChatColor getClosestColor(Color color) { | ||
Color nearestColor = null; | ||
double nearestDistance = Integer.MAX_VALUE; | ||
|
||
for (Color constantColor : COLORS.keySet()) { | ||
double distance = Math.pow(color.getRed() - constantColor.getRed(), 2) + Math.pow(color.getGreen() - constantColor.getGreen(), 2) + Math.pow(color.getBlue() - constantColor.getBlue(), 2); | ||
if (nearestDistance > distance) { | ||
nearestColor = constantColor; | ||
nearestDistance = distance; | ||
} | ||
} | ||
|
||
return COLORS.get(nearestColor); | ||
} | ||
|
||
private static int getVersion() { | ||
String version = Bukkit.getVersion(); | ||
Validate.notEmpty(version, "Can't get Minecraft Version from an empty or not valid string!"); | ||
|
||
int index = version.lastIndexOf("MC:"); | ||
if (index != -1) { | ||
version = version.substring(index + 4, version.length() -1); | ||
} else if (version.endsWith("SNAPSHOT")) { | ||
index = version.indexOf('-'); | ||
version = version.substring(0, index); | ||
} | ||
|
||
int lastDot = version.lastIndexOf('.'); | ||
if (version.indexOf('.') != lastDot) version = version.substring(0, lastDot); | ||
|
||
return Integer.parseInt(version.substring(2)); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/asintoto/colorlib/rgb/patterns/Gradient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
package com.asintoto.colorlib.rgb.patterns; | ||
|
||
import com.asintoto.colorlib.rgb.RGBColor; | ||
import java.awt.Color; | ||
import java.util.regex.Matcher; | ||
|
||
/** | ||
* | ||
* @author Asintoto | ||
*/ | ||
public class Gradient implements Pattern{ | ||
java.util.regex.Pattern patt = java.util.regex.Pattern.compile("<GRADIENT:([0-9A-Fa-f]{6})>(.*?)</GRADIENT:([0-9A-Fa-f]{6})>"); | ||
|
||
@Override | ||
public String process(String string) { | ||
Matcher match = patt.matcher(string); | ||
while (match.find()) { | ||
String start = match.group(1); | ||
String end = match.group(3); | ||
String content = match.group(2); | ||
string = string.replace(match.group(), RGBColor.color(content, new Color(Integer.parseInt(start, 16)), new Color(Integer.parseInt(end, 16)))); | ||
} | ||
return string; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/asintoto/colorlib/rgb/patterns/Pattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
package com.asintoto.colorlib.rgb.patterns; | ||
|
||
/** | ||
* | ||
* @author Asintoto | ||
*/ | ||
public interface Pattern { | ||
String process(String string); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/asintoto/colorlib/rgb/patterns/Rainbow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
package com.asintoto.colorlib.rgb.patterns; | ||
|
||
import com.asintoto.colorlib.rgb.RGBColor; | ||
import java.util.regex.Matcher; | ||
|
||
/** | ||
* | ||
* @author Asintoto | ||
*/ | ||
public class Rainbow implements Pattern{ | ||
java.util.regex.Pattern patt = java.util.regex.Pattern.compile("<RAINBOW:([0-9]{1,3})>(.*?)</RAINBOW>"); | ||
|
||
@Override | ||
public String process(String string) { | ||
Matcher match = patt.matcher(string); | ||
while (match.find()) { | ||
String saturation = match.group(1); | ||
String content = match.group(2); | ||
string = string.replace(match.group(), RGBColor.rainbow(content, Float.parseFloat(saturation))); | ||
} | ||
return string; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/asintoto/colorlib/rgb/patterns/Solid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
package com.asintoto.colorlib.rgb.patterns; | ||
|
||
import com.asintoto.colorlib.rgb.RGBColor; | ||
import java.util.regex.Matcher; | ||
|
||
/** | ||
* | ||
* @author Asintoto | ||
*/ | ||
public class Solid implements Pattern{ | ||
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("<SOLID:([0-9A-Fa-f]{6})>|#\\{([0-9A-Fa-f]{6})}"); | ||
|
||
public String process(String string) { | ||
Matcher matcher = pattern.matcher(string); | ||
while (matcher.find()) { | ||
String color = matcher.group(1); | ||
if (color == null) color = matcher.group(2); | ||
|
||
string = string.replace(matcher.group(), RGBColor.getColor(color) + ""); | ||
} | ||
return string; | ||
} | ||
} |