aboutsummaryrefslogtreecommitdiffstats
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
parentb5146e27f103523494fdec0272a084c18afe1a69 (diff)
downloadchawan-ea70eea0b7a4abea2f944195ea69db5ea5b9f3ea.tar.gz
term: fix ANSI color approximation
Fixes a bug that lead to incorrect color approximation.
-rw-r--r--src/display/term.nim15
-rw-r--r--src/utils/chamath.nim4
2 files changed, 8 insertions, 11 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
diff --git a/src/utils/chamath.nim b/src/utils/chamath.nim
deleted file mode 100644
index 579b7b69..00000000
--- a/src/utils/chamath.nim
+++ /dev/null
@@ -1,4 +0,0 @@
-func absSub*(a, b: SomeUnsignedInt): auto {.inline.} =
- if a > b:
- return a - b
- return b - a