From 7e576941ca5382ca4b5737fc0b45d33ddf9fe620 Mon Sep 17 00:00:00 2001 From: Jonas Wielicki Date: Thu, 9 Nov 2017 09:05:09 +0100 Subject: Add support for XEP-0392 (Consistent Color Generation) --- poezio/theming.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'poezio/theming.py') diff --git a/poezio/theming.py b/poezio/theming.py index 49f838d2..4c93d396 100755 --- a/poezio/theming.py +++ b/poezio/theming.py @@ -75,6 +75,7 @@ import curses import functools import os from os import path +from poezio import colors from importlib import machinery finder = machinery.PathFinder() @@ -363,6 +364,10 @@ class Theme(object): 'default': (7, -1), } + @property + def ccg_palette(self): + prepare_ccolor_palette(self) + return self.CCG_PALETTE # This is the default theme object, used if no theme is defined in the conf theme = Theme() @@ -508,6 +513,30 @@ def update_themes_dir(option=None, value=None): log.debug('Theme load path: %s', load_path) +def prepare_ccolor_palette(theme): + """ + Prepare the Consistent Color Generation (XEP-0392) palette for a theme. + """ + if hasattr(theme, "CCG_PALETTE"): + # theme overrides the palette + return + + if any(bg != -1 for fg, bg in theme.LIST_COLOR_NICKNAMES): + # explicitly disable CCG, can’t handle dynamic background colors + theme.CCG_PALETTE = None + return + + if not hasattr(theme, "CCG_Y"): + theme.CCG_Y = 0.5**0.45 + + theme.CCG_PALETTE = colors.generate_ccg_palette( + [ + fg for fg, _ in theme.LIST_COLOR_NICKNAMES + # exclude grayscale + if fg < 232 + ], + theme.CCG_Y, + ) def reload_theme(): theme_name = config.get('theme') @@ -531,6 +560,7 @@ def reload_theme(): if hasattr(new_theme, 'theme'): theme = new_theme.theme + prepare_ccolor_palette(theme) else: return 'No theme present in the theme file' -- cgit v1.2.3