Skip to content

Commit

Permalink
Merge pull request #36 from Vankka/colorlevel/8color
Browse files Browse the repository at this point in the history
Add indexed 8 color level
  • Loading branch information
zml2008 authored Oct 4, 2023
2 parents 99e103e + a00d4a6 commit a74d89c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/main/java/net/kyori/ansi/ColorLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,18 @@ public enum ColorLevel {
INDEXED_16 {
@Override
public @NotNull String determineEscape(final int rgbColor) {
float matchedDistance = Float.MAX_VALUE;
StandardColor match = StandardColor.BLACK;
for (final StandardColor potential : StandardColor.VALUES) {
final float distance = HSV.fromRGB(rgbColor).distance(HSV.fromRGB(potential.color));
if (distance < matchedDistance) {
match = potential;
matchedDistance = distance;
}
if (distance == 0) {
break; // same colour! whoo!
}
}
return match.index;
return findClosestColorEscape(rgbColor, StandardColor.VALUES_INDEXED16);
}
},
/**
* 8 color.
*
* @since 1.1.0
*/
INDEXED_8 {
@Override
public @NotNull String determineEscape(final int rgbColor) {
return findClosestColorEscape(rgbColor, StandardColor.VALUES_INDEXED8);
}
};

Expand Down Expand Up @@ -179,6 +178,7 @@ public enum ColorLevel {
case "truecolor": return ColorLevel.TRUE_COLOR;
case "indexed256": return ColorLevel.INDEXED_256;
case "indexed16": return ColorLevel.INDEXED_16;
case "indexed8": return ColorLevel.INDEXED_8;
// In other cases, fall through below
}
}
Expand Down Expand Up @@ -225,6 +225,22 @@ public enum ColorLevel {
*/
public abstract @NotNull String determineEscape(final int rgbColor);

private static String findClosestColorEscape(final int rgbColor, final StandardColor[] potentialColors) {
float matchedDistance = Float.MAX_VALUE;
StandardColor match = StandardColor.BLACK;
for (final StandardColor potential : potentialColors) {
final float distance = HSV.fromRGB(rgbColor).distance(HSV.fromRGB(potential.color));
if (distance < matchedDistance) {
match = potential;
matchedDistance = distance;
}
if (distance == 0) {
break; // same colour! whoo!
}
}
return match.index;
}

private enum StandardColor {
BLACK(0x00_00_00, "30"),
DARK_RED(0xaa_00_00, "31"),
Expand All @@ -243,7 +259,8 @@ private enum StandardColor {
AQUA(0x55_ff_ff, "96"),
WHITE(0xff_ff_ff, "97");

static final StandardColor[] VALUES;
static final StandardColor[] VALUES_INDEXED16;
static final StandardColor[] VALUES_INDEXED8;

final int color;
final String index;
Expand All @@ -254,7 +271,11 @@ private enum StandardColor {
}

static {
VALUES = StandardColor.values();
VALUES_INDEXED16 = StandardColor.values();

final StandardColor[] indexed8 = new StandardColor[8];
System.arraycopy(StandardColor.values(), 0, indexed8, 0, 8);
VALUES_INDEXED8 = indexed8;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/net/kyori/ansi/ANSIComponentRendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void testColor() {
assertEquals("\u001B[38;2;255;85;85mi'm red\u001b[0m", this.render(redAction, ColorLevel.TRUE_COLOR));
assertEquals("\u001B[38;5;9mi'm red\u001b[0m", this.render(redAction, ColorLevel.INDEXED_256));
assertEquals("\u001B[91mi'm red\u001b[0m", this.render(redAction, ColorLevel.INDEXED_16));
assertEquals("\u001B[31mi'm red\u001b[0m", this.render(redAction, ColorLevel.INDEXED_8));
assertEquals("i'm red", this.render(redAction, ColorLevel.NONE));

final Consumer<ANSIComponentRenderer<TestStyle>> pureRedAction = r -> {
Expand All @@ -67,6 +68,7 @@ void testColor() {
assertEquals("\u001B[38;2;255;0;0mi'm very red\u001b[0m", this.render(pureRedAction, ColorLevel.TRUE_COLOR));
assertEquals("\u001B[38;5;196mi'm very red\u001b[0m", this.render(pureRedAction, ColorLevel.INDEXED_256));
assertEquals("\u001B[31mi'm very red\u001b[0m", this.render(pureRedAction, ColorLevel.INDEXED_16));
assertEquals("\u001B[31mi'm very red\u001b[0m", this.render(pureRedAction, ColorLevel.INDEXED_8));
assertEquals("i'm very red", this.render(pureRedAction, ColorLevel.NONE));
}

Expand Down

0 comments on commit a74d89c

Please sign in to comment.