sample_prompts/disco: Actually use luminance for dir color

Instead of weirdly smearing the color, simply increase the values
until they are bright enough.

This prevents /tmp from being white, and guarantees visible colors for
all directories.
This commit is contained in:
Fabian Homborg 2022-01-05 18:11:35 +01:00
parent a7b4fc3845
commit 41da16408a

View file

@ -19,14 +19,17 @@ function fish_prompt
# We hash the physical PWD and turn that into a color. That means directories (usually) get different colors,
# but every directory always gets the same color. It's deterministic.
# We use cksum because 1. it's fast, 2. it's in POSIX, so it should be available everywhere.
set -l shas (pwd -P | cksum | string split -f1 ' ' | math --base=hex | string sub -s 3 | string match -ra ..) 0 0 0
# We get a fourth byte, just distribute it among the others.
# Also increase the value a bit so we don't get super dark colors.
# Really we want some contrast to the background (assuming black).
set -l smear 0x30
set -q shas[4]
and set smear (math 0x$shas[4] / 3 + 0x30)
set -l col (for f in $shas[1..3]; math --base=hex "min(255, 0x$f + $smear)"; end | string replace 0x '' | string pad -c 0 -w 2 | string join "")
set -l shas (pwd -P | cksum | string split -f1 ' ' | math --base=hex | string sub -s 3 | string match -ra ..)
set -l col 0x$shas[1..3]
# If the (simplified idea of) luminance is below 120 (out of 255), add some more.
# (this runs at most twice because we add 60)
while test (math 0.2126 x $col[1] + 0.7152 x $col[2] + 0.0722 x $col[3]) -lt 120
set col[1] (math --base=hex "min(255, $col[1] + 60)")
set col[2] (math --base=hex "min(255, $col[2] + 60)")
set col[3] (math --base=hex "min(255, $col[3] + 60)")
end
set -l col (string replace 0x '' $col | string pad -c 0 -w 2 | string join "")
set cwd (set_color $col)
end