From 68ac42092e073e20c02d8e7ef1a3794b5a753fca Mon Sep 17 00:00:00 2001 From: Lifepillar Date: Thu, 30 Nov 2017 18:31:18 +0100 Subject: [PATCH] Add script to change an xterm color palette. --- Readme.md | 37 +++++++--- {templates => scripts}/make_colorschemes.vim | 5 +- scripts/solarized8.sh | 72 +++++++++++++++++++ templates/_fix-256-palette.colortemplate | 74 ++++++++++++++++++++ templates/solarized8.colortemplate | 4 ++ 5 files changed, 179 insertions(+), 13 deletions(-) rename {templates => scripts}/make_colorschemes.vim (85%) create mode 100644 scripts/solarized8.sh create mode 100644 templates/_fix-256-palette.colortemplate diff --git a/Readme.md b/Readme.md index 531787b..bfffd25 100644 --- a/Readme.md +++ b/Readme.md @@ -15,14 +15,18 @@ differently (for instance, I have tried to avoid red on blue). The main reason for the existence of this project is that the original Solarized theme does not define `guifg` and `guibg` in terminal Vim, making it unsuitable -for versions of Vim supporting true-color terminals. Instead, this color scheme -works **out of the box everywhere**. For the best experience, you need: +for versions of Vim supporting true-color (i.e., 24-bit color) terminals. +Instead, this colorscheme works **out of the box everywhere**. For the best +experience, you need: - Vim ≥7.4.1799, or NeoVim, with `termguicolors` set, **and** - [a true-color enabled terminal](https://gist.github.com/XVilka/8346728). Solarized 8 also works in MacVim, gVim, etc…: no configuration is needed. + +## But, my terminal has only 256 colors! + For terminals not supporting true colors, Solarized 8 will fall back to use an approximate palette based on xterm's 256 colors. This is a very crude approximation, which looks more or less like this: @@ -34,17 +38,28 @@ Dark 256 color palette | Light 256 color palette (Yeah, it looks like a different colorscheme.) If you don't like this approximation, to get exact colors with such terminals -you must set your terminal's 16 ANSI colors to the Solarized palette (refer to -the manual of your terminal) and put this line in your `vimrc` before loading -the colorscheme: +you have two possibilities: + +1. Modify some of your terminal colors in the range 16–255 to match Solarized + palette. For this purpose, you may run `scripts/solarized8.sh`; no setting is + needed in Vim. Note, however, that this is not supported by all terminals + (e.g., in macOS's Terminal.app this approach does not work: the script has no + effect). + +2. Set your terminal's 16 ANSI colors (the colors in the range 0–15) to the + Solarized palette. Most terminals allow you to do that: see the manual of + your terminal to learn how. Then, put this line in your `vimrc` before + loading the colorscheme: ```vim let g:solarized_use16 = 1 ``` -Be aware that, if you set the variable above, but your terminal does not use the -Solarized palette, your colors will be completely off. Note also that if your -terminal does not support true colors, you must *not* set `termguicolors`. + Be aware that, if you set the variable above, but your terminal does not use + the Solarized palette, your colors will be completely off. + +Finally, if your terminal does not support true colors, you must *not* set +`termguicolors`. ## Installation @@ -56,7 +71,7 @@ recommend that you use them. Just clone this repo inside `pack/*/opt`, e.g.: Otherwise, use your favourite installation method. -There are actually four optimized* color schemes: +There are actually four optimized* colorschemes: - `solarized8`: the default Solarized theme; - `solarized8_low`: low-contrast variant; @@ -135,8 +150,8 @@ See `:h xterm-true-color` for the details. Do you want to hack the theme? Install [Colortemplate](https://github.com/lifepillar/vim-colortemplate), edit the -`*.colortemplate` files, then rebuild the colorschemes by sourcing the script in -the `templates` folder. +`*.colortemplate` files, then rebuild the colorschemes by sourcing +`scripts/make_colorschemes.vim`. If you extend or improve Solarized 8, please consider submitting a pull request! diff --git a/templates/make_colorschemes.vim b/scripts/make_colorschemes.vim similarity index 85% rename from templates/make_colorschemes.vim rename to scripts/make_colorschemes.vim index d9f1674..e79831c 100644 --- a/templates/make_colorschemes.vim +++ b/scripts/make_colorschemes.vim @@ -5,11 +5,12 @@ let s:curdir = fnamemodify(resolve(expand(':p')), ':h') let s:parent = fnamemodify(s:curdir, ':h') +let s:templates = s:parent.'/templates' let s:errors = 0 -execute 'lcd' s:curdir +execute 'lcd' s:templates -for s:template in glob(s:curdir . '/solarized8*.colortemplate', 1, 1) +for s:template in glob(s:templates . '/solarized8*.colortemplate', 1, 1) let s:template_name = fnamemodify(s:template, ':t:r') if s:template_name == 'solarized8' let g:colortemplate_no_doc = 0 diff --git a/scripts/solarized8.sh b/scripts/solarized8.sh new file mode 100644 index 0000000..bed4259 --- /dev/null +++ b/scripts/solarized8.sh @@ -0,0 +1,72 @@ +#!/bin/sh +# Modify 256-color palette in supported terminals +# Script adapted from https://github.com/chriskempson/base16-shell +set -o errexit + +hex2rgb() { + local s=$1 + local r=${s:1:2} + local g=${s:3:2} + local b=${s:5:2} + echo "$r/$g/$b" +} + +if [ "${TERM%%-*}" = 'linux' ]; then + # This script doesn't support linux console + return 2>/dev/null || exit 0 +fi + +if [ -n "$TMUX" ]; then + # tell tmux to pass the escape sequences through + # (Source: http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324) + printf_template="\033Ptmux;\033\033]4;%d;rgb:%s\007\033\\" + printf_template_var="\033Ptmux;\033\033]%d;rgb:%s\007\033\\" + printf_template_custom="\033Ptmux;\033\033]%s%s\007\033\\" +elif [ "${TERM%%-*}" = "screen" ]; then + # GNU screen (screen, screen-256color, screen-256color-bce) + printf_template="\033P\033]4;%d;rgb:%s\007\033\\" + printf_template_var="\033P\033]%d;rgb:%s\007\033\\" + printf_template_custom="\033P\033]%s%s\007\033\\" +else + printf_template="\033]4;%d;rgb:%s\033\\" + printf_template_var="\033]%d;rgb:%s\033\\" + printf_template_custom="\033]%s%s\033\\" +fi + +printf $printf_template 235 $(hex2rgb "#002b36") +printf $printf_template 236 $(hex2rgb "#073642") +printf $printf_template 242 $(hex2rgb "#586e75") +printf $printf_template 66 $(hex2rgb "#657b83") +printf $printf_template 246 $(hex2rgb "#839496") +printf $printf_template 247 $(hex2rgb "#93a1a1") +printf $printf_template 254 $(hex2rgb "#eee8d5") +printf $printf_template 230 $(hex2rgb "#fdf6e3") +printf $printf_template 136 $(hex2rgb "#b58900") +printf $printf_template 166 $(hex2rgb "#cb4b16") +printf $printf_template 160 $(hex2rgb "#dc322f") +printf $printf_template 162 $(hex2rgb "#d33682") +printf $printf_template 61 $(hex2rgb "#6c71c4") +printf $printf_template 32 $(hex2rgb "#268bd2") +printf $printf_template 37 $(hex2rgb "#2aa198") +printf $printf_template 106 $(hex2rgb "#859900") + +# foreground / background / cursor color +# if [ -n "$ITERM_SESSION_ID" ]; then +# # iTerm2 proprietary escape codes +# printf $printf_template_custom Pg 93a1a1 # forground +# printf $printf_template_custom Ph 002b36 # background +# printf $printf_template_custom Pi 93a1a1 # bold color +# printf $printf_template_custom Pj 586e75 # selection color +# printf $printf_template_custom Pk 93a1a1 # selected text color +# printf $printf_template_custom Pl 93a1a1 # cursor +# printf $printf_template_custom Pm 002b36 # cursor text +# else +# printf $printf_template_var 10 $color_foreground +# printf $printf_template_var 11 $color_background +# printf $printf_template_var 12 $color_cursor +# fi + +unset printf_template +unset printf_template_var +unset printf_template_custom + diff --git a/templates/_fix-256-palette.colortemplate b/templates/_fix-256-palette.colortemplate new file mode 100644 index 0000000..a1a3007 --- /dev/null +++ b/templates/_fix-256-palette.colortemplate @@ -0,0 +1,74 @@ +auxfile scripts/@shortname.sh +#!/bin/sh +# Modify 256-color palette in supported terminals +# Script adapted from https://github.com/chriskempson/base16-shell +set -o errexit + +hex2rgb() { + local s=$1 + local r=${s:1:2} + local g=${s:3:2} + local b=${s:5:2} + echo "$r/$g/$b" +} + +if [ "${TERM%%-*}" = 'linux' ]; then + # This script doesn't support linux console + return 2>/dev/null || exit 0 +fi + +if [ -n "$TMUX" ]; then + # tell tmux to pass the escape sequences through + # (Source: http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324) + printf_template="\033Ptmux;\033\033]4;%d;rgb:%s\007\033\\" + printf_template_var="\033Ptmux;\033\033]%d;rgb:%s\007\033\\" + printf_template_custom="\033Ptmux;\033\033]%s%s\007\033\\" +elif [ "${TERM%%-*}" = "screen" ]; then + # GNU screen (screen, screen-256color, screen-256color-bce) + printf_template="\033P\033]4;%d;rgb:%s\007\033\\" + printf_template_var="\033P\033]%d;rgb:%s\007\033\\" + printf_template_custom="\033P\033]%s%s\007\033\\" +else + printf_template="\033]4;%d;rgb:%s\033\\" + printf_template_var="\033]%d;rgb:%s\033\\" + printf_template_custom="\033]%s%s\033\\" +fi + +printf $printf_template @term256base03 $(hex2rgb "@guibase03") +printf $printf_template @term256base02 $(hex2rgb "@guibase02") +printf $printf_template @term256base01 $(hex2rgb "@guibase01") +printf $printf_template @term256base00 $(hex2rgb "@guibase00") +printf $printf_template @term256base0 $(hex2rgb "@guibase0") +printf $printf_template @term256base1 $(hex2rgb "@guibase1") +printf $printf_template @term256base2 $(hex2rgb "@guibase2") +printf $printf_template @term256base3 $(hex2rgb "@guibase3") +printf $printf_template @term256yellow $(hex2rgb "@guiyellow") +printf $printf_template @term256orange $(hex2rgb "@guiorange") +printf $printf_template @term256red $(hex2rgb "@guired") +printf $printf_template @term256magenta $(hex2rgb "@guimagenta") +printf $printf_template @term256violet $(hex2rgb "@guiviolet") +printf $printf_template @term256blue $(hex2rgb "@guiblue") +printf $printf_template @term256cyan $(hex2rgb "@guicyan") +printf $printf_template @term256green $(hex2rgb "@guigreen") + +# foreground / background / cursor color +# if [ -n "$ITERM_SESSION_ID" ]; then +# # iTerm2 proprietary escape codes +# printf $printf_template_custom Pg 93a1a1 # forground +# printf $printf_template_custom Ph 002b36 # background +# printf $printf_template_custom Pi 93a1a1 # bold color +# printf $printf_template_custom Pj 586e75 # selection color +# printf $printf_template_custom Pk 93a1a1 # selected text color +# printf $printf_template_custom Pl 93a1a1 # cursor +# printf $printf_template_custom Pm 002b36 # cursor text +# else +# printf $printf_template_var 10 $color_foreground +# printf $printf_template_var 11 $color_background +# printf $printf_template_var 12 $color_cursor +# fi + +unset printf_template +unset printf_template_var +unset printf_template_custom +endauxfile + diff --git a/templates/solarized8.colortemplate b/templates/solarized8.colortemplate index c1eda9b..52d866d 100644 --- a/templates/solarized8.colortemplate +++ b/templates/solarized8.colortemplate @@ -40,6 +40,10 @@ Color: green #859900 ~ 2 Color: back #002b36 ~ 8 # }}} +# Fix 256-color palette in supported terminals {{{ +Include: _fix-256-palette.colortemplate +# }}} + # Default highlight groups {{{ verbatim if !has('gui_running') && get(g:, '@optionprefix_termtrans', 0)