From c29afdb83e7fd180e8c539507aa814bae2c92969 Mon Sep 17 00:00:00 2001 From: Spencer Warneke Date: Mon, 15 Feb 2021 18:44:19 -0800 Subject: [PATCH 1/2] feature/truecolor: TrueColor Theming Support Adds the ability to use (R,G,B) tuples in theme files to more accurately specify the colors desired for each segment. Not all terminals have TrueColor support, so in the case that a theme uses (R,G,B) tuples that cannot be supported by the current terminal, the colors are approximated using colortrans.rgb2short to bring them to the nearest xterm-256 color. --- powerline_shell/__init__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/powerline_shell/__init__.py b/powerline_shell/__init__.py index ae62704c..ce4ef904 100644 --- a/powerline_shell/__init__.py +++ b/powerline_shell/__init__.py @@ -6,6 +6,7 @@ import sys import importlib import json +from . import colortrans from .utils import warn, py3, import_file import re @@ -86,6 +87,10 @@ class Powerline(object): def __init__(self, args, config, theme): self.args = args self.config = config + theme_wants_truecolor = hasattr(theme, "use_truecolor") and theme.use_truecolor + + env_colorterm = os.getenv("COLORTERM") + self.truecolor_supported = env_colorterm is not None and "truecolor" in env_colorterm self.theme = theme self.cwd = get_valid_cwd() mode = config.get("mode", "patched") @@ -105,6 +110,12 @@ def color(self, prefix, code): return '' elif code == self.theme.RESET: return self.reset + elif isinstance(code, tuple): + # Do we support TrueColor? If not, supply the closest possible xterm-256 color. + if self.truecolor_supported: + return self.color_template % ('[%s;2;%s;%s;%sm' % (prefix, *code)) + else: + return self.color_template % ('[%s;5;%sm' % (prefix, colortrans.rgb2short(*code))) else: return self.color_template % ('[%s;5;%sm' % (prefix, code)) From ba81ac9502e0908c57e31dabff261486ac98b447 Mon Sep 17 00:00:00 2001 From: Spencer Warneke Date: Mon, 15 Feb 2021 19:03:43 -0800 Subject: [PATCH 2/2] feature/truecolor: README update and TrueColor color test added --- README.md | 13 ++++++++++--- colortest_truecolor.py | 23 +++++++++++++++++++++++ powerline_shell/__init__.py | 2 -- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100755 colortest_truecolor.py diff --git a/README.md b/README.md index e6295711..98802156 100644 --- a/README.md +++ b/README.md @@ -261,10 +261,17 @@ to the path of the file. For example your configuration might have: "theme": "~/mythemes/my-great-theme.py" ``` -You can then modify the color codes to your liking. Theme colors are specified -using [Xterm-256 color codes](https://jonasjacek.github.io/colors/). +You can then modify the color codes to your liking. Theme colors can be specified +using [Xterm-256 color codes](https://jonasjacek.github.io/colors/), or using RGB tuple values +if your terminal supports TrueColor. To check if this is the case, you can run: -A script for testing color combinations is provided at `colortest.py`. Note +``` + $ echo $COLORTERM +``` + +If `truecolor` is printed, you're good to go with RGB values. + +Scripts for testing color combinations are provided at `colortest.py` and `colortest_truecolor.py`. Note that the colors you see may vary depending on your terminal. When designing a theme, please test your theme on multiple terminals, especially with default settings. diff --git a/colortest_truecolor.py b/colortest_truecolor.py new file mode 100755 index 00000000..29e4400a --- /dev/null +++ b/colortest_truecolor.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python2 +import sys + +ESCAPE = chr(27) + +def fg(color): + return ESCAPE + '[38;2;%d;%d;%dm' % color + +def bg(color): + return ESCAPE + '[48;2;%d;%d;%dm' % color + +def reset(): + return ESCAPE + '[48;0m' + +if __name__ == "__main__": + if len(sys.argv) < 8: + print 'Usage: colortest_truecolor.py fg_red fg_green fg_blue bg_red bg_green bg_blue test_string' + sys.exit(1) + + fg_red, fg_green, fg_blue, bg_red, bg_green, bg_blue = map(int, sys.argv[1:-1]) + test_string = sys.argv[-1] + + print bg((bg_red, bg_green, bg_blue)), fg((fg_red, fg_green, fg_blue)), test_string, reset() diff --git a/powerline_shell/__init__.py b/powerline_shell/__init__.py index ce4ef904..4e979e2a 100644 --- a/powerline_shell/__init__.py +++ b/powerline_shell/__init__.py @@ -87,8 +87,6 @@ class Powerline(object): def __init__(self, args, config, theme): self.args = args self.config = config - theme_wants_truecolor = hasattr(theme, "use_truecolor") and theme.use_truecolor - env_colorterm = os.getenv("COLORTERM") self.truecolor_supported = env_colorterm is not None and "truecolor" in env_colorterm self.theme = theme