aboutsummaryrefslogtreecommitdiffstats
path: root/src/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/io')
-rw-r--r--src/io/term.nim33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/io/term.nim b/src/io/term.nim
index 0406907d..518990e3 100644
--- a/src/io/term.nim
+++ b/src/io/term.nim
@@ -42,6 +42,7 @@ type
cleared: bool
prevgrid: FixedGrid
attrs*: WindowAttributes
+ mincontrast: float
colormode: ColorMode
formatmode: FormatMode
smcup: bool
@@ -162,6 +163,32 @@ proc disableAltScreen(term: Terminal): string =
else:
return RMCUP()
+proc distance(a, b: CellColor): float =
+ let a = if a.rgb:
+ a.rgbcolor
+ elif a == defaultColor:
+ ColorsRGB["black"]
+ else:
+ ANSIColorMap[a.color mod 10]
+ let b = if b.rgb:
+ b.rgbcolor
+ elif b == defaultColor:
+ ColorsRGB["white"]
+ else:
+ ANSIColorMap[b.color mod 10]
+ sqrt(float((a.r - b.r) ^ 2 + (a.g - b.b) ^ 2 + (a.g - b.g) ^ 2))
+
+proc invert(color: CellColor, bg: bool): CellColor =
+ if color == defaultColor:
+ if bg:
+ return CellColor(rgb: true, rgbcolor: ColorsRGB["white"])
+ else:
+ return CellColor(rgb: true, rgbcolor: ColorsRGB["black"])
+ elif color.rgb:
+ return CellColor(rgb: true, rgbcolor: RGBColor(0xFFFFFF - uint32(color.rgbcolor)))
+ else:
+ return CellColor(rgb: true, rgbcolor: RGBColor(0xFFFFFF - uint32(ANSIColorMap[color.color mod 10])))
+
# Use euclidian distance to quantize RGB colors.
proc approximateANSIColor(rgb: RGBColor, exclude = -1): int =
var a = 0
@@ -183,6 +210,11 @@ proc processFormat*(term: Terminal, format: var Format, cellf: Format): string =
result &= SGR(FormatCodes[flag].e)
var cellf = cellf
+ if term.mincontrast >= 0 and distance(cellf.bgcolor, cellf.fgcolor) <= term.mincontrast:
+ cellf.fgcolor = invert(cellf.fgcolor, false)
+ if distance(cellf.bgcolor, cellf.fgcolor) <= term.mincontrast:
+ cellf.fgcolor = defaultColor
+ cellf.bgcolor = defaultColor
case term.colormode
of ANSI, EIGHT_BIT:
if cellf.bgcolor.rgb:
@@ -416,6 +448,7 @@ proc detectTermAttributes(term: Terminal) =
term.formatmode = term.config.formatmode.get
if term.config.altscreen.isSome:
term.smcup = term.config.altscreen.get
+ term.mincontrast = term.config.mincontrast
proc start*(term: Terminal, infile: File) =
term.infile = infile