Merge pull request #16 from dylanaraps/json

colors: Store colors in a json format.
This commit is contained in:
Dylan Araps 2017-06-29 22:52:21 +10:00 committed by GitHub
commit 27e73e5bba
11 changed files with 162 additions and 76 deletions

View File

@ -90,7 +90,6 @@ def process_args(args):
# Create a list of hex colors.
colors_plain = gen_colors.get_colors(image, args.q)
colors_plain[8] = set_colors.set_grey(colors_plain)
if not args.n:
wallpaper.set_wallpaper(image)
@ -101,7 +100,7 @@ def process_args(args):
# -f
elif args.f:
colors_plain = util.read_file(args.f)
colors_plain = util.read_file_json(args.f)
set_colors.send_sequences(colors_plain, args.t)
export_colors.export_colors(colors_plain)

View File

@ -6,20 +6,21 @@ from pywal import util
def plain(colors):
"""Convert colors to plain hex."""
return [f"{color}\n" for color in colors]
return [f"{color}\n" for color in colors["colors"].values()]
def shell(colors):
"""Convert colors to shell variables."""
return [f"color{index}='{color}'\n"
for index, color in enumerate(colors)]
for index, color in enumerate(colors["colors"].values())]
def css(colors):
"""Convert colors to css variables."""
css_colors = [":root {\n"]
css_colors.extend([f"\t--color{index}: {color};\n"
for index, color in enumerate(colors)])
for index, color in
enumerate(colors["colors"].values())])
css_colors.append("}\n")
return css_colors
@ -27,7 +28,7 @@ def css(colors):
def scss(colors):
"""Convert colors to scss variables."""
return [f"$color{index}: {color};\n"
for index, color in enumerate(colors)]
for index, color in enumerate(colors["colors"].values())]
def putty(colors):
@ -38,7 +39,8 @@ def putty(colors):
"[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]\n",
]
putty_colors.extend([f"\"colour{index}\"=\"{rgb(color)}\"\n"
for index, color in enumerate(colors)])
for index, color in
enumerate(colors["colors"].values())])
return putty_colors
@ -46,33 +48,45 @@ def putty(colors):
def xrdb(colors):
"""Convert colors to xrdb format."""
x_colors = []
x_colors.append(f"URxvt*foreground: {colors[15]}\n")
x_colors.append(f"XTerm*foreground: {colors[15]}\n")
x_colors.append(f"URxvt*background: {colors[0]}\n")
x_colors.append(f"XTerm*background: {colors[0]}\n")
x_colors.append(f"URxvt*cursorColor: {colors[15]}\n")
x_colors.append(f"XTerm*cursorColor: {colors[15]}\n")
x_colors.append(f"URxvt*foreground: {colors['special']['foreground']}\n")
x_colors.append(f"XTerm*foreground: {colors['special']['foreground']}\n")
x_colors.append(f"URxvt*background: {colors['special']['background']}\n")
x_colors.append(f"XTerm*background: {colors['special']['background']}\n")
x_colors.append(f"URxvt*cursorColor: {colors['special']['cursor']}\n")
x_colors.append(f"XTerm*cursorColor: {colors['special']['cursor']}\n")
# Colors 0-15.
x_colors.extend([f"*.color{index}: {color}\n*color{index}: {color}\n"
for index, color in enumerate(colors)])
x_colors.extend([f"*.color{index}: {color}\n*color{index}: {color}\n"
for index, color in enumerate(colors["colors"].values())])
x_colors.append(f"*.color66: {colors[0]}\n*color66: {colors[0]}\n")
x_colors.append(f"*.color66: {colors['special']['background']}\n"
f"*color66: {colors['special']['background']}\n")
# Rofi colors.
x_colors.append(f"rofi.color-window: {colors[0]}, "
f"{colors[0]}, {colors[10]}\n")
x_colors.append(f"rofi.color-normal: {colors[0]}, "
f"{colors[15]}, {colors[0]}, "
f"{colors[10]}, {colors[0]}\n")
x_colors.append(f"rofi.color-active: {colors[0]}, "
f"{colors[15]}, {colors[0]}, "
f"{colors[10]}, {colors[0]}\n")
x_colors.append(f"rofi.color-urgent: {colors[0]}, "
f"{colors[9]}, {colors[0]}, "
f"{colors[9]}, {colors[15]}\n")
x_colors.append(f"rofi.color-window: "
f"{colors['special']['background']}, "
f"{colors['special']['background']}, "
f"{colors['colors']['color10']}\n")
x_colors.append(f"rofi.color-normal: "
f"{colors['special']['background']}, "
f"{colors['special']['foreground']}, "
f"{colors['special']['background']}, "
f"{colors['colors']['color10']}, "
f"{colors['special']['background']}\n")
x_colors.append(f"rofi.color-active: "
f"{colors['special']['background']}, "
f"{colors['special']['foreground']}, "
f"{colors['special']['background']}, "
f"{colors['colors']['color10']}, "
f"{colors['special']['background']}\n")
x_colors.append(f"rofi.color-urgent: "
f"{colors['special']['background']}, "
f"{colors['colors']['color9']}, "
f"{colors['special']['background']}, "
f"{colors['colors']['color9']}, "
f"{colors['special']['foreground']}\n")
# Emacs colors.
x_colors.append(f"emacs*background: {colors[0]}\n")
x_colors.append(f"emacs*foreground: {colors[15]}\n")
x_colors.append(f"emacs*background: {colors['special']['background']}\n")
x_colors.append(f"emacs*foreground: {colors['special']['foreground']}\n")
return x_colors

