backend: Add Colorz

This commit is contained in:
Dylan Araps 2018-03-31 10:30:11 +11:00
parent 6430724400
commit 37bc93cc03
5 changed files with 88 additions and 29 deletions

View File

@ -10,9 +10,11 @@ Created by Dylan Araps.
"""
from . import colorthief
from . import colorz
from . import wal
__all__ = [
"colorthief",
"colorz",
"wal",
]

View File

@ -9,31 +9,6 @@ from .. import util
from ..settings import COLOR_COUNT
def adjust(img, colors):
"""Create palette."""
raw_colors = colors[:1] + colors[8:16] + colors[8:-1]
# Modify colors to make a better scheme.
raw_colors[0] = util.darken_color(colors[0], 0.80)
raw_colors[15] = util.lighten_color(colors[15], 0.80)
raw_colors[7] = raw_colors[15]
colors = {"wallpaper": img, "alpha": util.Color.alpha_num,
"special": {}, "colors": {}}
for i, color in enumerate(raw_colors):
colors["colors"]["color%s" % i] = util.lighten_color(color, 0.25)
raw_colors[8] = util.blend_color(raw_colors[0], raw_colors[15])
colors["colors"]["color0"] = raw_colors[0]
colors["special"]["background"] = raw_colors[0]
colors["special"]["foreground"] = raw_colors[15]
colors["special"]["cursor"] = raw_colors[1]
return colors
def gen_colors(img, color_count):
"""Loop until 16 colors are generated."""
color_thief = ColorThief(img)
@ -58,7 +33,36 @@ def gen_colors(img, color_count):
return [util.rgb_to_hex(color) for color in raw_colors]
def adjust(img, colors, light):
"""Create palette."""
if light:
print("colors: Colortheif backend doesn't support light themes.")
raw_colors = colors[:1] + colors[8:16] + colors[8:-1]
raw_colors[0] = util.darken_color(colors[0], 0.80)
raw_colors[15] = util.lighten_color(colors[15], 0.80)
raw_colors[7] = raw_colors[15]
colors = {"wallpaper": img,
"alpha": util.Color.alpha_num,
"special": {},
"colors": {}}
for i, color in enumerate(raw_colors):
colors["colors"]["color%s" % i] = util.lighten_color(color, 0.25)
raw_colors[8] = util.blend_color(raw_colors[0], raw_colors[15])
colors["colors"]["color0"] = raw_colors[0]
colors["special"]["background"] = raw_colors[0]
colors["special"]["foreground"] = raw_colors[15]
colors["special"]["cursor"] = raw_colors[1]
return colors
def get(img, color_count=COLOR_COUNT, light=False):
"""Get colorscheme."""
colors = gen_colors(img, color_count)
return adjust(img, colors)
return adjust(img, colors, light)

49
pywal/backends/colorz.py Normal file
View File

@ -0,0 +1,49 @@
"""
Generate a colorscheme using Colorz.
"""
import subprocess
from .. import util
from ..settings import COLOR_COUNT
def gen_colors(img, color_count):
"""Generate a colorscheme using Colorz."""
flags = ["-n", "6", "--bold", "0", "--no-preview"]
return subprocess.check_output(["colorz", *flags, img]).splitlines()
def adjust(img, colors, light):
"""Create palette."""
if light:
print("colors: Colorz backend doesn't support light themes.")
raw_colors = ["#000000", *colors, "#FFFFFF",
"#333333", *colors, "#FFFFFF"]
raw_colors[0] = util.darken_color(colors[0], 0.75)
raw_colors[8] = util.darken_color(colors[0], 0.25)
raw_colors[7] = util.lighten_color(colors[0], 0.75)
raw_colors[15] = raw_colors[7]
colors = {"wallpaper": img,
"alpha": util.Color.alpha_num,
"special": {},
"colors": {}}
for i, color in enumerate(raw_colors):
colors["colors"]["color%s" % i] = color
colors["special"]["background"] = raw_colors[0]
colors["special"]["foreground"] = raw_colors[15]
colors["special"]["cursor"] = raw_colors[1]
return colors
def get(img, color_count=COLOR_COUNT, light=False):
"""Get colorscheme."""
colors = gen_colors(img, color_count)
colors = [color.decode('UTF-8').split()[0] for color in colors]
return adjust(img, colors, light)

View File

@ -57,7 +57,7 @@ def gen_colors(img, color_count):
def adjust(img, colors, light):
"""Sort 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."""
raw_colors = colors[:1] + colors[8:16] + colors[8:-1]
@ -78,8 +78,11 @@ def adjust(img, colors, light):
raw_colors[8] = util.darken_color(raw_colors[7], 0.30)
raw_colors[15] = util.blend_color(raw_colors[15], "#EEEEEE")
colors = {"wallpaper": img, "alpha": util.Color.alpha_num,
"special": {}, "colors": {}}
colors = {"wallpaper": img,
"alpha": util.Color.alpha_num,
"special": {},
"colors": {}}
colors["special"]["background"] = raw_colors[0]
colors["special"]["foreground"] = raw_colors[15]
colors["special"]["cursor"] = raw_colors[15]

View File

@ -13,6 +13,7 @@ def get(backend_type="wal"):
"""Get backend function name from name."""
return {
"colorthief": backends.colorthief.get,
"colorz": backends.colorz.get,
"wal": backends.wal.get,
}.get(backend_type, backend_type)