summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTony Cebzanov <tonycpsu@gmail.com>2020-06-13 11:37:36 -0400
committerTony Cebzanov <tonycpsu@gmail.com>2020-06-13 11:37:36 -0400
commit274cd8b2736592128a63ce69a10063941d04938f (patch)
treefc2858f2a987f00ed4ece6a215d00311fa365ea0
parent2cc54891965283faf9113da72202f5d405f90fa3 (diff)
downloadurwid-274cd8b2736592128a63ce69a10063941d04938f.tar.gz
Find nearest approximation for 24-bit colors in 256-color AttrSpecs
Palette entries can only distinguish between "low" and "high" colors, so there's no way for a user to specify different colors for 88, 256, and 24-bit color screens. Prior to this change, someone wanting to specify a 24-bit color but have their applicaiton also work when running in 256-color mode would have to maintain two different palette entries, or do a color conversion themselves to avoid an AttrSpec error. With this change, an AttrSpec with colors=256 will instead convert 24-bit foreground and/or background colors to the nearest color from the 256-color palette. In most cases this is what the user will want, and if they want to handle 256-color mode separately, they can still do so. This just makes it easier to support both 24-bit and 8-bit color modes.
-rwxr-xr-xurwid/display_common.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/urwid/display_common.py b/urwid/display_common.py
index 878bbc9..8361b45 100755
--- a/urwid/display_common.py
+++ b/urwid/display_common.py
@@ -386,6 +386,20 @@ def _parse_color_256(desc):
except ValueError:
return None
+
+def _true_to_256(desc):
+
+ if not (desc.startswith('#') and len(desc) == 7):
+ return None
+
+ c256 = _parse_color_256("#" + "".join([
+ format(int(x, 16)//16, "x")
+ for x in [desc[1:3], desc[3:5], desc[5:7] ]
+ ]
+ ))
+ return _color_desc_256(c256)
+
+
def _parse_color_88(desc):
"""
Return a color number for the description desc.
@@ -616,7 +630,7 @@ class AttrSpec(object):
scolor = _parse_color_true(part)
flags |= _FG_TRUE_COLOR
else:
- scolor = _parse_color_256(part)
+ scolor = _parse_color_256(_true_to_256(part) or part)
flags |= _FG_HIGH_COLOR
# _parse_color_*() return None for unrecognised colors
if scolor is None:
@@ -658,7 +672,7 @@ class AttrSpec(object):
color = _parse_color_true(background)
flags |= _BG_TRUE_COLOR
else:
- color = _parse_color_256(background)
+ color = _parse_color_256(_true_to_256(background) or background)
flags |= _BG_HIGH_COLOR
if color is None:
raise AttrSpecError(("Unrecognised color specification " +