View File

@ -9,6 +9,7 @@ import shutil
import subprocess
from pywal.settings import CACHE_DIR, COLOR_COUNT
from pywal import set_colors
from pywal import util
@ -96,9 +97,10 @@ def get_colors(img, quiet):
# Cache the sequences file.
cache_file = pathlib.Path(CACHE_DIR / "schemes" / img.replace("/", "_"))
cache_file = pathlib.Path(cache_file.with_suffix(".json"))
if cache_file.is_file():
colors = util.read_file(cache_file)
colors = util.read_file_json(cache_file)
print("colors: Found cached colorscheme.")
else:
@ -111,7 +113,7 @@ def get_colors(img, quiet):
colors = sort_colors(colors)
# Cache the colorscheme.
util.save_file("\n".join(colors), cache_file)
util.save_file_json(colors, cache_file)
print("colors: Generated colorscheme")
if not quiet:
@ -121,5 +123,27 @@ def get_colors(img, quiet):
def sort_colors(colors):
"""Sort the generated colors."""
return colors[:1] + colors[9:] + colors[8:]
"""Sort the generated colors and store them in a dict that
we will later save in json format."""
raw_colors = colors[:1] + colors[9:] + colors[8:]
# Special colors.
colors_special = {}
colors_special.update({"background": raw_colors[0]})
colors_special.update({"foreground": raw_colors[15]})
colors_special.update({"cursor": raw_colors[15]})
# Colors 0-15
colors_hex = {}
[colors_hex.update({f"color{index}": color}) # pylint: disable=W0106
for index, color in enumerate(raw_colors)]
# Color 8
colors_hex["color8"] = set_colors.set_grey(raw_colors)
# Add the colors to a dict.
colors = {}
colors["special"] = colors_special
colors["colors"] = colors_hex
return colors

View File

