diff --git a/src/main/java/TerminalImageViewer.java b/src/main/java/TerminalImageViewer.java index a036fa0..ed5c251 100644 --- a/src/main/java/TerminalImageViewer.java +++ b/src/main/java/TerminalImageViewer.java @@ -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(""); + } + sb.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(); }