Skip to content

Commit 215b301

Browse files
committed
Version 1.0
0 parents  commit 215b301

File tree

5 files changed

+398
-0
lines changed

5 files changed

+398
-0
lines changed

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) [2023] [Jiří Apjár]
4+
Copyright (c) [2023] [Filip Zeman]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# ForestColorAPI
2+
![badge](https://img.shields.io/github/v/release/ForestTechMC/ForestColorAPI)
3+
[![badge](https://jitpack.io/v/ForestTechMC/ForestColorAPI.svg)](https://jitpack.io/#ForestTechMC/ForestColorAPI)
4+
![badge](https://img.shields.io/github/downloads/ForestTechMC/ForestColorAPI/total)
5+
![badge](https://img.shields.io/github/last-commit/ForestTechMC/ForestColorAPI)
6+
![badge](https://img.shields.io/badge/platform-spigot%20%7C%20bungeecord-lightgrey)
7+
[![badge](https://img.shields.io/discord/896466173166747650?label=discord)](https://discord.gg/2PpdrfxhD4)
8+
[![badge](https://img.shields.io/github/license/ForestTechMC/ForestColorAPI)](https://github.com/ForestTechMC/ForestColorAPI/blob/master/LICENSE.txt)
9+
10+
**[JavaDoc 1.0](https://foresttechmc.github.io/ForestColorAPI/1.0/)**
11+
12+
Small and effective Color API for your plugins.\
13+
Only 1.16+ version of spigot support!
14+
15+
## Table of contents
16+
17+
* [Getting started](#getting-started)
18+
* [Example of patterns](#example-of-patterns)
19+
* [Using the color API](#using-color-api)
20+
* [License](#license)
21+
22+
## Getting started
23+
24+
Make sure you reloaded maven or gradle in your project.
25+
26+
### Add ForestColorAPI to your project
27+
28+
[![badge](https://jitpack.io/v/ForestTechMC/ForestColorAPI.svg)](https://jitpack.io/#ForestTechMC/ForestColorAPI)
29+
30+
You need to add this dependency into your plugin, then look at under the dependencies example
31+
32+
<details>
33+
<summary>Maven</summary>
34+
35+
```xml
36+
<repositories>
37+
<repository>
38+
<id>jitpack.io</id>
39+
<url>https://jitpack.io</url>
40+
</repository>
41+
</repositories>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>com.github.ForestTechMC</groupId>
46+
<artifactId>ForestColorAPI</artifactId>
47+
<version>VERSION</version>
48+
<scope>provided</scope>
49+
</dependency>
50+
</dependencies>
51+
```
52+
</details>
53+
54+
<details>
55+
<summary>Gradle</summary>
56+
57+
```gradle
58+
allprojects {
59+
repositories {
60+
...
61+
maven { url 'https://jitpack.io' }
62+
}
63+
}
64+
65+
dependencies {
66+
implementation 'com.github.ForestTechMC:ForestColorAPI:VERSION'
67+
}
68+
```
69+
</details>
70+
71+
### Example of patterns
72+
73+
If you want to use gradient you need to use pattern like `{/#b36000}Super gradient!{/#72cc00}`
74+
For normal RGB `{#72cc00}Super color!`
75+
76+
77+
### Using Color API
78+
79+
```java
80+
// Example of removing chars, legacy colors, and patterns
81+
String cleanMessage = ColorAPI.clear("{/#b36000}Super gradient{/#72cc00} &3Some color");
82+
// Output -> "Super gradient Some color" message without patterns, colors, chars...
83+
// You can use separate methods to clear only some kind of coloring
84+
85+
// Example of universal usage for colorize
86+
player.sendMessage(ColorAPI.colorize("{/#b36000}Super gradient{/#72cc00} {#b36000}Super RGB color"));
87+
88+
// Example of gradient method
89+
player.sendMessage(ColorAPI.colorizeGradient("{/#b36000}Super gradient{/#72cc00}"));
90+
91+
// Example of RGB method
92+
player.sendMessage(ColorAPI.colorizeRGB("{#b36000}Super RGB color"));
93+
94+
// Example of classic method
95+
player.sendMessage(ColorAPI.colorizeClassic("&3Some color"));
96+
97+
// Example of selecting colorize type <GRADIENT | RGB | CLASSIC>
98+
player.sendMessage(ColorAPI.colorizeType(ColorizeType.RGB, "{#b36000}Super RGB color"));
99+
```
100+
101+
## License
102+
ForestRedisAPI is licensed under the permissive MIT license. Please see [`LICENSE.txt`](https://github.com/ForestTechMC/ForestColorAPI/blob/master/LICENSE.txt) for more information.

pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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>org.example</groupId>
8+
<artifactId>ForestColorAPI</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
18+
<repositories>
19+
<repository>
20+
<id>spigot-repo</id>
21+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
22+
</repository>
23+
<repository>
24+
<id>jitpack.io</id>
25+
<url>https://jitpack.io</url>
26+
</repository>
27+
</repositories>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>net.md-5</groupId>
32+
<artifactId>bungeecord-chat</artifactId>
33+
<version>1.16-R0.4</version>
34+
</dependency>
35+
</dependencies>
36+
</project>
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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

Comments
 (0)