@ -37,19 +37,26 @@ def set_grey(colors):
def send_sequences(colors, vte):
"""Send colors to all open terminals."""
sequences = [set_color(num, color) for num, color in enumerate(colors)]
sequences.append(set_special(10, colors[15]))
sequences.append(set_special(11, colors[0]))
sequences.append(set_special(12, colors[15]))
sequences.append(set_special(13, colors[15]))
sequences.append(set_special(14, colors[0]))
# Colors 0-15.
sequences = [set_color(num, color)
for num, color in enumerate(colors["colors"].values())]
# Special colors.
# http://pod.tst.eu/http://cvs.schmorp.de/rxvt-unicode/doc/rxvt.7.pod#XTerm_Operating_System_Commands
# 10 = foreground, 11 = background, 12 = cursor foregound
# 13 = mouse foreground
sequences.append(set_special(10, colors["special"]["foreground"]))
sequences.append(set_special(11, colors["special"]["background"]))
sequences.append(set_special(12, colors["special"]["cursor"]))
sequences.append(set_special(13, colors["special"]["cursor"]))
# Set a blank color that isn"t affected by bold highlighting.
sequences.append(set_color(66, colors[0]))
sequences.append(set_color(66, colors["special"]["background"]))
# This escape sequence doesn"t work in VTE terminals.
if not vte:
sequences.append(set_special(708, colors[0]))
sequences.append(set_special(708, colors["special"]["background"]))
# Get a list of terminals.
terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/")

View File

@ -1,6 +1,7 @@
"""
Misc helper functions.
"""
import json
import os
import pathlib
import subprocess
@ -13,12 +14,25 @@ def read_file(input_file):
return colors
def read_file_json(input_file):
"""Read colors from a json file."""
with open(input_file) as json_file:
colors = json.load(json_file)
return colors
def save_file(colors, export_file):
"""Write the colors to the file."""
with open(export_file, "w") as file:
file.write(colors)
def save_file_json(colors, export_file):
"""Write the colors to a json file."""
with open(export_file, "w") as file:
json.dump(colors, file, indent=4)
def create_dir(directory):
"""Alias to create the cache dir."""
pathlib.Path(directory).mkdir(parents=True, exist_ok=True)

View File

@ -7,7 +7,7 @@ from pywal import util
# Import colors.
COLORS = util.read_file("tests/test_files/test_file")
COLORS = util.read_file_json("tests/test_files/test_file.json")
class TestExportColors(unittest.TestCase):
@ -15,7 +15,7 @@ class TestExportColors(unittest.TestCase):
def test_save_colors(self):
"""> Export colors to a file."""
tmp_file = pathlib.Path("/tmp/test_file")
tmp_file = pathlib.Path("/tmp/test_file.json")
export_colors.save_colors(COLORS, tmp_file, "plain colors")
result = tmp_file.is_file()
self.assertTrue(result)

View File

@ -1,16 +1 @@
#363442
#99A3B1
#C5BDB6
#98AEC2
#A8B9C6
#96C4CF
#B7C5CC
#C9CFD0
#999999
#99A3B1
#C5BDB6
#98AEC2
#A8B9C6
#96C4CF
#B7C5CC
#C9CFD0
/home/dylan/Pictures/Wallpapers/1.jpg

View File

@ -0,0 +1,26 @@
{
"special": {
"background":"#3A5130",
"foreground":"#FAF9F5",
"cursor":"#FAF9F5"
},
"colors": {
"color0":"#3A5130",
"color1":"#E3A19D",
"color2":"#E1CEAE",
"color3":"#D6DDCC",
"color4":"#F1D2CB",
"color5":"#F5E9D6",
"color6":"#F9F0E5",
"color7":"#FAF9F5",
"color8":"#999999",
"color9":"#E3A19D",
"color10":"#E1CEAE",
"color11":"#D6DDCC",
"color12":"#F1D2CB",
"color13":"#F5E9D6",
"color14":"#F9F0E5",
"color15":"#FAF9F5"
}
}

View File

