Added command line option --nine, which reverts to generating 9 colors, and alacritty output templates.

This commit is contained in:
Todd Sonjiku 2021-05-20 18:47:20 +03:00 committed by eylles
parent 2e04b0fdf3
commit d0855de1b8
10 changed files with 209 additions and 59 deletions

View File

@ -53,6 +53,9 @@ def get_args():
"flag is used: Go through the images in order " "flag is used: Go through the images in order "
"instead of shuffled.") "instead of shuffled.")
arg.add_argument("--nine", action="store_true",
help="Use 9 color output. ")
arg.add_argument("--recursive", action="store_true", arg.add_argument("--recursive", action="store_true",
help="When pywal is given a directory as input and this " help="When pywal is given a directory as input and this "
"flag is used: Search for images recursively in " "flag is used: Search for images recursively in "
@ -175,7 +178,7 @@ def parse_args(parser):
if args.i: if args.i:
image_file = image.get(args.i, iterative=args.iterative, image_file = image.get(args.i, iterative=args.iterative,
recursive=args.recursive) recursive=args.recursive)
colors_plain = colors.get(image_file, args.l, args.backend, colors_plain = colors.get(image_file, args.l, args.nine, args.backend,
sat=args.saturate) sat=args.saturate)
if args.theme: if args.theme:
@ -186,7 +189,7 @@ def parse_args(parser):
if args.w: if args.w:
cached_wallpaper = util.read_file(os.path.join(CACHE_DIR, "wal")) cached_wallpaper = util.read_file(os.path.join(CACHE_DIR, "wal"))
colors_plain = colors.get(cached_wallpaper[0], args.l, args.backend, colors_plain = colors.get(cached_wallpaper[0], args.l, args.nine, args.backend,
sat=args.saturate) sat=args.saturate)
if args.b: if args.b:

View File

@ -37,15 +37,15 @@ def gen_colors(img):
return [util.rgb_to_hex(color) for color in raw_colors] return [util.rgb_to_hex(color) for color in raw_colors]
def adjust(cols, light): def adjust(cols, light, nine):
"""Create palette.""" """Create palette."""
cols.sort(key=util.rgb_to_yiq) cols.sort(key=util.rgb_to_yiq)
raw_colors = [*cols, *cols] raw_colors = [*cols, *cols]
return colors.generic_adjust(raw_colors, light) return colors.generic_adjust(raw_colors, light, nine)
def get(img, light=False): def get(img, light=False, nine=False):
"""Get colorscheme.""" """Get colorscheme."""
cols = gen_colors(img) cols = gen_colors(img)
return adjust(cols, light) return adjust(cols, light, nine)

View File

@ -23,15 +23,15 @@ def gen_colors(img):
return [util.rgb_to_hex([*color[0]]) for color in raw_colors] return [util.rgb_to_hex([*color[0]]) for color in raw_colors]
def adjust(cols, light): def adjust(cols, light, nine):
"""Create palette.""" """Create palette."""
raw_colors = [cols[0], *cols, "#FFFFFF", raw_colors = [cols[0], *cols, "#FFFFFF",
"#000000", *cols, "#FFFFFF"] "#000000", *cols, "#FFFFFF"]
return colors.generic_adjust(raw_colors, light) return colors.generic_adjust(raw_colors, light, nine)
def get(img, light=False): def get(img, light=False, nine=False):
"""Get colorscheme.""" """Get colorscheme."""
cols = gen_colors(img) cols = gen_colors(img)
@ -40,4 +40,4 @@ def get(img, light=False):
logging.error("Try another backend or another image. (wal --backend)") logging.error("Try another backend or another image. (wal --backend)")
sys.exit(1) sys.exit(1)
return adjust(cols, light) return adjust(cols, light, nine)

View File

@ -22,16 +22,16 @@ def gen_colors(img):
return [util.rgb_to_hex(col[1]) for col in palette] return [util.rgb_to_hex(col[1]) for col in palette]
def adjust(cols, light): def adjust(cols, light, nine):
"""Create palette.""" """Create palette."""
cols.sort(key=util.rgb_to_yiq) cols.sort(key=util.rgb_to_yiq)
raw_colors = [*cols, *cols] raw_colors = [*cols, *cols]
raw_colors[0] = util.lighten_color(cols[0], 0.40) raw_colors[0] = util.lighten_color(cols[0], 0.40)
return colors.generic_adjust(raw_colors, light) return colors.generic_adjust(raw_colors, light, nine)
def get(img, light=False): def get(img, light=False, nine=False):
"""Get colorscheme.""" """Get colorscheme."""
cols = gen_colors(img) cols = gen_colors(img)
return adjust(cols, light) return adjust(cols, light, nine)

View File

@ -16,15 +16,15 @@ def gen_colors(img):
return subprocess.check_output([*cmd, img]).splitlines() return subprocess.check_output([*cmd, img]).splitlines()
def adjust(cols, light): def adjust(cols, light, nine):
"""Create palette.""" """Create palette."""
cols.sort(key=util.rgb_to_yiq) cols.sort(key=util.rgb_to_yiq)
raw_colors = [*cols[8:], *cols[8:]] raw_colors = [*cols[8:], *cols[8:]]
return colors.generic_adjust(raw_colors, light) return colors.generic_adjust(raw_colors, light, nine)
def get(img, light=False): def get(img, light=False, nine=False):
"""Get colorscheme.""" """Get colorscheme."""
if not shutil.which("schemer2"): if not shutil.which("schemer2"):
logging.error("Schemer2 wasn't found on your system.") logging.error("Schemer2 wasn't found on your system.")
@ -32,4 +32,4 @@ def get(img, light=False):
sys.exit(1) sys.exit(1)
cols = [col.decode('UTF-8') for col in gen_colors(img)] cols = [col.decode('UTF-8') for col in gen_colors(img)]
return adjust(cols, light) return adjust(cols, light, nine)

View File

@ -55,15 +55,15 @@ def gen_colors(img):
return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]] return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]]
def adjust(cols, light): def adjust(cols, light, nine):
"""Adjust the generated colors and store them in a dict that """Adjust the generated colors and store them in a dict that
we will later save in json format.""" we will later save in json format."""
raw_colors = cols[:1] + cols[8:16] + cols[8:-1] raw_colors = cols[:1] + cols[8:16] + cols[8:-1]
return colors.generic_adjust(raw_colors, light) return colors.generic_adjust(raw_colors, light, nine)
def get(img, light=False): def get(img, light=False, nine=False):
"""Get colorscheme.""" """Get colorscheme."""
colors = gen_colors(img) colors = gen_colors(img)
return adjust(colors, light) return adjust(colors, light, nine)

