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 ae62704c..4e979e2a 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,8 @@ class Powerline(object): def __init__(self, args, config, theme): self.args = args self.config = config + 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 +108,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))