Merge pull request #3159 from InspectorCaracal/ansi-fallback

Replace ANSI color fallback logic
This commit is contained in:
Griatch 2023-04-08 18:19:23 +02:00 committed by GitHub
commit d49a8e5e0f

View file

@ -332,7 +332,7 @@ class ANSIParser(object):
colval = 134 + ord(letter) colval = 134 + ord(letter)
# ansi fallback logic expects r,g,b values in [0..5] range # ansi fallback logic expects r,g,b values in [0..5] range
gray = (ord(letter) - 97) / 5.0 gray = round((ord(letter) - 97) / 5.0)
red, green, blue = gray, gray, gray red, green, blue = gray, gray, gray
if use_xterm256: if use_xterm256:
@ -347,62 +347,57 @@ class ANSIParser(object):
else: else:
# xterm256 not supported, convert the rgb value to ansi instead # xterm256 not supported, convert the rgb value to ansi instead
if red == green == blue and red < 3: rgb = (red, green, blue)
if background:
return ANSI_BACK_BLACK def _convert_for_ansi(val):
elif red >= 1: return int((val+1)//2)
return ANSI_HILITE + ANSI_BLACK
else: # greys
return ANSI_NORMAL + ANSI_BLACK if (max(rgb) - min(rgb)) <= 1:
elif red == green == blue: match rgb:
if background: case (0,0,0):
return ANSI_BACK_WHITE return ANSI_BACK_BLACK if background else ANSI_NORMAL + ANSI_BLACK
elif red >= 4: case ((1|2), (1|2), (1|2)):
return ANSI_HILITE + ANSI_WHITE return ANSI_BACK_BLACK if background else ANSI_HILITE + ANSI_BLACK
else: case ((2|3), (2|3), (2|3)):
return ANSI_NORMAL + ANSI_WHITE return ANSI_BACK_WHITE if background else ANSI_NORMAL + ANSI_WHITE
elif red > green and red > blue: case ((3|4), (3|4), (3|4)):
if background: return ANSI_BACK_WHITE if background else ANSI_NORMAL + ANSI_WHITE
return ANSI_BACK_RED case ((4|5), (4|5), (4|5)):
elif red >= 3: return ANSI_BACK_WHITE if background else ANSI_HILITE + ANSI_WHITE
return ANSI_HILITE + ANSI_RED
else: match tuple(_convert_for_ansi(c) for c in rgb):
return ANSI_NORMAL + ANSI_RED # red
elif red == green and red > blue: case ((2|3), (0|1), (0|1)):
if background: return ANSI_BACK_RED if background else ANSI_HILITE + ANSI_RED
return ANSI_BACK_YELLOW case ((1|2), 0, 0):
elif red >= 3: return ANSI_BACK_RED if background else ANSI_NORMAL + ANSI_RED
return ANSI_HILITE + ANSI_YELLOW # green
else: case ((0|1), (2|3), (0|1)):
return ANSI_NORMAL + ANSI_YELLOW return ANSI_BACK_GREEN if background else ANSI_HILITE + ANSI_GREEN
elif red == blue and red > green: case ((0 | 1), 1, 0) if green > red:
if background: return ANSI_BACK_GREEN if background else ANSI_NORMAL + ANSI_GREEN
return ANSI_BACK_MAGENTA # blue
elif red >= 3: case ((0|1), (0|1), (2|3)):
return ANSI_HILITE + ANSI_MAGENTA return ANSI_BACK_BLUE if background else ANSI_HILITE + ANSI_BLUE
else: case (0, 0, 1):
return ANSI_NORMAL + ANSI_MAGENTA return ANSI_BACK_BLUE if background else ANSI_NORMAL + ANSI_BLUE
elif green > blue: # cyan
if background: case ((0|1|2), (2|3), (2|3)) if red == min(rgb):
return ANSI_BACK_GREEN return ANSI_BACK_CYAN if background else ANSI_HILITE + ANSI_CYAN
elif green >= 3: case (0, (1|2), (1|2)):
return ANSI_HILITE + ANSI_GREEN return ANSI_BACK_CYAN if background else ANSI_NORMAL + ANSI_CYAN
else: # yellow
return ANSI_NORMAL + ANSI_GREEN case ((2|3), (2|3), (0|1|2)) if blue == min(rgb):
elif green == blue: return ANSI_BACK_YELLOW if background else ANSI_HILITE + ANSI_YELLOW
if background: case ((2|1), (2|1), (0|1)):
return ANSI_BACK_CYAN return ANSI_BACK_YELLOW if background else ANSI_NORMAL + ANSI_YELLOW
elif green >= 3: # magenta
return ANSI_HILITE + ANSI_CYAN case ((2|3), (0|1|2), (2|3)) if green == min(rgb):
else: return ANSI_BACK_MAGENTA if background else ANSI_HILITE + ANSI_MAGENTA
return ANSI_NORMAL + ANSI_CYAN case ((1|2), 0, (1|2)):
else: # mostly blue return ANSI_BACK_MAGENTA if background else ANSI_NORMAL + ANSI_MAGENTA
if background:
return ANSI_BACK_BLUE
elif blue >= 3:
return ANSI_HILITE + ANSI_BLUE
else:
return ANSI_NORMAL + ANSI_BLUE
def strip_raw_codes(self, string): def strip_raw_codes(self, string):
""" """