From 19c29624133d1882d34c67d0493bb93392cf7c0e Mon Sep 17 00:00:00 2001 From: Georg Weber Date: Fri, 20 Sep 2019 13:01:24 +0200 Subject: [PATCH 1/4] Fix getOppositeColor The function getOppositeColor in color_compliment.py did not work properly. It was mixing value ranges from 0-1 and ranges from 0-255 --- powerline_shell/color_compliment.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/powerline_shell/color_compliment.py b/powerline_shell/color_compliment.py index f0517115..118008a0 100644 --- a/powerline_shell/color_compliment.py +++ b/powerline_shell/color_compliment.py @@ -10,21 +10,11 @@ def getOppositeColor(r,g,b): - r, g, b = [x/255.0 for x in [r, g, b]] # convert to float before getting hls value - hls = rgb_to_hls(r,g,b) - opp = list(hls[:]) - opp[0] = (opp[0]+0.2)%1 # shift hue (a.k.a. color) - if opp[1] > 255/2: # for level you want to make sure they - opp[1] -= 255/2 # are quite different so easily readable - else: - opp[1] += 255/2 - if opp[2] > -0.5: # if saturation is low on first color increase second's - opp[2] -= 0.5 - opp = hls_to_rgb(*opp) - m = max(opp) - if m > 255: #colorsys module doesn't give caps to their conversions - opp = [ x*254/m for x in opp] - return tuple([ int(x) for x in opp]) + """returns RGB components of complementary color""" + # colorsys functions expect values to be between 0 and 1 + hls = rgb_to_hls(*[x/255.0 for x in [r, g, b]]) # r,g,b are now between 0 and 1 + opp = hls_to_rgb(*[ (x+0.5)%1 for x in hls ]) + return tuple([ int(x*255) for x in opp ]) # convert back to value range 0-255 def stringToHashToColorAndOpposite(string): if py3: From 589b33c14ce3953cf7ce2cd3eb31bce84b33082c Mon Sep 17 00:00:00 2001 From: Georg Weber Date: Fri, 20 Sep 2019 13:02:10 +0200 Subject: [PATCH 2/4] Optimize dark theme colorize for hostname When hostname colorization is enabled, then a random number is being generated for the foreground color of the hostname and the opposite color is being calculated for the background color. When running in a shell with dark background color it typically looks better if the background color of the hostname is "bright". So I introduced an option in the configuration named "hostname.dark" that (when set to true) picks the brighter of the two colors for the hostname as the background color. --- powerline_shell/segments/hostname.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/powerline_shell/segments/hostname.py b/powerline_shell/segments/hostname.py index 894e139e..79546af9 100644 --- a/powerline_shell/segments/hostname.py +++ b/powerline_shell/segments/hostname.py @@ -10,6 +10,11 @@ def add_to_powerline(self): if powerline.segment_conf("hostname", "colorize"): hostname = gethostname() FG, BG = stringToHashToColorAndOpposite(hostname) + # if we operate on a dark background then + # we want the brighter color to always be the + # background color + if powerline.segment_conf("hostname", "dark") and sum(FG) > sum(BG): + FG,BG = BG,FG FG, BG = (rgb2short(*color) for color in [FG, BG]) host_prompt = " %s " % hostname.split(".")[0] powerline.append(host_prompt, FG, BG) From 02e2ee613b982bfd3308f3c6d6aa0fb577529015 Mon Sep 17 00:00:00 2001 From: Georg Weber Date: Fri, 20 Sep 2019 13:19:23 +0200 Subject: [PATCH 3/4] Add documentation for `dark` option into README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e6295711..73ded26d 100644 --- a/README.md +++ b/README.md @@ -299,11 +299,14 @@ The options for the `cwd` segment are: - `full_cwd`: If true, the last directory will not be shortened when `max_dir_size` is used. -The `hostname` segment provides one option: +The `hostname` segment provides two options: - `colorize`: If true, the hostname will be colorized based on a hash of itself. +- `dark`: If true (and colorize=true), a bright color will be used as the + background color + The `vcs` segment provides one option: - `show_symbol`: If `true`, the version control system segment will start with From 4a4184bb66f91537a24de7bb9d64f7220aa8faf4 Mon Sep 17 00:00:00 2001 From: George Date: Sat, 6 Mar 2021 16:40:43 +0100 Subject: [PATCH 4/4] Fix getOppositeColor The initial fix unnecessarily divided RGB values by 255 --- powerline_shell/color_compliment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerline_shell/color_compliment.py b/powerline_shell/color_compliment.py index 118008a0..16bbc3e3 100644 --- a/powerline_shell/color_compliment.py +++ b/powerline_shell/color_compliment.py @@ -12,7 +12,7 @@ def getOppositeColor(r,g,b): """returns RGB components of complementary color""" # colorsys functions expect values to be between 0 and 1 - hls = rgb_to_hls(*[x/255.0 for x in [r, g, b]]) # r,g,b are now between 0 and 1 + hls = rgb_to_hls(r, g, b) # r,g,b are now between 0 and 1 opp = hls_to_rgb(*[ (x+0.5)%1 for x in hls ]) return tuple([ int(x*255) for x in opp ]) # convert back to value range 0-255