View File

@ -60,13 +60,20 @@ def colors_to_dict(colors, img):
} }
def generic_adjust(colors, light): def generic_adjust(colors, light, nine):
"""Generic color adjustment for themers.""" """Generic color adjustment for themers."""
if light: if light:
for color in colors: for color in colors:
color = util.saturate_color(color, 0.60) color = util.saturate_color(color, 0.60)
color = util.darken_color(color, 0.5) color = util.darken_color(color, 0.5)
if nine:
colors[0] = util.lighten_color(colors[0], 0.95)
colors[7] = util.darken_color(colors[0], 0.75)
colors[8] = util.darken_color(colors[0], 0.25)
colors[15] = colors[7]
else:
colors[0] = util.lighten_color(colors[0], 0.75) colors[0] = util.lighten_color(colors[0], 0.75)
colors[7] = util.darken_color(colors[0], 0.50) colors[7] = util.darken_color(colors[0], 0.50)
@ -79,6 +86,13 @@ def generic_adjust(colors, light):
colors[6] = util.darken_color(colors[6], 0.25) colors[6] = util.darken_color(colors[6], 0.25)
colors[15] = util.darken_color(colors[0], 0.75) colors[15] = util.darken_color(colors[0], 0.75)
else:
if nine:
colors[0] = util.darken_color(colors[0], 0.80)
colors[7] = util.lighten_color(colors[0], 0.75)
colors[8] = util.lighten_color(colors[0], 0.25)
colors[15] = colors[7]
else: else:
colors[0] = util.darken_color(colors[0], 0.75) colors[0] = util.darken_color(colors[0], 0.75)
@ -106,15 +120,16 @@ def saturate_colors(colors, amount):
return colors return colors
def cache_fname(img, backend, light, cache_dir, sat=""): def cache_fname(img, backend, nine, light, cache_dir, sat=""):
"""Create the cache file name.""" """Create the cache file name."""
color_type = "light" if light else "dark" color_type = "light" if light else "dark"
color_num = "9" if nine else "16"
file_name = re.sub("[/|\\|.]", "_", img) file_name = re.sub("[/|\\|.]", "_", img)
file_size = os.path.getsize(img) file_size = os.path.getsize(img)
file_parts = [file_name, color_type, backend, file_parts = [file_name, color_num, color_type, backend,
sat, file_size, __cache_version__] sat, file_size, __cache_version__]
return [cache_dir, "schemes", "%s_%s_%s_%s_%s_%s.json" % (*file_parts,)] return [cache_dir, "schemes", "%s_%s_%s_%s_%s_%s_%s.json" % (*file_parts,)]
def get_backend(backend): def get_backend(backend):
@ -141,10 +156,10 @@ def palette():
print("\n") print("\n")
def get(img, light=False, backend="wal", cache_dir=CACHE_DIR, sat=""): def get(img, light=False, nine=False, backend="wal", cache_dir=CACHE_DIR, sat=""):
"""Generate a palette.""" """Generate a palette."""
# home_dylan_img_jpg_backend_1.2.2.json # home_dylan_img_jpg_backend_1.2.2.json
cache_name = cache_fname(img, backend, light, cache_dir, sat) cache_name = cache_fname(img, backend, nine, light, cache_dir, sat)
cache_file = os.path.join(*cache_name) cache_file = os.path.join(*cache_name)
if os.path.isfile(cache_file): if os.path.isfile(cache_file):
@ -166,7 +181,7 @@ def get(img, light=False, backend="wal", cache_dir=CACHE_DIR, sat=""):
logging.info("Using %s backend.", backend) logging.info("Using %s backend.", backend)
backend = sys.modules["pywal.backends.%s" % backend] backend = sys.modules["pywal.backends.%s" % backend]
colors = getattr(backend, "get")(img, light) colors = getattr(backend, "get")(img, light, nine)
colors = colors_to_dict(saturate_colors(colors, sat), img) colors = colors_to_dict(saturate_colors(colors, sat), img)
util.save_file_json(colors, cache_file) util.save_file_json(colors, cache_file)

