Skip to content

Commit f48c1d2

Browse files
committed
Project Uploaded
0 parents  commit f48c1d2

File tree

11 files changed

+633
-0
lines changed

11 files changed

+633
-0
lines changed

.gitignore

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# User-specific stuff
2+
.idea/
3+
4+
*.iml
5+
*.ipr
6+
*.iws
7+
8+
# IntelliJ
9+
out/
10+
11+
# Compiled class file
12+
*.class
13+
14+
# Log file
15+
*.log
16+
17+
# BlueJ files
18+
*.ctxt
19+
20+
# Package Files #
21+
*.jar
22+
*.war
23+
*.nar
24+
*.ear
25+
*.zip
26+
*.tar.gz
27+
*.rar
28+
29+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
30+
hs_err_pid*
31+
32+
*~
33+
34+
# temporary files which can be created if a process still has a handle open of a deleted file
35+
.fuse_hidden*
36+
37+
# KDE directory preferences
38+
.directory
39+
40+
# Linux trash folder which might appear on any partition or disk
41+
.Trash-*
42+
43+
# .nfs files are created when an open file is removed but is still being accessed
44+
.nfs*
45+
46+
# General
47+
.DS_Store
48+
.AppleDouble
49+
.LSOverride
50+
51+
# Icon must end with two \r
52+
Icon
53+
54+
# Thumbnails
55+
._*
56+
57+
# Files that might appear in the root of a volume
58+
.DocumentRevisions-V100
59+
.fseventsd
60+
.Spotlight-V100
61+
.TemporaryItems
62+
.Trashes
63+
.VolumeIcon.icns
64+
.com.apple.timemachine.donotpresent
65+
66+
# Directories potentially created on remote AFP share
67+
.AppleDB
68+
.AppleDesktop
69+
Network Trash Folder
70+
Temporary Items
71+
.apdisk
72+
73+
# Windows thumbnail cache files
74+
Thumbs.db
75+
Thumbs.db:encryptable
76+
ehthumbs.db
77+
ehthumbs_vista.db
78+
79+
# Dump file
80+
*.stackdump
81+
82+
# Folder config file
83+
[Dd]esktop.ini
84+
85+
# Recycle Bin used on file shares
86+
$RECYCLE.BIN/
87+
88+
# Windows Installer files
89+
*.cab
90+
*.msi
91+
*.msix
92+
*.msm
93+
*.msp
94+
95+
# Windows shortcuts
96+
*.lnk
97+
98+
target/
99+
100+
pom.xml.tag
101+
pom.xml.releaseBackup
102+
pom.xml.versionsBackup
103+
pom.xml.next
104+
105+
release.properties
106+
dependency-reduced-pom.xml
107+
buildNumber.properties
108+
.mvn/timing.properties
109+
.mvn/wrapper/maven-wrapper.jar
110+
.flattened-pom.xml
111+
112+
# Common working directory
113+
run/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="MatrixColorAPI [clean,package]" type="MavenRunConfiguration" factoryName="Maven" nameIsGenerated="true">
3+
<MavenSettings>
4+
<option name="myGeneralSettings" />
5+
<option name="myRunnerSettings" />
6+
<option name="myRunnerParameters">
7+
<MavenRunnerParameters>
8+
<option name="cmdOptions" />
9+
<option name="profiles">
10+
<set />
11+
</option>
12+
<option name="goals">
13+
<list>
14+
<option value="clean" />
15+
<option value="package" />
16+
</list>
17+
</option>
18+
<option name="multimoduleDir" />
19+
<option name="pomFileName" />
20+
<option name="profilesMap">
21+
<map />
22+
</option>
23+
<option name="projectsCmdOptionValues">
24+
<list />
25+
</option>
26+
<option name="resolveToWorkspace" value="false" />
27+
<option name="workingDirPath" value="$PROJECT_DIR$" />
28+
</MavenRunnerParameters>
29+
</option>
30+
</MavenSettings>
31+
<method v="2" />
32+
</configuration>
33+
</component>

pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.matrixcreations</groupId>
8+
<artifactId>MatrixColorAPI</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>jar</packaging>
11+
12+
<name>MatrixColorAPI</name>
13+
14+
<properties>
15+
<java.version>16</java.version>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<build>
20+
<defaultGoal>clean package</defaultGoal>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<version>3.13.0</version>
26+
<configuration>
27+
<source>${java.version}</source>
28+
<target>${java.version}</target>
29+
</configuration>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-shade-plugin</artifactId>
34+
<version>3.5.3</version>
35+
<executions>
36+
<execution>
37+
<phase>package</phase>
38+
<goals>
39+
<goal>shade</goal>
40+
</goals>
41+
</execution>
42+
</executions>
43+
</plugin>
44+
</plugins>
45+
<resources>
46+
<resource>
47+
<directory>src/main/resources</directory>
48+
<filtering>true</filtering>
49+
</resource>
50+
</resources>
51+
</build>
52+
53+
<repositories>
54+
<repository>
55+
<id>spigotmc-repo</id>
56+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
57+
</repository>
58+
<repository>
59+
<id>sonatype</id>
60+
<url>https://oss.sonatype.org/content/groups/public/</url>
61+
</repository>
62+
</repositories>
63+
64+
<dependencies>
65+
<dependency>
66+
<groupId>net.kyori</groupId>
67+
<artifactId>adventure-api</artifactId>
68+
<version>4.17.0</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>net.kyori</groupId>
72+
<artifactId>adventure-text-serializer-legacy</artifactId>
73+
<version>4.17.0</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.projectlombok</groupId>
77+
<artifactId>lombok</artifactId>
78+
<version>1.18.26</version>
79+
<scope>provided</scope>
80+
</dependency>
81+
</dependencies>
82+
</project>
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package net.matrixcreations.libraries;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.TextComponent;
5+
import net.kyori.adventure.text.format.TextColor;
6+
import net.kyori.adventure.text.format.TextDecoration;
7+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
8+
9+
import java.util.List;
10+
import java.util.regex.Matcher;
11+
import java.util.regex.Pattern;
12+
import java.util.stream.Collectors;
13+
14+
public class ColorUtils {
15+
16+
private static final Pattern HEX_PATTERN = Pattern.compile("&#([A-Fa-f0-9]{6})");
17+
private static final Pattern GRADIENT_PATTERN = Pattern.compile("<GRADIENT:#([A-Fa-f0-9]{6})>(.*?)</GRADIENT:#([A-Fa-f0-9]{6})>");
18+
private static final Pattern SOLID_PATTERN = Pattern.compile("<SOLID:#([A-Fa-f0-9]{6})>(.*?)(?:</SOLID>|$)");
19+
private static final Pattern LEGACY_PATTERN = Pattern.compile("&([0-9a-fk-or])");
20+
21+
public static String process(String text) {
22+
text = processGradients(text);
23+
text = processSolidColors(text);
24+
text = processHexColors(text);
25+
return processLegacyCodes(text);
26+
}
27+
28+
public static List<String> process(List<String> texts) {
29+
return texts.stream().map(ColorUtils::process).collect(Collectors.toList());
30+
}
31+
32+
private static String processGradients(String text) {
33+
Matcher matcher = GRADIENT_PATTERN.matcher(text);
34+
StringBuffer buffer = new StringBuffer();
35+
36+
while (matcher.find()) {
37+
String startColor = matcher.group(1);
38+
String content = matcher.group(2);
39+
String endColor = matcher.group(3);
40+
41+
String gradientText = applyGradient(content, startColor, endColor);
42+
matcher.appendReplacement(buffer, Matcher.quoteReplacement(gradientText));
43+
}
44+
45+
matcher.appendTail(buffer);
46+
return buffer.toString();
47+
}
48+
49+
private static String processSolidColors(String text) {
50+
Matcher matcher = SOLID_PATTERN.matcher(text);
51+
StringBuffer buffer = new StringBuffer();
52+
53+
while (matcher.find()) {
54+
String hexColor = matcher.group(1);
55+
String content = matcher.group(2);
56+
57+
String solidColorText = applySolidColor(content, hexColor);
58+
matcher.appendReplacement(buffer, Matcher.quoteReplacement(solidColorText));
59+
}
60+
61+
matcher.appendTail(buffer);
62+
return buffer.toString();
63+
}
64+
65+
private static String processHexColors(String text) {
66+
Matcher matcher = HEX_PATTERN.matcher(text);
67+
StringBuffer buffer = new StringBuffer();
68+
69+
while (matcher.find()) {
70+
String hexColor = matcher.group(1);
71+
String replacement = "§x§" + String.join("§", hexColor.split(""));
72+
matcher.appendReplacement(buffer, replacement);
73+
}
74+
75+
matcher.appendTail(buffer);
76+
return buffer.toString();
77+
}
78+
79+
private static String processLegacyCodes(String text) {
80+
return text.replace('&', '§');
81+
}
82+
83+
private static String applyGradient(String text, String startColor, String endColor) {
84+
TextColor start = TextColor.fromHexString("#" + startColor);
85+
TextColor end = TextColor.fromHexString("#" + endColor);
86+
87+
StringBuilder result = new StringBuilder();
88+
boolean isBold = text.contains("§l") || text.contains("&l");
89+
boolean isItalic = text.contains("§o") || text.contains("&o");
90+
boolean isUnderlined = text.contains("§n") || text.contains("&n");
91+
boolean isStrikethrough = text.contains("§m") || text.contains("&m");
92+
boolean isObfuscated = text.contains("§k") || text.contains("&k");
93+
94+
// Remove formatting markers from the actual text before processing
95+
text = text.replace("§l", "").replace("&l", "");
96+
text = text.replace("§o", "").replace("&o", "");
97+
text = text.replace("§n", "").replace("&n", "");
98+
text = text.replace("§m", "").replace("&m", "");
99+
text = text.replace("§k", "").replace("&k", "");
100+
101+
int length = text.length();
102+
for (int i = 0; i < length; i++) {
103+
float ratio = (float) i / length; // Changed ratio calculation
104+
TextColor color = interpolateColor(start, end, ratio);
105+
106+
TextComponent.Builder component = Component.text()
107+
.content(String.valueOf(text.charAt(i)))
108+
.color(color);
109+
110+
if (isBold) component.decorate(TextDecoration.BOLD);
111+
if (isItalic) component.decorate(TextDecoration.ITALIC);
112+
if (isUnderlined) component.decorate(TextDecoration.UNDERLINED);
113+
if (isStrikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
114+
if (isObfuscated) component.decorate(TextDecoration.OBFUSCATED);
115+
116+
result.append(LegacyComponentSerializer.legacySection().serialize(component.build()));
117+
}
118+
119+
return result.toString();
120+
}
121+
122+
123+
private static String applySolidColor(String text, String hexColor) {
124+
TextColor color = TextColor.fromHexString("#" + hexColor);
125+
126+
boolean isBold = text.contains("§l") || text.contains("&l");
127+
boolean isItalic = text.contains("§o") || text.contains("&o");
128+
boolean isUnderlined = text.contains("§n") || text.contains("&n");
129+
boolean isStrikethrough = text.contains("§m") || text.contains("&m");
130+
boolean isObfuscated = text.contains("§k") || text.contains("&k");
131+
132+
text = text.replace("§l", "").replace("&l", "");
133+
text = text.replace("§o", "").replace("&o", "");
134+
text = text.replace("§n", "").replace("&n", "");
135+
text = text.replace("§m", "").replace("&m", "");
136+
text = text.replace("§k", "").replace("&k", "");
137+
138+
TextComponent.Builder component = Component.text()
139+
.content(text)
140+
.color(color);
141+
142+
if (isBold) component.decorate(TextDecoration.BOLD);
143+
if (isItalic) component.decorate(TextDecoration.ITALIC);
144+
if (isUnderlined) component.decorate(TextDecoration.UNDERLINED);
145+
if (isStrikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
146+
if (isObfuscated) component.decorate(TextDecoration.OBFUSCATED);
147+
148+
return LegacyComponentSerializer.legacySection().serialize(component.build());
149+
}
150+
151+
private static TextColor interpolateColor(TextColor start, TextColor end, float ratio) {
152+
int red = (int) (start.red() + (end.red() - start.red()) * ratio);
153+
int green = (int) (start.green() + (end.green() - start.green()) * ratio);
154+
int blue = (int) (start.blue() + (end.blue() - start.blue()) * ratio);
155+
156+
return TextColor.color(red, green, blue);
157+
}
158+
}

0 commit comments

Comments
 (0)