Skip to content

Commit

Permalink
Merge pull request #104 from deviantfero/transparency-support
Browse files Browse the repository at this point in the history
Transparency support and refactor
  • Loading branch information
deviantfero authored Aug 25, 2018
2 parents 9554834 + c9a17e4 commit 61c505f
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 111 deletions.
9 changes: 8 additions & 1 deletion wpgtk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def read_args(args):
help="select a temporary backend",
const="list", nargs="?")

parser.add_argument("--alpha",
help="set a one time alpha value",
nargs=1)

parser.add_argument("--pywal",
help="list included pywal themes "
"or replace your current colorscheme with a "
Expand Down Expand Up @@ -150,6 +154,9 @@ def process_args(args):
if args.n:
config.wpgtk["set_wallpaper"] = "false"

if args.alpha:
config.wpgtk["alpha"] = args.alpha[0]

if args.m:
filename = random.choice(files.get_file_list())
themer.set_theme(filename, filename, args.r)
Expand All @@ -167,7 +174,7 @@ def process_args(args):
templates = files.get_file_list(config.OPT_DIR, False)
any(print(t) for t in templates if ".base" in t)
else:
files.show_files()
print("\n".join(files.get_file_list()))
exit(0)

if args.t:
Expand Down
119 changes: 69 additions & 50 deletions wpgtk/data/color.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import logging
import pywal
from subprocess import call
from subprocess import Popen
from random import shuffle
from os.path import join, isfile
from random import randint
Expand All @@ -11,19 +11,23 @@
from . import sample


def get_pywal_dict(filename):
image = pywal.image.get(join(config.WALL_DIR, filename))
def get_pywal_dict(wallpaper):
"""get the color dictionary of a given wallpaper"""
pywal.util.Color.alpha_num = config.wpgtk.get('alpha', '100')
image = pywal.image.get(join(config.WALL_DIR, wallpaper))

return pywal.colors.get(image,
backend=config.wpgtk.get('backend', 'wal'),
cache_dir=config.WALL_DIR)


def get_color_list(filename, json=False):

"""extract a list with 16 colors from a json or a pywal dict"""
is_new = not isfile(files.get_cache_path(filename))

theme = get_pywal_dict(filename) if not json\
else pywal.util.read_file_json(filename)
theme = pywal.util.read_file_json(filename) if json\
else get_pywal_dict(filename)

color_list = theme["color"] if "color" in theme \
else list(theme["colors"].values())

Expand All @@ -35,12 +39,16 @@ def get_color_list(filename, json=False):


def is_dark_theme(color_list):
"""compare brightness values to see if a color-scheme
is light or dark"""
fg_brightness = util.get_hls_val(color_list[7], 'light')
bg_brightness = util.get_hls_val(color_list[0], 'light')

return fg_brightness > bg_brightness


def shuffle_colors(colors):
"""shuffle a color list in gorups of 8"""
color_group = [[colors[i], colors[i + 8]] for i in range(1, 7)]
shuffle(color_group)

Expand All @@ -51,6 +59,7 @@ def shuffle_colors(colors):


def write_colors(img, color_list):
"""write changes to a cache file to persist customizations"""
color_dict = get_pywal_dict(img)

color_dict = pywal.colors.colors_to_dict(color_list, img)
Expand All @@ -64,10 +73,12 @@ def write_colors(img, color_list):

def change_colors(colors, which):
opt = which

if which in config.FILE_DIC:
which = config.FILE_DIC[which]

tmp_filename = which + '.base'
try:
tmp_filename = which + '.base'
with open(tmp_filename, 'r') as tmp_file:
first_line = tmp_file.readline()
tmp_file.seek(0)
Expand All @@ -76,125 +87,133 @@ def change_colors(colors, which):
if 'wpgtk-ignore' not in first_line:
for k, v in config.keywords.items():
tmp_data = tmp_data.replace(util.build_key(k), v)
for k, v in colors["wpgtk"].items():
tmp_data = tmp_data.replace(util.build_key(k), v.strip('#'))
for k, v in colors["colors"].items():
k = util.build_key(k).upper()
tmp_data = tmp_data.replace(k, v.strip('#'))

for k, v in {**colors["wpgtk"], **colors["colors"]}.items():
tmp_data = tmp_data.replace(util.build_key(k.upper()), v.strip('#'))

if colors['icons'] and opt == 'icon-step1':
for k, v in colors['icons'].items():
tmp_data = tmp_data.replace(k, v.replace('#', ''))
tmp_data = tmp_data.replace(k, v.strip('#'))

with open(which, 'w') as target_file:
logging.info("applying: %s" % opt.split('/').pop())
target_file.write(tmp_data)
logging.info("wrote: %s" % opt.split('/').pop())

except IOError as err:
except IOError:
logging.error("%s - base file does not exist" % opt)


def auto_adjust_colors(clist):
light = config.wpgtk.getboolean('light_theme', False)

bm = util.alter_brightness
alter_brightness = util.alter_brightness
get_hls_val = util.get_hls_val

added_sat = 0.25 if light else 0.1
sign = -1 if light else 1

if light == is_dark_theme(clist):
# convert dark to light or the other way around
sat_diff = -0.1 if light else 0.1
clist = [clist[0]] \
+ [util.alter_brightness(x, 0, sat_diff) for x in clist[1:7]] \
+ [alter_brightness(x, 0, sat_diff) for x in clist[1:7]] \
+ clist[7:]
clist[7], clist[0] = clist[0], clist[7]

comment = [bm(clist[0], sign * 20)]
fg = [bm(clist[7], sign * 60)]
comment = [alter_brightness(clist[0], sign * 20)]
fg = [alter_brightness(clist[7], sign * 60)]
clist = clist[:8] + comment \
+ [bm(x, sign * util.get_hls_val(x, 'light') * 0.3, added_sat)
+ [alter_brightness(x, sign * get_hls_val(x, 'light') * 0.3, added_sat)
for x in clist[1:7]] + fg

return clist


def prepare_icon_colors(colors):
def add_icon_colors(colors):
try:
glyph = util.alter_brightness(colors['wpgtk']['COLORIN'], -15)
file_current_glyph = open(config.FILE_DIC['icon-step1'], "r")
icon_dic = {}

for line in file_current_glyph:
if('glyphColorNew=' in line):
icon_dic['oldglyph'] = line.split('=')[1].strip('\n')
if('frontColorNew=' in line):
icon_dic['oldfront'] = line.split('=')[1].strip('\n')
if('backColorNew=' in line):
icon_dic['oldback'] = line.split('=')[1].strip('\n')
file_current_glyph.close()
with open(config.FILE_DIC['icon-step1'], "r") as icon_file:
for line in icon_file:
if('glyphColorNew=' in line):
icon_dic['oldglyph'] = line.split('=')[1].strip('\n')

if('frontColorNew=' in line):
icon_dic['oldfront'] = line.split('=')[1].strip('\n')

if('backColorNew=' in line):
icon_dic['oldback'] = line.split('=')[1].strip('\n')

icon_dic['newglyph'] = glyph
icon_dic['newfront'] = colors['wpgtk']['COLORACT']
icon_dic['newback'] = colors['wpgtk']['COLORIN']

return icon_dic

except IOError:
logging.error("icons - base file does not exists")
return


def change_templates(colors):
"""call change_colors on each custom template
installed or defined by the user"""
template_dir = config.FILE_DIC['templates']
fl = files.get_file_list(template_dir, images=False)
fl = list(filter(lambda x: '.base' in x, fl))
templates = files.get_file_list(template_dir, images=False)
templates = list(filter(lambda x: '.base' in x, templates))

try:
for word in fl:
original = word.split('.base', len(word)).pop(0)
for template in templates:
original = template.split('.base').pop(0)
change_colors(colors, join(template_dir, original))

except Exception as e:
logging.error(str(e))
logging.error('optional file ' + original, file=sys.stderr)


def split_active(hexc, is_dark_theme=True):
"""extract active and inactive colors from a given
hex color value"""
brightness = util.get_hls_val(hexc, 'light')

if is_dark_theme:
return [util.alter_brightness(hexc, brightness * -0.20),
util.alter_brightness(hexc, brightness * -0.45)]
return {"COLORACT": util.alter_brightness(hexc, brightness * -0.20),
"COLORIN": util.alter_brightness(hexc, brightness * -0.45)}
else:
return [util.alter_brightness(hexc, brightness * 0.30), hexc]
return {"COLORACT": util.alter_brightness(hexc, brightness * 0.30),
"COLORIN": hexc}


def prepare_colors(cdic):
wpcol = cdic['wpgtk'] = {}
cl = [cdic['colors']['color%s' % i] for i in range(16)]
def add_wpgtk_colors(cdic):
"""ensamble wpgtk color dictionary"""
index = config.wpgtk.getint("active")
index = index if index > 0 else randint(9, 14)

# getting base colors
if(config.wpgtk.getint('active') > 0):
print(config.wpgtk.getint('active'))
bc = cl[config.wpgtk.getint('active')]
else:
bc = cl[randint(9, 14)]
base_color = cdic['colors']['color%s' % index]

wpcol['COLORACT'], wpcol['COLORIN'] = split_active(bc, is_dark_theme(cl))
cdic['icons'] = prepare_icon_colors(cdic)
color_list = [cdic['colors']['color%s' % i] for i in range(16)]
cdic['wpgtk'] = split_active(base_color, is_dark_theme(color_list))
cdic['icons'] = add_icon_colors(cdic)

return cdic


def apply_colorscheme(colors):
colors = prepare_colors(colors)
colors = add_wpgtk_colors(colors)

if isfile(config.FILE_DIC['icon-step2']):
change_colors(colors, 'icon-step1')
call(config.FILE_DIC['icon-step2'])
Popen(config.FILE_DIC['icon-step2'])

change_templates(colors)

if config.wpgtk.getboolean('tint2'):
util.reload_tint2()

if config.wpgtk.getboolean('openbox'):
util.reload_openbox()

if config.wpgtk.getboolean('gtk'):
pywal.reload.gtk()
19 changes: 10 additions & 9 deletions wpgtk/data/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__version__ = '5.5.2'

options = None
settings = None
wpgtk = None
keywords = None

Expand All @@ -22,6 +22,7 @@
SHELL_DIR = os.path.join(WALL_DIR, "shell")
SCHEME_DIR = os.path.join(WALL_DIR, "schemes")
OPT_DIR = os.path.join(WPG_DIR, "templates")

RCC = [] # random color cache


Expand All @@ -33,20 +34,20 @@


def write_conf(config_path=CONF_FILE):
global options
global settings
with open(config_path, 'w') as config_file:
options.write(config_file)
settings.write(config_file)


def load_sections():
global options
global settings
global wpgtk
global keywords
options = configparser.ConfigParser()
options.optionxform = str
options.read(CONF_FILE)
wpgtk = options['wpgtk']
keywords = options['keywords']
settings = configparser.ConfigParser()
settings.optionxform = str
settings.read(CONF_FILE)
wpgtk = settings['wpgtk']
keywords = settings['keywords']


def init():
Expand Down
Loading

0 comments on commit 61c505f

Please sign in to comment.