View File

@ -0,0 +1,53 @@
colors:
primary:
background: "{background}"
foreground: "{foreground}"
cursor:
text: CellBackground
cursor: CellForeground
vi_mode_cursor:
text: CellBackground
cursor: CellForeground
search:
matches:
foreground: '{color0}'
background: '{color15}'
focused_match:
foreground: CellBackground
background: CellForeground
bar:
foreground: '{color8}'
background: '{color7}'
line_indicator:
foreground: None
background: None
selection:
text: CellBackground
background: CellForeground
normal:
black: "{color0}"
red: "{color1}"
green: "{color2}"
yellow: "{color3}"
blue: "{color4}"
magenta: "{color5}"
cyan: "{color6}"
white: "{color7}"
bright:
black: "{color8}"
red: "{color9}"
green: "{color10}"
yellow: "{color11}"
blue: "{color12}"
magenta: "{color13}"
cyan: "{color14}"
white: "{color15}"

View File

@ -0,0 +1,63 @@
colors:
primary:
background: "{background}"
foreground: "{foreground}"
cursor:
text: CellBackground
cursor: CellForeground
vi_mode_cursor:
text: CellBackground
cursor: CellForeground
search:
matches:
foreground: '{color0}'
background: '{color15}'
focused_match:
foreground: CellBackground
background: CellForeground
bar:
foreground: '{color8}'
background: '{color7}'
line_indicator:
foreground: None
background: None
selection:
text: CellBackground
background: CellForeground
normal:
black: "{color0}"
red: "{color1}"
green: "{color2}"
yellow: "{color3}"
blue: "{color4}"
magenta: "{color5}"
cyan: "{color6}"
white: "{color7}"
bright:
black: "{color8}"
red: "{color9}"
green: "{color10}"
yellow: "{color11}"
blue: "{color12}"
magenta: "{color13}"
cyan: "{color14}"
white: "{color15}"
dim:
black: "{color15}"
red: "{color9}"
green: "{color13}"
yellow: "{color11}"
blue: "{color12}"
magenta: "{color10}"
cyan: "{color14}"
white: "{color8}"

View File

