Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding TrueColor/RGB support to themes #515

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions colortest_truecolor.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions powerline_shell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import importlib
import json
from . import colortrans
from .utils import warn, py3, import_file
import re

Expand Down Expand Up @@ -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")
Expand All @@ -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))

Expand Down