@ -6,7 +6,7 @@ from pywal import util
# Import colors.
COLORS = util.read_file("tests/test_files/test_file")
COLORS = util.read_file_json("tests/test_files/test_file.json")
class TestFormatColors(unittest.TestCase):
@ -15,32 +15,32 @@ class TestFormatColors(unittest.TestCase):
def test_plain(self):
"""> Convert colors to plain."""
result = format_colors.plain(COLORS)
self.assertEqual(result[0], "#363442\n")
self.assertEqual(result[0], "#3A5130\n")
def test_shell(self):
"""> Convert colors to shell variables."""
result = format_colors.shell(COLORS)
self.assertEqual(result[0], "color0='#363442'\n")
self.assertEqual(result[0], "color0='#3A5130'\n")
def test_css(self):
"""> Convert colors to css variables."""
result = format_colors.css(COLORS)
self.assertEqual(result[1], "\t--color0: #363442;\n")
self.assertEqual(result[1], "\t--color0: #3A5130;\n")
def test_scss(self):
"""> Convert colors to scss variables."""
result = format_colors.scss(COLORS)
self.assertEqual(result[0], "$color0: #363442;\n")
self.assertEqual(result[0], "$color0: #3A5130;\n")
def test_putty(self):
"""> Convert colors to putty theme."""
result = format_colors.putty(COLORS)
self.assertEqual(result[2], "\"colour0\"=\"54,52,66\"\n")
self.assertEqual(result[2], "\"colour0\"=\"58,81,48\"\n")
def test_xrdb(self):
"""> Convert colors to putty theme."""
result = format_colors.xrdb(COLORS)
self.assertEqual(result[6], "*.color0: #363442\n*color0: #363442\n")
self.assertEqual(result[6], "*.color0: #3A5130\n*color0: #3A5130\n")
if __name__ == "__main__":

View File

@ -6,7 +6,7 @@ from pywal import util
# Import colors.
COLORS = util.read_file("tests/test_files/test_file")
COLORS = util.read_file_json("tests/test_files/test_file.json")
class TestSetColors(unittest.TestCase):
@ -14,17 +14,18 @@ class TestSetColors(unittest.TestCase):
def test_set_special(self):
"""> Create special escape sequence."""
result = set_colors.set_special(11, COLORS[0])
self.assertEqual(result, "\x1b]11;#363442\x07")
result = set_colors.set_special(11, COLORS["special"]["background"])
self.assertEqual(result, "\x1b]11;#3A5130\x07")
def test_set_color(self):
"""> Create color escape sequence."""
result = set_colors.set_color(11, COLORS[0])
self.assertEqual(result, "\033]4;11;#363442\007")
result = set_colors.set_color(11, COLORS["colors"]["color0"])
self.assertEqual(result, "\033]4;11;#3A5130\007")
def test_set_grey(self):
"""> Create special escape sequence."""
result = set_colors.set_grey(COLORS)
colors = [list(COLORS["colors"].values())]
result = set_colors.set_grey(colors[0])
self.assertEqual(result, "#999999")

View File

@ -5,18 +5,27 @@ import pathlib
from pywal import util
# Import colors.
COLORS = util.read_file_json("tests/test_files/test_file.json")
class TestUtil(unittest.TestCase):
"""Test the util functions."""
def test_read_file_start(self):
def test_read_file(self):
"""> Read colors from a file."""
result = util.read_file("tests/test_files/test_file")
self.assertEqual(result[0], "#363442")
self.assertEqual(result[0], "/home/dylan/Pictures/Wallpapers/1.jpg")
def test_read_file_start(self):
"""> Read colors from a file."""
result = util.read_file_json("tests/test_files/test_file.json")
self.assertEqual(result["colors"]["color0"], "#3A5130")
def test_read_file_end(self):
"""> Read colors from a file."""
result = util.read_file("tests/test_files/test_file")
self.assertEqual(result[15], "#C9CFD0")
result = util.read_file_json("tests/test_files/test_file.json")
self.assertEqual(result["colors"]["color15"], "#FAF9F5")
def test_save_file(self):
"""> Save colors to a file."""
@ -25,6 +34,13 @@ class TestUtil(unittest.TestCase):
result = tmp_file.is_file()
self.assertTrue(result)
def test_save_file_json(self):
"""> Save colors to a file."""
tmp_file = pathlib.Path("/tmp/test_file.json")
util.save_file_json(COLORS, tmp_file)
result = tmp_file.is_file()
self.assertTrue(result)
def test_create_dir(self):
"""> Create a directoru."""
tmp_dir = pathlib.Path("/tmp/test_dir")