@ -16,6 +16,10 @@ def list_out():
for theme in list_themes()] for theme in list_themes()]
ligh_themes = [theme.name.replace(".json", "") ligh_themes = [theme.name.replace(".json", "")
for theme in list_themes(dark=False)] for theme in list_themes(dark=False)]
dark9_themes = [theme.name.replace(".json", "")
for theme in list_themes(nine=True)]
ligh9_themes = [theme.name.replace(".json", "")
for theme in list_themes(dark=False,nine=True)]
user_themes = [theme.name.replace(".json", "") user_themes = [theme.name.replace(".json", "")
for theme in list_themes_user()] for theme in list_themes_user()]
@ -33,8 +37,14 @@ def list_out():
print("\033[1;32mDark Themes\033[0m:") print("\033[1;32mDark Themes\033[0m:")
print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t
for t in sorted(dark_themes))) for t in sorted(dark_themes)))
print("\033[1;32mDark9 Themes\033[0m:")
print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t
for t in sorted(dark9_themes)))
print("\033[1;32mLight Themes\033[0m:") print("\033[1;32mLight Themes\033[0m:")
print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t
for t in sorted(ligh_themes)))
print("\033[1;32mLight9 Themes\033[0m:")
print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t
for t in sorted(ligh_themes))) for t in sorted(ligh_themes)))
@ -45,17 +55,20 @@ def list_out():
print(" - random_user (select a random user theme)") print(" - random_user (select a random user theme)")
def list_themes(dark=True): def list_themes(dark=True, nine=False):
"""List all installed theme files.""" """List all installed theme files."""
dark = "dark" if dark else "light" dark = "dark" if dark else "light"
themes = os.scandir(os.path.join(MODULE_DIR, "colorschemes", dark)) nine = "9" if nine else "16"
themes = os.scandir(os.path.join(MODULE_DIR, "colorschemes", dark, nine))
return [t for t in themes if os.path.isfile(t.path)] return [t for t in themes if os.path.isfile(t.path)]
def list_themes_user(): def list_themes_user():
"""List user theme files.""" """List user theme files."""
themes = [*os.scandir(os.path.join(CONF_DIR, "colorschemes/dark/")), themes = [*os.scandir(os.path.join(CONF_DIR, "colorschemes/dark/9")),
*os.scandir(os.path.join(CONF_DIR, "colorschemes/light/"))] *os.scandir(os.path.join(CONF_DIR, "colorschemes/dark/16")),
*os.scandir(os.path.join(CONF_DIR, "colorschemes/light/9")),
*os.scandir(os.path.join(CONF_DIR, "colorschemes/light/16"))]
return [t for t in themes if os.path.isfile(t.path)] return [t for t in themes if os.path.isfile(t.path)]
@ -91,7 +104,7 @@ def parse(theme_file):
return data return data
def get_random_theme(dark=True): def get_random_theme(dark=True, nine=False):
"""Get a random theme file.""" """Get a random theme file."""
themes = [theme.path for theme in list_themes(dark)] themes = [theme.path for theme in list_themes(dark)]
random.shuffle(themes) random.shuffle(themes)
@ -105,16 +118,17 @@ def get_random_theme_user():
return themes[0] return themes[0]
def file(input_file, light=False): def file(input_file, light=False, nine=False):
"""Import colorscheme from json file.""" """Import colorscheme from json file."""
util.create_dir(os.path.join(CONF_DIR, "colorschemes/light/")) util.create_dir(os.path.join(CONF_DIR, "colorschemes/light/"))
util.create_dir(os.path.join(CONF_DIR, "colorschemes/dark/")) util.create_dir(os.path.join(CONF_DIR, "colorschemes/dark/"))
theme_name = ".".join((input_file, "json")) theme_name = ".".join((input_file, "json"))
bri = "light" if light else "dark" bri = "light" if light else "dark"
full16 = "9" if nine else "16"
user_theme_file = os.path.join(CONF_DIR, "colorschemes", bri, theme_name) user_theme_file = os.path.join(CONF_DIR, "colorschemes", full16, bri, theme_name)
theme_file = os.path.join(MODULE_DIR, "colorschemes", bri, theme_name) theme_file = os.path.join(MODULE_DIR, "colorschemes", full16, bri, theme_name)
# Find the theme file. # Find the theme file.
if input_file in ("random", "random_dark"): if input_file in ("random", "random_dark"):
@ -146,9 +160,11 @@ def file(input_file, light=False):
sys.exit(1) sys.exit(1)
def save(colors, theme_name, light=False): def save(colors, theme_name, light=False, nine=False):
"""Save colors to a theme file.""" """Save colors to a theme file."""
theme_file = theme_name + ".json" theme_file = theme_name + ".json"
theme_path = os.path.join(CONF_DIR, "colorschemes", theme_path = os.path.join(CONF_DIR, "colorschemes",
"light" if light else "dark", theme_file) "9" if nine else "16",
"light" if light else "dark",
theme_file)
util.save_file_json(colors, theme_path) util.save_file_json(colors, theme_path)