From e08a2fa91eb4bc052228e297b3b6663e0ce0b46f Mon Sep 17 00:00:00 2001 From: "Azalea (on HyDEV-Daisy)" Date: Sun, 10 Apr 2022 03:18:18 -0400 Subject: [PATCH] [+] RGB: Convert to ANSI TrueColor --- hyfetch/color_util.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/hyfetch/color_util.py b/hyfetch/color_util.py index 79f0962e..04b0f8c2 100644 --- a/hyfetch/color_util.py +++ b/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()