xiwal

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

commit
2360c3c880fe6945dedc39ab1952f7ecee89070b
parent
96a204aedf54cde3e9772c04032c63d7a3f9aefb
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2021-01-20 22:08
implement oklab color space

Diffstat

M xiwal/lch.py 64 ++++++++++++++++++++++++++++++-------------------------------

1 files changed, 31 insertions, 33 deletions


diff --git a/xiwal/lch.py b/xiwal/lch.py

@@ -1,6 +1,9 @@
    1    -1 import math
   -1     1 """Oklab color model.
   -1     2 
   -1     3 https://bottosson.github.io/posts/oklab/
   -1     4 """
    2     5 
    3    -1 WHITE = (95.05, 100, 108.9)
   -1     6 import math
    4     7 
    5     8 
    6     9 def _srgb2rgb(c):
@@ -9,11 +12,10 @@ def _srgb2rgb(c):
    9    12 		c = c / 12.92
   10    13 	else:
   11    14 		c = ((c + 0.055) / 1.055) ** 2.4
   12    -1 	return c * 100
   -1    15 	return c
   13    16 
   14    17 
   15    18 def _rgb2srgb(c):
   16    -1 	c = c / 100.0
   17    19 	if c <= 0.0031308:
   18    20 		c = c * 12.92
   19    21 	else:
@@ -21,46 +23,42 @@ def _rgb2srgb(c):
   21    23 	return c * 255
   22    24 
   23    25 
   24    -1 def _xyz2lab(t):
   25    -1 	if t > 216.0 / 24389:
   26    -1 		return t ** (1.0 / 3)
   27    -1 	else:
   28    -1 		return 841.0 / 108 * t + 4.0 / 29
   29    -1 
   30    -1 
   31    -1 def _lab2xyz(t):
   32    -1 	if t > 6.0 / 29:
   33    -1 		return t ** 3
   34    -1 	else:
   35    -1 		return 108.0 / 841 * (t - 4.0 / 29)
   36    -1 
   37    -1 
   38    26 def rgb2lab(rgb):
   39    27 	r, g, b = map(_srgb2rgb, rgb)
   40    28 
   41    -1 	x = _xyz2lab((0.4124 * r + 0.3576 * g + 0.1805 * b) / WHITE[0])
   42    -1 	y = _xyz2lab((0.2126 * r + 0.7152 * g + 0.0722 * b) / WHITE[1])
   43    -1 	z = _xyz2lab((0.0193 * r + 0.1192 * g + 0.9505 * b) / WHITE[2])
   -1    29 	l = 0.4121656120 * r + 0.5362752080 * g + 0.0514575653 * b
   -1    30 	m = 0.2118591070 * r + 0.6807189584 * g + 0.1074065790 * b
   -1    31 	s = 0.0883097947 * r + 0.2818474174 * g + 0.6302613616 * b
   44    32 
   45    -1 	l = 116 * y - 16
   46    -1 	a = 500 * (x - y)
   47    -1 	b = 200 * (y - z)
   -1    33 	l_ = l ** (1 / 3)
   -1    34 	m_ = m ** (1 / 3)
   -1    35 	s_ = s ** (1 / 3)
   48    36 
   49    -1 	return l, a, b
   -1    37 	L = 0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_
   -1    38 	a = 1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_
   -1    39 	b = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_
   -1    40 
   -1    41 	return L, a, b
   50    42 
   51    43 
   52    44 def lab2rgb(lab):
   53    -1 	l, a, b = lab
   -1    45 	L, a, b = lab
   -1    46 
   -1    47 	l_ = L + 0.3963377774 * a + 0.2158037573 * b
   -1    48 	m_ = L - 0.1055613458 * a - 0.0638541728 * b
   -1    49 	s_ = L - 0.0894841775 * a - 1.2914855480 * b
   54    50 
   55    -1 	l = (l + 16) / 116.0
   -1    51 	l = l_ ** 3
   -1    52 	m = m_ ** 3
   -1    53 	s = s_ ** 3
   56    54 
   57    -1 	x = WHITE[0] * _lab2xyz(l + a / 500)
   58    -1 	y = WHITE[1] * _lab2xyz(l)
   59    -1 	z = WHITE[2] * _lab2xyz(l - b / 200)
   -1    55 	r = +4.0767245293 * l - 3.3072168827 * m + 0.2307590544 * s
   -1    56 	g = -1.2681437731 * l + 2.6093323231 * m - 0.3411344290 * s
   -1    57 	b = -0.0041119885 * l - 0.7034763098 * m + 1.7068625689 * s
   60    58 
   61    -1 	r =  3.2406 * x - 1.5372 * y - 0.4986 * z
   62    -1 	g = -0.9689 * x + 1.8758 * y + 0.0415 * z
   63    -1 	b =  0.0557 * x - 0.2040 * y + 1.0570 * z
   -1    59 	r = max(0, min(1, r))
   -1    60 	g = max(0, min(1, g))
   -1    61 	b = max(0, min(1, b))
   64    62 
   65    63 	return tuple(map(_rgb2srgb, (r, g, b)))
   66    64