add color

This commit is contained in:
Looly
2022-09-15 12:28:23 +08:00
parent 08a829cf6d
commit 790315f839
18 changed files with 263 additions and 18 deletions

View File

@@ -113,9 +113,42 @@ public enum Ansi4BitBackgroundColor implements AnsiElement {
return this.code;
}
/**
* 获取ANSI颜色代码
*
* @param isBackground 是否背景色
* @return 颜色代码
*/
public int getCode(boolean isBackground) {
return isBackground ? this.code : this.code - 10;
}
/**
* 获取背景色对应的前景色
*
* @return 前景色
*/
public Ansi4BitColor asForeground() {
return Ansi4BitColor.of(getCode(false));
}
@Override
public String toString() {
return StrUtil.toString(this.code);
}
/**
* 根据code查找对应的Ansi4BitBackgroundColor
*
* @param code Ansi 4bit 颜色代码
* @return Ansi4BitBackgroundColor
*/
public static Ansi4BitBackgroundColor of(int code) {
for (Ansi4BitBackgroundColor item : Ansi4BitBackgroundColor.values()) {
if (item.getCode() == code) {
return item;
}
}
throw new IllegalArgumentException(StrUtil.format("No matched Ansi4BitBackgroundColor instance,code={}", code));
}
}

View File

@@ -123,9 +123,32 @@ public enum Ansi4BitColor implements AnsiElement {
return isBackground ? this.code + 10 : this.code;
}
/**
* 获取前景色对应的背景色
*
* @return 背景色
*/
public Ansi4BitBackgroundColor asBackground() {
return Ansi4BitBackgroundColor.of(getCode(true));
}
@Override
public String toString() {
return StrUtil.toString(this.code);
}
/**
* 根据code查找对应的Ansi4BitColor
*
* @param code Ansi 4bit 颜色代码
* @return Ansi4BitColor
*/
public static Ansi4BitColor of(int code) {
for (Ansi4BitColor item : Ansi4BitColor.values()) {
if (item.getCode() == code) {
return item;
}
}
throw new IllegalArgumentException(StrUtil.format("No matched Ansi4BitColor instance,code={}", code));
}
}

View File

@@ -70,6 +70,30 @@ public class Ansi8BitColor implements AnsiElement {
return this.code;
}
/**
* 转换为前景色
*
* @return 前景色
*/
public Ansi8BitColor asForeground() {
if (PREFIX_FORE.equals(this.prefix)) {
return this;
}
return Ansi8BitColor.foreground(this.code);
}
/**
* 转换为背景色
*
* @return 背景色
*/
public Ansi8BitColor asBackground() {
if (PREFIX_BACK.equals(this.prefix)) {
return this;
}
return Ansi8BitColor.background(this.code);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {

View File

@@ -1275,6 +1275,7 @@ public class MapUtil extends MapGetUtil {
* @param map Map一般用于线程安全的Map
* @param key 键
* @param mappingFunction 值计算函数
* @return 值
* @see <a href="https://bugs.openjdk.java.net/browse/JDK-8161372">https://bugs.openjdk.java.net/browse/JDK-8161372</a>
*/
public static <K, V> V computeIfAbsent(final Map<K, V> map, final K key, final Function<? super K, ? extends V> mappingFunction) {