From 3ffeed1cd4ed78b1183f648b3071f38e74b60f88 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:12:15 +0800 Subject: [PATCH 1/6] Fix flag colours and marked/suspended/buried colours not showing up --- src/addon/colors.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/addon/colors.py b/src/addon/colors.py index 7352a2f..46cfb4a 100644 --- a/src/addon/colors.py +++ b/src/addon/colors.py @@ -2,6 +2,7 @@ from anki.hooks import wrap import aqt +import aqt.browser import aqt.colors from aqt import gui_hooks, mw from aqt.webview import AnkiWebView @@ -13,6 +14,19 @@ conf = ConfigManager() +# Sourced from aqt.browser.table.backend_color_to_aqt_color +ARGB_ONLY_ENTRIES = ( + "STATE_MARKED", + "STATE_SUSPENDED", + "STATE_BURIED", + "FLAG_1", + "FLAG_2", + "FLAG_3", + "FLAG_4", + "FLAG_5", + "FLAG_6", + "FLAG_7" +) # ReColor Python Colors def recolor_python() -> None: @@ -41,6 +55,12 @@ def hex_with_alpha_to_rgba(hex_color: str) -> str: return hex_color +def hex_with_alpha_to_argb(hex_color: str) -> str: + # ARGB_ONLY_ENTRIES get passed directly into QColor's ctor, which doesn't take rgba + # but it does take ARGB hex, so convert rgba to argb (https://doc.qt.io/qt-6/qcolor.html#fromString) + return "#" + hex_color[-2:] + hex_color[1:-2] + + def replace_color( color_entries: Dict[str, List[str]], anki_name: str, @@ -50,8 +70,9 @@ def replace_color( addon_name = anki_name if (anki_color := getattr(aqt.colors, anki_name, None)) is not None: color_entry = color_entries[addon_name] - anki_color["light"] = hex_with_alpha_to_rgba(color_entry[1]) - anki_color["dark"] = hex_with_alpha_to_rgba(color_entry[2]) + color_map_fn = hex_with_alpha_to_rgba if anki_name not in ARGB_ONLY_ENTRIES else hex_with_alpha_to_argb + anki_color["light"] = color_map_fn(color_entry[1]) + anki_color["dark"] = color_map_fn(color_entry[2]) setattr(aqt.colors, anki_name, anki_color) From 50a7e69ad6e0143cf49ac427b5d5fceafc54d1e7 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:15:28 +0800 Subject: [PATCH 2/6] Remove redundant import --- src/addon/colors.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/addon/colors.py b/src/addon/colors.py index 46cfb4a..f53e3f5 100644 --- a/src/addon/colors.py +++ b/src/addon/colors.py @@ -2,7 +2,6 @@ from anki.hooks import wrap import aqt -import aqt.browser import aqt.colors from aqt import gui_hooks, mw from aqt.webview import AnkiWebView From 768982e921c8db342f53617c3473c2a9b673768b Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Sun, 8 Dec 2024 22:48:31 +0800 Subject: [PATCH 3/6] Use abs. indices instead of rel. in case rgb is passed in instead of rgba --- src/addon/colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/addon/colors.py b/src/addon/colors.py index f53e3f5..57635af 100644 --- a/src/addon/colors.py +++ b/src/addon/colors.py @@ -57,7 +57,7 @@ def hex_with_alpha_to_rgba(hex_color: str) -> str: def hex_with_alpha_to_argb(hex_color: str) -> str: # ARGB_ONLY_ENTRIES get passed directly into QColor's ctor, which doesn't take rgba # but it does take ARGB hex, so convert rgba to argb (https://doc.qt.io/qt-6/qcolor.html#fromString) - return "#" + hex_color[-2:] + hex_color[1:-2] + return "#" + hex_color[7:9] + hex_color[1:7] def replace_color( From 907bd5fa49f84a5f7f828e94c0d7076e101bb256 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:51:36 +0800 Subject: [PATCH 4/6] Added check for hex and support for rgba(...) --- src/addon/colors.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/addon/colors.py b/src/addon/colors.py index 57635af..60c8eab 100644 --- a/src/addon/colors.py +++ b/src/addon/colors.py @@ -1,4 +1,5 @@ from typing import Any, Optional, Tuple, List, Dict +import re from anki.hooks import wrap import aqt @@ -54,10 +55,23 @@ def hex_with_alpha_to_rgba(hex_color: str) -> str: return hex_color +RGBA_RE = re.compile(r"\((\d+),\s*(\d+),\s*(\d+),\s*(\d+\.*\d+?)\)\s*") + def hex_with_alpha_to_argb(hex_color: str) -> str: - # ARGB_ONLY_ENTRIES get passed directly into QColor's ctor, which doesn't take rgba - # but it does take ARGB hex, so convert rgba to argb (https://doc.qt.io/qt-6/qcolor.html#fromString) - return "#" + hex_color[7:9] + hex_color[1:7] + # ARGB_ONLY_ENTRIES get passed directly into a QColor, so they need to be of RGB hex format + # QColor also takes #ARGB, so prepend the alpha instead of removing it (https://doc.qt.io/qt-6/qcolor.html#fromString) + if hex_color.startswith("#") and len(hex_color) == 9: + # assume #RGBA, convert to #ARGB + return "#" + hex_color[7:9] + hex_color[1:7] + elif hex_color.startswith("rgba") and (m := re.match(RGBA_RE, hex_color[4:])): + # convert rgba(...) to #ARGB + r = int(m.group(1)) + g = int(m.group(2)) + b = int(m.group(3)) + a = int(255 * float(m.group(4))) + return f"#{a:02x}{r:0x}{g:02x}{b:02x}" + # fallback to original + return hex_color def replace_color( From 53a08044a4cb2f878f8bf452c060b02a036375c1 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Tue, 10 Dec 2024 08:54:58 +0800 Subject: [PATCH 5/6] Add missing format specifier --- src/addon/colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/addon/colors.py b/src/addon/colors.py index 60c8eab..d524306 100644 --- a/src/addon/colors.py +++ b/src/addon/colors.py @@ -69,7 +69,7 @@ def hex_with_alpha_to_argb(hex_color: str) -> str: g = int(m.group(2)) b = int(m.group(3)) a = int(255 * float(m.group(4))) - return f"#{a:02x}{r:0x}{g:02x}{b:02x}" + return f"#{a:02x}{r:02x}{g:02x}{b:02x}" # fallback to original return hex_color From 8376931acef4bd85b10624b69e4c0b17938c2811 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Sat, 14 Dec 2024 20:28:27 +0800 Subject: [PATCH 6/6] Handle ACCENT_CARD and ACCENT_NOTE --- src/addon/colors.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/addon/colors.py b/src/addon/colors.py index d524306..3d22840 100644 --- a/src/addon/colors.py +++ b/src/addon/colors.py @@ -15,6 +15,7 @@ conf = ConfigManager() # Sourced from aqt.browser.table.backend_color_to_aqt_color +# ACCENT_CARD and ACCENT_NOTE are passed to ThemeManager.qcolor, which doesn't support #RGBA ARGB_ONLY_ENTRIES = ( "STATE_MARKED", "STATE_SUSPENDED", @@ -25,7 +26,9 @@ "FLAG_4", "FLAG_5", "FLAG_6", - "FLAG_7" + "FLAG_7", + "ACCENT_CARD", + "ACCENT_NOTE" ) # ReColor Python Colors