diff options
author | bptato <nincsnevem662@gmail.com> | 2023-06-10 01:56:08 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-06-10 01:56:08 +0200 |
commit | 1bd7651f70ef7ba7a3506f12d4c9f4d7735180f5 (patch) | |
tree | f05b9750153e18e2d3856869e7df4a2ea69f58ea | |
parent | e8e9d27c9496ee5a0679197272d5ca8d1b5a2953 (diff) | |
download | chawan-1bd7651f70ef7ba7a3506f12d4c9f4d7735180f5.tar.gz |
approximateANSIColor: fix possible underflow
-rw-r--r-- | src/display/term.nim | 7 | ||||
-rw-r--r-- | src/utils/chamath.nim | 4 |
2 files changed, 8 insertions, 3 deletions
diff --git a/src/display/term.nim b/src/display/term.nim index fa3c5e7f..332ecd33 100644 --- a/src/display/term.nim +++ b/src/display/term.nim @@ -12,6 +12,7 @@ import config/config import data/charset import encoding/encoderstream import io/window +import utils/chamath import utils/twtstr import types/color @@ -217,9 +218,9 @@ proc approximateANSIColor(rgb: RGBColor, exclude = -1): int = if i == exclude: continue let color = ANSIColorMap[i] if color == rgb: return i - let x = uint16(color.r - rgb.r) ^ 2 - let y = uint16(color.g - rgb.b) ^ 2 - let z = uint16(color.g - rgb.g) ^ 2 + 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 if n == -1 or b < a: n = i diff --git a/src/utils/chamath.nim b/src/utils/chamath.nim new file mode 100644 index 00000000..579b7b69 --- /dev/null +++ b/src/utils/chamath.nim @@ -0,0 +1,4 @@ +func absSub*(a, b: SomeUnsignedInt): auto {.inline.} = + if a > b: + return a - b + return b - a |