foxygit / nfo Log in
commit 8643e50dbf929b85fbb284abfb092b9156a80bb5
Author:     MrjensK <jens.se@icloud.com>
AuthorDate: Wed Apr 22 21:02:55 2026 +0200
Commit:     MrjensK <jens.se@icloud.com>
CommitDate: Wed Apr 22 21:02:55 2026 +0200

    256 colors
---
 index.html | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/index.html b/index.html
index ec0133c..05e859a 100644
--- a/index.html
+++ b/index.html
@@ -324,6 +324,7 @@
   <div class="dlg-row"><label>Rader</label><input type="number" id="img-rows" value="40" min="5" max="300"></div>
   <div class="dlg-check"><input type="checkbox" id="img-ascii" onchange="imgAsciiToggle()"><label for="img-ascii">ASCII-tecken  <span id="img-ascii-ramp" style="color:var(--dim2);font-size:10px;letter-spacing:1px"> .:-=+*#%@</span></label></div>
   <div class="dlg-check" id="img-half-row"><input type="checkbox" id="img-half" checked><label for="img-half">Halvblock ▀ (dubbel vertikal upplösning)</label></div>
+  <div class="dlg-check"><input type="checkbox" id="img-256"><label for="img-256">256 färger <span style="color:var(--dim2);font-size:10px">(bättre färgnoggrannhet)</span></label></div>
   <div class="dlg-check"><input type="checkbox" id="img-dither" checked><label for="img-dither">Dithering (bättre färgövergångar)</label></div>
   <div class="dlg-check"><input type="checkbox" id="img-aspect" checked onchange="imgAutoRows()"><label for="img-aspect">Bevara proportioner</label></div>
   <div id="img-preview-wrap"></div>
@@ -1566,13 +1567,15 @@ function buildANSBytes() {
   const bytes = [];
   function pushStr(s) { for (let i=0;i<s.length;i++) bytes.push(s.charCodeAt(i)); }
   function emitColor(fg,bg) {
-    const bold=fg>=8, base=bold?fg-8:fg;
-    const parts=['0'];
-    if (bold) parts.push('1');
-    parts.push(String(30+base));
-    if (bg>=0 && bg<8)  parts.push(String(40+bg));
-    if (bg>=8)          parts.push(String(100+(bg-8)));
-    pushStr('\x1b['+parts.join(';')+'m');
+    pushStr('\x1b[0m');
+    if (fg <= 7)       { if (fg!==7) pushStr(`\x1b[${30+fg}m`); }
+    else if (fg <= 15) { pushStr(`\x1b[1;${30+fg-8}m`); }
+    else               { pushStr(`\x1b[38;5;${fg}m`); }
+    if (bg >= 0) {
+      if (bg <= 7)       pushStr(`\x1b[${40+bg}m`);
+      else if (bg <= 15) pushStr(`\x1b[${100+(bg-8)}m`);
+      else               pushStr(`\x1b[48;5;${bg}m`);
+    }
   }
   let curFg=7, curBg=-1;
   emitColor(7,-1);
@@ -1718,6 +1721,7 @@ function doImgConvert() {
   const rows   = parseInt(document.getElementById('img-rows').value,   10) || 40;
   const ascii  = document.getElementById('img-ascii').checked;
   const half   = !ascii && document.getElementById('img-half').checked;
+  const use256 = document.getElementById('img-256').checked;
   const dither = document.getElementById('img-dither').checked;
   hideImgDlg();

@@ -1735,9 +1739,10 @@ function doImgConvert() {
     parseInt(h.slice(1,3),16), parseInt(h.slice(3,5),16), parseInt(h.slice(5,7),16)
   ]);

+  const colorLimit = use256 ? 256 : 16;
   function nearest(r,g,b) {
     let best=0, bestD=Infinity;
-    for (let i=0;i<16;i++) {
+    for (let i=0;i<colorLimit;i++) {
       const [pr,pg,pb]=palRGB[i];
       const d=(r-pr)**2+(g-pg)**2+(b-pb)**2;
       if (d<bestD) { bestD=d; best=i; }