This commit is contained in:
Stefan Haustein 2016-07-17 03:40:53 +02:00
parent 1082b5aea8
commit f9ddd5df2d

View file

@ -136,6 +136,8 @@ public class TerminalImageViewer {
System.out.print(imageData.dump(mode)); System.out.print(imageData.dump(mode));
} }
/** /**
* ANSI control code helpers * ANSI control code helpers
*/ */
@ -454,6 +456,9 @@ public class TerminalImageViewer {
this.data = new byte[width * height * 4]; this.data = new byte[width * height * 4];
} }
public String hex6(int r, int g, int b) {
return Integer.toHexString((1 << 24) | ((r & 255) << 16) | ((g & 255) << 8) | (b & 255)).substring(1);
}
/** /**
* Convert the image to an Ansi control character string setting the colors * Convert the image to an Ansi control character string setting the colors
*/ */
@ -463,6 +468,23 @@ public class TerminalImageViewer {
for (int y = 0; y < height - 7; y += 8) { for (int y = 0; y < height - 7; y += 8) {
int pos = y * width * 4; int pos = y * width * 4;
if (html) {
String last = "";
for (int x = 0; x < width - 3; x += 4) {
blockChar.load(data, pos, width * 4);
String fg = hex6(blockChar.fgColor[0], blockChar.fgColor[1], blockChar.fgColor[2]);
String bg = hex6(blockChar.bgColor[0], blockChar.bgColor[1], blockChar.bgColor[2]);
String style = "background-color:#" + bg + ";color:#" + fg;
if (!style.equals(last)) {
if (!last.isEmpty()) {
sb.append("</tt>");
}
sb.append("<tt style='").append(style).append("'>");
sb.append(blockChar.character);
pos += 16;
}
} else {
String lastFg = ""; String lastFg = "";
String lastBg = ""; String lastBg = "";
for (int x = 0; x < width - 3; x += 4) { for (int x = 0; x < width - 3; x += 4) {
@ -480,8 +502,10 @@ public class TerminalImageViewer {
sb.append(blockChar.character); sb.append(blockChar.character);
pos += 16; pos += 16;
} }
}
sb.append(Ansi.RESET).append("\n"); sb.append(Ansi.RESET).append("\n");
} }
}
return sb.toString(); return sb.toString();
} }
} }