Browse Source

[+] RGB: Convert to ANSI TrueColor

Azalea (on HyDEV-Daisy) 3 years ago
parent
commit
e08a2fa91e
1 changed files with 35 additions and 0 deletions
  1. 35 0
      hyfetch/color_util.py

+ 35 - 0
hyfetch/color_util.py

@@ -24,3 +24,38 @@ class RGB(NamedTuple):
         g = int(hex[2:4], 16)
         b = int(hex[4:6], 16)
         return cls(r, g, b)
+
+    def to_ansi_rgb(self, foreground: bool = True) -> str:
+        """
+        Convert RGB to ANSI TrueColor (RGB) Escape Code.
+
+        This uses the 24-bit color encoding (an uint8 for each color value), and supports 16 million
+        colors. However, not all terminal emulators support this escape code. (For example, IntelliJ
+        debug console doesn't support it).
+
+        Currently, we do not know how to detect whether a terminal environment supports ANSI RGB. If
+        you have any thoughts, feel free to submit an issue on our Github page!
+
+        :param foreground: Whether the color is for foreground text or background color
+        :return: ANSI RGB escape code like \033[38;2;255;100;0m
+        """
+        c = '38' if foreground else '48'
+        return f'\033[{c};2;{self.r};{self.g};{self.b}m'
+
+    def to_ansi_256(self, foreground: bool = True) -> str:
+        """
+        Convert RGB to ANSI 256 Color Escape Code.
+
+        This encoding supports 256 colors in total.
+
+        :return: ANSI 256 escape code like \033[38;5;206m'
+        """
+        raise NotImplementedError()
+
+    def to_ansi_16(self) -> str:
+        """
+        Convert RGB to ANSI 16 Color Escape Code
+
+        :return: ANSI 16 escape code
+        """
+        raise NotImplementedError()