summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordylan <dylan.araps@gmail.com>2020-01-23 10:33:47 +0200
committerGitHub <noreply@github.com>2020-01-23 10:33:47 +0200
commitca389ce114d73ea9f46f02b3e611fdc1fab26186 (patch)
tree2df7c17653b557a86ea57dae2847a6d3edfcc33c
parentc18f4014f969504deaeb692aa88660ee4cb10f3f (diff)
parentd4bd389b438df7bc4d3f302d44e23c9bbdaba8ec (diff)
Merge pull request #474 from AmitPr/master
Add ability to run functions on colors from templates
-rw-r--r--README.md4
-rw-r--r--pywal/export.py44
-rw-r--r--pywal/util.py24
3 files changed, 63 insertions, 9 deletions
diff --git a/README.md b/README.md
index 0e14f97..443cca3 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@
<img src="https://i.imgur.com/HhK3LDv.jpg" alt="img" align="right" width="400px">
-Pywal is a tool that generates a color palette from the dominant colors in an image. It then applies the colors system-wide and on-the-fly in all of your favourite programs.
+Pywal is a tool that generates a color palette from the dominant colors in an image. It then applies the colors system-wide and on-the-fly in all of your favourite programs.
There are currently 5 supported color generation backends, each providing a different palette of colors from each image. You're bound to find an appealing color-scheme.
@@ -21,4 +21,4 @@ The goal of Pywal was to be as out of the way as possible. It doesn't modify any
Terminal emulators and TTYs have their color-schemes updated in real-time with no delay. With minimal configuration this functionality can be extended to almost anything running on your system.
-### More: \[[Installation](https://github.com/dylanaraps/pywal/wiki/Installation)\] \[[Getting Started](https://github.com/dylanaraps/pywal/wiki/Getting-Started)\] \[[Customization](https://github.com/dylanaraps/pywal/wiki/Customization)\] \[[Wiki](https://github.com/dylanaraps/pywal/wiki)\] \[[Screenshots](https://www.reddit.com/r/unixporn/search?q=wal&restrict_sr=on&sort=relevance&t=all)\]
+### More: \[[Installation](https://github.com/dylanaraps/pywal/wiki/Installation)] \[[Getting Started](https://github.com/dylanaraps/pywal/wiki/Getting-Started)] \[[Customization](https://github.com/dylanaraps/pywal/wiki/Customization)] \[[Wiki](https://github.com/dylanaraps/pywal/wiki)] \[[Screenshots](https://www.reddit.com/r/unixporn/search?q=wal&restrict_sr=on&sort=relevance&t=all)]
diff --git a/pywal/export.py b/pywal/export.py
index 88c98a6..d9ff16d 100644
--- a/pywal/export.py
+++ b/pywal/export.py
@@ -3,22 +3,60 @@ Export colors in various formats.
"""
import logging
import os
+import re
-from .settings import CACHE_DIR, MODULE_DIR, CONF_DIR
from . import util
+from .settings import CACHE_DIR, CONF_DIR, MODULE_DIR
def template(colors, input_file, output_file=None):
"""Read template file, substitute markers and
save the file elsewhere."""
template_data = util.read_file_raw(input_file)
-
+ for i, l in enumerate(template_data):
+ for match in re.finditer(r"(?<=(?<!\{))(\{([^{}]+)\})(?=(?!\}))", l):
+ # Get the color, and the functions associated with it
+ cname, _, funcs = match.group(2).partition(".")
+ # Check that functions are needed for this color
+ if len(funcs) == 0:
+ continue
+ # Build up a string which will be replaced with the new color
+ replace_str = cname
+ # Color to be modified copied into new one
+ new_color = util.Color(colors[cname].hex_color)
+ # Execute each function to be done
+ for func in filter(None, funcs.split(")")):
+ # Get function name and arguments
+ func = func.split("(")
+ fname = func[0]
+ if fname[0] == '.':
+ fname = fname[1:]
+ if not hasattr(new_color, fname):
+ logging.error(
+ "Syntax error in template file '%s' on line '%s'",
+ input_file, i)
+ function = getattr(new_color, fname)
+
+ # If the function is callable, call it
+ if callable(function):
+ if len(func) > 1:
+ new_color = function(*func[1].split(","))
+ else:
+ new_color = function()
+ # string to replace generated colors
+ if func[0] != '.':
+ replace_str += "."
+ replace_str += "(".join(func) + ")"
+ # If the color was changed, replace with a unique identifier.
+ if new_color is not colors[cname]:
+ template_data[i] = l.replace(
+ replace_str, "color" + new_color.strip)
+ colors["color" + new_color.strip] = new_color
try:
template_data = "".join(template_data).format(**colors)
except ValueError:
logging.error("Syntax error in template file '%s'.", input_file)
return
-
util.save_file(template_data, output_file)
diff --git a/pywal/util.py b/pywal/util.py
index e4b146a..cd6628a 100644
--- a/pywal/util.py
+++ b/pywal/util.py
@@ -5,10 +5,11 @@ import colorsys
import json
import logging
import os
+import platform
+import re
import shutil
import subprocess
import sys
-import platform
class Color:
@@ -35,7 +36,7 @@ class Color:
def rgba(self):
"""Convert a hex color to rgba."""
return "rgba(%s,%s,%s,%s)" % (*hex_to_rgb(self.hex_color),
- int(self.alpha_num)/100)
+ int(self.alpha_num) / 100)
@property
def alpha(self):
@@ -57,6 +58,21 @@ class Color:
"""Strip '#' from color."""
return self.hex_color[1:]
+ def lighten(self, percent):
+ """Lighten color by percent"""
+ percent = float(re.sub(r'[\D\.]', '', str(percent)))
+ return Color(lighten_color(self.hex_color, percent / 100))
+
+ def darken(self, percent):
+ """Darken color by percent"""
+ percent = float(re.sub(r'[\D\.]', '', str(percent)))
+ return Color(darken_color(self.hex_color, percent / 100))
+
+ def saturate(self, percent):
+ """Saturate a color"""
+ percent = float(re.sub(r'[\D\.]', '', str(percent)))
+ return Color(saturate_color(self.hex_color, percent / 100))
+
def read_file(input_file):
"""Read data from a file and trim newlines."""
@@ -156,11 +172,11 @@ def blend_color(color, color2):
def saturate_color(color, amount):
"""Saturate a hex color."""
r, g, b = hex_to_rgb(color)
- r, g, b = [x/255.0 for x in (r, g, b)]
+ r, g, b = [x / 255.0 for x in (r, g, b)]
h, l, s = colorsys.rgb_to_hls(r, g, b)
s = amount
r, g, b = colorsys.hls_to_rgb(h, l, s)
- r, g, b = [x*255.0 for x in (r, g, b)]
+ r, g, b = [x * 255.0 for x in (r, g, b)]
return rgb_to_hex((int(r), int(g), int(b)))