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));
}
/**
* ANSI control code helpers
*/
@ -454,6 +456,9 @@ public class TerminalImageViewer {
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
*/
@ -463,24 +468,43 @@ public class TerminalImageViewer {
for (int y = 0; y < height - 7; y += 8) {
int pos = y * width * 4;
String lastFg = "";
String lastBg = "";
for (int x = 0; x < width - 3; x += 4) {
blockChar.load(data, pos, width * 4);
String fg = Ansi.color(Ansi.FG|mode, blockChar.fgColor[0], blockChar.fgColor[1], blockChar.fgColor[2]);
String bg = Ansi.color(Ansi.BG|mode, blockChar.bgColor[0], blockChar.bgColor[1], blockChar.bgColor[2]);
if (!fg.equals(lastFg)) {
sb.append(fg);
lastFg = fg;
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;
}
if (!bg.equals(lastBg)) {
sb.append(bg);
lastBg = bg;
} else {
String lastFg = "";
String lastBg = "";
for (int x = 0; x < width - 3; x += 4) {
blockChar.load(data, pos, width * 4);
String fg = Ansi.color(Ansi.FG | mode, blockChar.fgColor[0], blockChar.fgColor[1], blockChar.fgColor[2]);
String bg = Ansi.color(Ansi.BG | mode, blockChar.bgColor[0], blockChar.bgColor[1], blockChar.bgColor[2]);
if (!fg.equals(lastFg)) {
sb.append(fg);
lastFg = fg;
}
if (!bg.equals(lastBg)) {
sb.append(bg);
lastBg = bg;
}
sb.append(blockChar.character);
pos += 16;
}
sb.append(blockChar.character);
pos += 16;
}
sb.append(Ansi.RESET).append("\n");
}
}
return sb.toString();
}