about summary refs log tree commit diff stats
path: root/src/display
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-07-06 15:56:19 +0200
committerbptato <nincsnevem662@gmail.com>2023-07-06 16:11:18 +0200
commitea70eea0b7a4abea2f944195ea69db5ea5b9f3ea (patch)
treef86ca4fce9a0e525a7f2744675b8ee88c814f259 /src/display
parentb5146e27f103523494fdec0272a084c18afe1a69 (diff)
downloadchawan-ea70eea0b7a4abea2f944195ea69db5ea5b9f3ea.tar.gz
term: fix ANSI color approximation
Fixes a bug that lead to incorrect color approximation.
Diffstat (limited to 'src/display')
-rw-r--r--src/display/term.nim15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/display/term.nim b/src/display/term.nim
index 42946dcc..9ed448f9 100644
--- a/src/display/term.nim
+++ b/src/display/term.nim
@@ -1,4 +1,3 @@
-import math
 import options
 import os
 import streams
@@ -13,7 +12,6 @@ import data/charset
 import encoding/encoderstream
 import io/window
 import types/color
-import utils/chamath
 import utils/opt
 import utils/twtstr
 
@@ -216,16 +214,19 @@ proc getRGB(a: CellColor, bg: bool): RGBColor =
 
 # Use euclidian distance to quantize RGB colors.
 proc approximateANSIColor(rgb: RGBColor): ANSIColor =
-  var a = 0u16
+  var a = 0i32
   var n = -1
   for i in 0 .. ANSIColorMap.high:
     let color = ANSIColorMap[i]
     if color == rgb:
       return ANSIColor(i)
-    let x = uint16(absSub(color.r, rgb.r)) ^ 2
-    let y = uint16(absSub(color.g, rgb.b)) ^ 2
-    let z = uint16(absSub(color.g, rgb.g)) ^ 2
-    let b = x + y + z
+    let x = int32(color.r) - int32(rgb.r)
+    let y = int32(color.g) - int32(rgb.g)
+    let z = int32(color.b) - int32(rgb.b)
+    let xx = x * x
+    let yy = y * y
+    let zz = z * z
+    let b = xx + yy + zz
     if n == -1 or b < a:
       n = i
       a = b