-
Notifications
You must be signed in to change notification settings - Fork 29
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
1 parent
5db7fd2
commit f728c3e
Showing
4 changed files
with
48 additions
and
3 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
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
43 changes: 43 additions & 0 deletions
43
Common/src/main/java/net/frankheijden/serverutils/common/utils/HexUtils.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,43 @@ | ||
package net.frankheijden.serverutils.common.utils; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Provides basic conversion between the hex format <#FFFFFF> and bukkit hex format &x&F&F&F&F&F&F. | ||
*/ | ||
public class HexUtils { | ||
|
||
private static final Pattern hexPattern = Pattern.compile("<(#[0-9a-fA-F]{6})>"); | ||
private static final char COLOR_CHAR = '&'; | ||
|
||
/** | ||
* Prefixes each character provided with the color character {@link #COLOR_CHAR}. | ||
* @param color The color to prefix with the color character. | ||
* @return The prefixed color. | ||
*/ | ||
public static String convertHexColor(String color) { | ||
StringBuilder sb = new StringBuilder(2 * (color.length() + 1)).append(COLOR_CHAR).append("x"); | ||
for (char c : color.toCharArray()) { | ||
sb.append(COLOR_CHAR).append(c); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* Converts the input string to a bukkit readable color string. | ||
* The accepted hex format is `<#FFFFFF>`. | ||
* @param str The input string. | ||
* @return The output converted hex string. | ||
*/ | ||
public static String convertHexString(String str) { | ||
StringBuffer sb = new StringBuffer(str.length()); | ||
Matcher matcher = hexPattern.matcher(str); | ||
while (matcher.find()) { | ||
String hex = matcher.group(); | ||
matcher.appendReplacement(sb, convertHexColor(hex.substring(2, hex.length() - 1))); | ||
} | ||
matcher.appendTail(sb); | ||
return sb.toString(); | ||
} | ||
} |
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