Skip to content

Commit

Permalink
Utils: Generalize monotone generation method
Browse files Browse the repository at this point in the history
Side effect is that background highlights will look slightly different,
but I think it's negligible.
  • Loading branch information
jan-warchol committed Nov 19, 2019
1 parent e401528 commit c5d9447
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions utils/palettes/selenized_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def generate_palette(
accent_offset=0,
accent_l_spread=None,
br_accent_shift=None,
br_bg_extra_saturation=1.1
br_bg_extra_saturation=1.2
):
bg_l, bg_a, bg_b = background

Expand All @@ -34,28 +34,33 @@ def generate_palette(

### MONOTONES

# bright bg and fg are calculated using contrast. For some reason bright
# background looks washed out without increased saturation
br_bg_l = bg_l + direction*contrast/10
br_bg_a, br_bg_b = bg_a*br_bg_extra_saturation, bg_b*br_bg_extra_saturation
monotones = {
"bg": [bg_l, bg_a, bg_b],
"fg": [fg_l, fg_a, fg_b],
}

hi_bg_l = bg_l + direction*contrast/4
br_fg_l = fg_l + direction*contrast/5
# define additional monotones using contrast
# (0 - lightness like in bg, 1 - like in fg)
# For some reason bright background looks washed out without increased saturation
monotone_spec = {
"black": [1/10, br_bg_extra_saturation],
"br_black": [1/4, br_bg_extra_saturation],
"white": [5/8, 1],
"br_white": [1+1/5, 1],
}

# color used for comments and other secondary content; it's a weighted
# average of fg and bg
dim_fg_l = 3/8 * bg_l + 5/8 * fg_l
dim_fg_a = 3/8 * bg_a + 5/8 * fg_a
dim_fg_b = 3/8 * bg_b + 5/8 * fg_b
# use weighted average.
def expand_monotone(spec):
relative_lightness, extra_saturation = spec
l = bg_l + relative_lightness * (fg_l - bg_l)
fg_weight = min(relative_lightness, 1)
bg_weight = max(1 - relative_lightness, 0)
a = (fg_weight*fg_a + bg_weight*bg_a) * extra_saturation
b = (fg_weight*fg_b + bg_weight*bg_b) * extra_saturation
return [l, a, b]

monotones = {
"bg": [bg_l, bg_a, bg_b ],
"black": [br_bg_l, br_bg_a, br_bg_b ],
"br_black": [hi_bg_l, br_bg_a, br_bg_b ],
"fg": [fg_l, fg_a, fg_b ],
"white": [dim_fg_l, dim_fg_a, dim_fg_b],
"br_white": [br_fg_l, fg_a, fg_b ],
}
for name in monotone_spec:
monotones[name] = expand_monotone(monotone_spec[name])



Expand Down Expand Up @@ -116,7 +121,7 @@ def generate_palette(

# some debug
acc_bg_dists = [float(abs(accents[color][0]-bg_l)) for color in accents]
acc_hi_dists = [float(abs(accents[color][0]-hi_bg_l)) for color in accents]
acc_hi_dists = [float(abs(accents[color][0]-monotones["br_black"][0])) for color in accents]

print("""Foreground: {}
Background: {}
Expand All @@ -136,9 +141,9 @@ def generate_palette(
float(accent_base_l),
float(accent_base_l - accent_l_spread),

float(abs(fg_l-dim_fg_l)),
float(abs(hi_bg_l-bg_l)),
float(abs(hi_bg_l-dim_fg_l)),
float(abs(fg_l-monotones["white"][0])),
float(abs(monotones["br_black"][0]-bg_l)),
float(abs(monotones["br_black"][0]-monotones["white"][0])),
min(acc_hi_dists),
max(acc_hi_dists),
min(acc_bg_dists),
Expand Down

0 comments on commit c5d9447

Please sign in to comment.