about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsterni <sternenseemann@systemli.org>2021-07-09 21:49:50 +0200
committerSören Tempel <soeren+git@soeren-tempel.net>2021-07-15 13:28:33 +0200
commit206c5894492fabc675fc8e6ab38480292aaffe18 (patch)
treeceaa955c9b377916c37dfddbb733ff56e3bd84e5
parentc2870abc18c8d5e80b180e5fd15c7f799ac820e6 (diff)
pty.color: use named constants in 256 color conversion
This should make the code a bit more readable, hopefully. Also
simplify the conditions while we're at it.
-rw-r--r--saneterm/color.py31
1 files changed, 24 insertions, 7 deletions
diff --git a/saneterm/color.py b/saneterm/color.py
index 06e1ade..0674f28 100644
--- a/saneterm/color.py
+++ b/saneterm/color.py
@@ -2,6 +2,20 @@ from enum import Enum, auto, unique
 
 from gi.repository import Gdk
 
+# lower bounds for the extended (256) color sections
+# except for the regular 8 colors:
+#
+#     0 -   7   regular 8 colors
+#     8 -  15   bright 8 colors
+#    16 - 231   6 * 6 * 6 color cube
+#   232 - 255   24 step grayscale
+#
+# For a description of the sections and their meaning
+# as well as color values see the comment in Color.to_gdk()
+EXTENDED_COLOR_BRIGHT_LOWER = 8
+EXTENDED_COLOR_CUBE_LOWER = 16
+EXTENDED_COLOR_GRAYSCALE_LOWER = 232
+
 @unique
 class BasicColor(Enum):
     BLACK = 0
@@ -134,13 +148,16 @@ class Color(object):
         elif self.type is ColorType.TRUECOLOR:
             return int_triple_to_rgba(self.data)
         elif self.type is ColorType.NUMBERED_256:
-            if self.data < 8:
+            if self.data < EXTENDED_COLOR_BRIGHT_LOWER:
                 # normal 8 colors
                 return basic_color_to_rgba(BasicColor(self.data), bright=False)
-            elif self.data >= 8 and self.data < 16:
+            elif self.data < EXTENDED_COLOR_CUBE_LOWER:
                 # bright 8 colors
-                return basic_color_to_rgba(BasicColor(self.data - 8), bright=True)
-            elif self.data >= 16 and self.data < 232:
+                return basic_color_to_rgba(
+                    BasicColor(self.data - EXTENDED_COLOR_BRIGHT_LOWER),
+                    bright=True
+                )
+            elif self.data < EXTENDED_COLOR_GRAYSCALE_LOWER:
                 # color cube which is constructed in the following manner:
                 #
                 # * The color number is described by the following formula:
@@ -153,13 +170,13 @@ class Color(object):
                 # This is not documented anywhere as far as I am aware.
                 # The information presented here has been reverse engineered
                 # from XTerm's 256colres.pl.
-                tmp = self.data - 16
-                (r, tmp) = divmod(tmp, 36)
+                tmp = self.data - EXTENDED_COLOR_CUBE_LOWER
+                (r, tmp) = divmod(tmp, 6 * 6)
                 (g, b) = divmod(tmp, 6)
 
                 triple = tuple(map(extended_color_val, (r, g, b)))
                 return Gdk.RGBA(*triple)
             else:
                 # grayscale in 24 steps
-                c = (self.data - 232) * (1.0/24)
+                c = (self.data - EXTENDED_COLOR_GRAYSCALE_LOWER) * (1.0/24)
                 return Gdk.RGBA(c, c, c, 1.0)