summaryrefslogtreecommitdiff
path: root/poezio/colors.py
diff options
context:
space:
mode:
authorMaxime Buquet <pep@bouah.net>2019-02-14 17:52:30 +0100
committerMaxime Buquet <pep@bouah.net>2019-02-14 17:52:30 +0100
commit4e231185f5569a3caf8acd347cd5d7e652ab0ce4 (patch)
treebc5639229e8743b21dd7ca9e8a367a8af037d324 /poezio/colors.py
parentfa2f6a62ed2db069256a75a5f9f08e0306bf4673 (diff)
parent322f02f0f4ff7b2fc622e0a08f0363d806dc0bae (diff)
downloadpoezio-4e231185f5569a3caf8acd347cd5d7e652ab0ce4.tar.gz
poezio-4e231185f5569a3caf8acd347cd5d7e652ab0ce4.tar.bz2
poezio-4e231185f5569a3caf8acd347cd5d7e652ab0ce4.tar.xz
poezio-4e231185f5569a3caf8acd347cd5d7e652ab0ce4.zip
Merge branch 'feature/xep-0392-0.5' into 'master'
Bring XEP-0392 implementation to XEP version 0.5 See merge request poezio/poezio!13
Diffstat (limited to 'poezio/colors.py')
-rw-r--r--poezio/colors.py39
1 files changed, 8 insertions, 31 deletions
diff --git a/poezio/colors.py b/poezio/colors.py
index 6bbbb12e..c1019145 100644
--- a/poezio/colors.py
+++ b/poezio/colors.py
@@ -3,6 +3,8 @@ import curses
import hashlib
import math
+from . import hsluv
+
Palette = Dict[float, int]
# BT.601 (YCbCr) constants, see XEP-0392
@@ -33,13 +35,6 @@ def ncurses_color_to_rgb(color: int) -> Tuple[float, float, float]:
return r / 5, g / 5, b / 5
-def rgb_to_ycbcr(r: float, g: float, b: float) -> Tuple[float, float, float]:
- y = K_R * r + K_G * g + K_B * b
- cr = (r - y) / (1 - K_R) / 2
- cb = (b - y) / (1 - K_B) / 2
- return y, cb, cr
-
-
def generate_ccg_palette(curses_palette: List[int],
reference_y: float) -> Palette:
cbcr_palette = {} # type: Dict[float, Tuple[float, int]]
@@ -48,8 +43,10 @@ def generate_ccg_palette(curses_palette: List[int],
# drop grayscale
if r == g == b:
continue
- y, cb, cr = rgb_to_ycbcr(r, g, b)
- key = round(cbcr_to_angle(cb, cr), 2)
+ h, _, y = hsluv.rgb_to_hsluv((r, g, b))
+ # this is to keep the code compatible with earlier versions of XEP-0392
+ y = y / 100
+ key = round(h)
try:
existing_y, *_ = cbcr_palette[key]
except KeyError:
@@ -68,35 +65,15 @@ def text_to_angle(text: str) -> float:
hf = hashlib.sha1()
hf.update(text.encode("utf-8"))
hue = int.from_bytes(hf.digest()[:2], "little")
- return hue / 65535 * math.pi * 2
-
-
-def angle_to_cbcr_edge(angle: float) -> Tuple[float, float]:
- cr = math.sin(angle)
- cb = math.cos(angle)
- if abs(cr) > abs(cb):
- factor = 0.5 / abs(cr)
- else:
- factor = 0.5 / abs(cb)
- return cb * factor, cr * factor
-
-
-def cbcr_to_angle(cb: float, cr: float) -> float:
- magn = math.sqrt(cb**2 + cr**2)
- if magn > 0:
- cr /= magn
- cb /= magn
- return math.atan2(cr, cb) % (2 * math.pi)
+ return hue / 65535 * 360
def ccg_palette_lookup(palette: Palette, angle: float) -> int:
# try quick lookup first
try:
- color = palette[round(angle, 2)]
+ return palette[round(angle)]
except KeyError:
pass
- else:
- return color
best_metric = float("inf")
best = None