Merge pull request #60 from dylanaraps/dark

colors: Darken bg if contrast is too low.
This commit is contained in:
Dylan Araps 2017-07-28 15:52:27 +10:00 committed by GitHub
commit 8a91a67087
3 changed files with 36 additions and 12 deletions

View File

@ -54,6 +54,10 @@ def sort_colors(img, colors):
we will later save in json format.""" we will later save in json format."""
raw_colors = colors[:1] + colors[9:] + colors[8:] raw_colors = colors[:1] + colors[9:] + colors[8:]
# Darken the background color if it's too light.
if int(raw_colors[0][1]) >= 3:
raw_colors[0] = util.darken_color(raw_colors[0], 0.25)
colors = {"wallpaper": img} colors = {"wallpaper": img}
colors_special = {} colors_special = {}

View File

@ -20,7 +20,8 @@ class Color:
@property @property
def rgb(self): def rgb(self):
"""Convert a hex color to rgb.""" """Convert a hex color to rgb."""
return hex_to_rgb(self.hex_color) red, green, blue = hex_to_rgb(self.hex_color)
return f"{red},{green},{blue}"
@property @property
def xrgba(self): def xrgba(self):
@ -99,8 +100,7 @@ def create_dir(directory):
def hex_to_rgb(color): def hex_to_rgb(color):
"""Convert a hex color to rgb.""" """Convert a hex color to rgb."""
red, green, blue = list(bytes.fromhex(color.strip("#"))) return tuple(bytes.fromhex(color.strip("#")))
return f"{red},{green},{blue}"
def hex_to_xrgba(color): def hex_to_xrgba(color):
@ -109,6 +109,16 @@ def hex_to_xrgba(color):
return f"{col[1]}{col[2]}/{col[3]}{col[4]}/{col[5]}{col[6]}/ff" return f"{col[1]}{col[2]}/{col[3]}{col[4]}/{col[5]}{col[6]}/ff"
def rgb_to_hex(color):
"""Convert an rgb color to hex."""
return f"#{color[0]:02x}{color[1]:02x}{color[2]:02x}"
def darken_color(color, darkness):
"""Darken a hex color."""
return rgb_to_hex([int(col * (1 - darkness)) for col in hex_to_rgb(color)])
def disown(*cmd): def disown(*cmd):
"""Call a system command in the background, """Call a system command in the background,
disown it and hide it's output.""" disown it and hide it's output."""

View File

@ -64,23 +64,33 @@ class TestUtil(unittest.TestCase):
def test_hex_to_rgb_black(self): def test_hex_to_rgb_black(self):
"""> Convert #000000 to RGB.""" """> Convert #000000 to RGB."""
result = util.hex_to_rgb("#000000") result = util.hex_to_rgb("#000000")
self.assertEqual(result, "0,0,0") self.assertEqual(result, (0, 0, 0))
def test_hex_to_rgb_white(self): def test_hex_to_rgb_white(self):
"""> Convert #FFFFFF to RGB.""" """> Convert #ffffff to RGB."""
result = util.hex_to_rgb("#FFFFFF") result = util.hex_to_rgb("#ffffff")
self.assertEqual(result, "255,255,255") self.assertEqual(result, (255, 255, 255))
def test_hex_to_rgb_rand(self): def test_hex_to_rgb_rand(self):
"""> Convert #98AEC2 to RGB.""" """> Convert #98aec2 to RGB."""
result = util.hex_to_rgb("#98AEC2") result = util.hex_to_rgb("#98aec2")
self.assertEqual(result, "152,174,194") self.assertEqual(result, (152, 174, 194))
def test_hex_to_xrgba(self): def test_hex_to_xrgba(self):
"""> Convert #98AEC2 to XRGBA.""" """> Convert #98aec2 to XRGBA."""
result = util.hex_to_xrgba("#98AEC2") result = util.hex_to_xrgba("#98aec2")
self.assertEqual(result, "98/ae/c2/ff") self.assertEqual(result, "98/ae/c2/ff")
def test_rgb_to_hex(self):
"""> Convert 152,174,194 to HEX."""
result = util.rgb_to_hex((152, 174, 194))
self.assertEqual(result, "#98aec2")
def test_darken_color(self):
"""> Darken #ffffff by 0.25."""
result = util.darken_color("#ffffff", 0.25)
self.assertEqual(result, "#bfbfbf")
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()