xiwal

Generate terminal color schemes
git clone https://git.ce9e.org/xiwal.git

commit
0e6c8e53a21b43683bd265dc86e34456df038552
parent
d271ac3429f10a91feb772d23ce84ad6bf1260e8
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-11-15 09:22
perf: cache some values in score calculation

Diffstat

M wal/scheme.py 31 ++++++++++++++++++-------------

1 files changed, 18 insertions, 13 deletions


diff --git a/wal/scheme.py b/wal/scheme.py

@@ -1,4 +1,5 @@
    1     1 import math
   -1     2 import functools
    2     3 
    3     4 from . import lch
    4     5 
@@ -33,24 +34,28 @@ def permutate(a, n):
   33    34 				yield (a[i], *rest)
   34    35 
   35    36 
   -1    37 @functools.lru_cache(maxsize=32)
   -1    38 def distance(color, i):
   -1    39 	hue = math.pi / 3 * i + OFFSET
   -1    40 	d = abs(color[2] - hue)
   -1    41 	if d > math.pi:
   -1    42 		d = 2 * math.pi - d
   -1    43 
   -1    44 	c = color[1]
   -1    45 	if i in [0, 1]:
   -1    46 		c = max(c, C_RG)
   -1    47 
   -1    48 	return d ** 4 * c, c
   -1    49 
   -1    50 
   36    51 def score(colors):
   37    52 	sum_score = 0
   38    53 	sum_chroma = 0
   39    54 
   40    55 	for i, color in enumerate(colors):
   41    -1 		j = ORDER[i]
   42    -1 
   43    -1 		hue = math.pi / 3 * j + OFFSET
   44    -1 		d = abs(color[2] - hue)
   45    -1 		if d > math.pi:
   46    -1 			d = 2 * math.pi - d
   47    -1 
   48    -1 		c = color[1]
   49    -1 		if j in [0, 1]:
   50    -1 			c = max(c, C_RG)
   51    -1 
   52    -1 		sum_score += d ** 4 * c
   53    -1 		sum_chroma += c
   -1    56 		score, chroma = distance(color, ORDER[i])
   -1    57 		sum_score += score
   -1    58 		sum_chroma += chroma
   54    59 
   55    60 	return sum_score / sum_chroma
   56    61