From bf14779eb34b1d2028869e81deb02da9c0d3c53c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Tue, 3 Nov 2026 16:38:56 +0100 Subject: [PATCH] fix: also replace colors in rgb() notation in main.css CKAN 2.11.7 writes several colors in main.css in `rgb(r, g, b)` notation with floating point values (e.g. `rgb(25.6, 85.6, 104)` instead of `#1a5668`). Seven of the 16 colors in `color_map` only appear in that notation, so the DCOR branding (e.g. the header) would partly be lost when generating dcor_main.css for CKAN 2.11.7. Colors in rgb notation are now replaced as well if all channels are within 1 of a mapped color (the closest one is used). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG | 2 + ckanext/dcor_theme/css/make_dcor_main_css.py | 38 +++++++++++++++-- ckanext/dcor_theme/tests/test_css.py | 44 ++++++++++++++++++++ 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 ckanext/dcor_theme/tests/test_css.py diff --git a/CHANGELOG b/CHANGELOG index 6ef6503..6dd974a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,6 @@ 1.1.5 + - fix: DCOR colors were not applied to colors in `rgb()` notation, which + CKAN 2.11.7 uses in `main.css` for some colors - fix: compatibility with setuptools>=82 (no `pkg_resources`), which is shipped with CKAN 2.11.6 1.1.4 diff --git a/ckanext/dcor_theme/css/make_dcor_main_css.py b/ckanext/dcor_theme/css/make_dcor_main_css.py index cfa95d5..c98661b 100644 --- a/ckanext/dcor_theme/css/make_dcor_main_css.py +++ b/ckanext/dcor_theme/css/make_dcor_main_css.py @@ -1,6 +1,7 @@ """Map CKAN default colors to DCOR colors and generate dcor_theme_colors.css""" from importlib import resources as importlib_resources import pathlib +import re #: Path to CKAN's main.css ckan_css_path = pathlib.Path(str( @@ -28,6 +29,39 @@ } +#: Regular expression for colors in `rgb(r, g, b)` notation +RGB_RE = re.compile(r"rgb\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)") + + +def replace_colors(css_data): + """Replace CKAN default colors with DCOR colors + + Since CKAN 2.11.7, `main.css` contains some colors in `rgb(r, g, b)` + notation with floating point values instead of hex notation, e.g. + `rgb(25.6, 85.6, 104)` instead of `#1a5668`. These are replaced as + well if all channels are within 1 of a color in :const:`color_map`. + If several colors match, the closest one is used. + """ + for color in color_map: + css_data = css_data.replace(color, color_map[color]) + + rgb_map = {tuple(int(color[ii:ii + 2], 16) for ii in (1, 3, 5)): new + for color, new in color_map.items()} + + def replace_rgb(match): + rgb = [float(value) for value in match.groups()] + candidates = [] + for key, new in rgb_map.items(): + diffs = [abs(a - b) for a, b in zip(rgb, key)] + if max(diffs) <= 1: + candidates.append((sum(d ** 2 for d in diffs), new)) + if candidates: + return min(candidates)[1] + return match.group(0) + + return RGB_RE.sub(replace_rgb, css_data) + + def main(): here = pathlib.Path(__file__).parent.resolve() # The webassets_dcor.yml file is already present in the public/base/css @@ -38,9 +72,7 @@ def main(): dcor_css_path = base_css / "dcor_main.css" # replace colors - main_css_data = ckan_css_path.read_text() - for color in color_map: - main_css_data = main_css_data.replace(color, color_map[color]) + main_css_data = replace_colors(ckan_css_path.read_text()) # add custom css inputs for ci in sorted(here.glob("*.css")): diff --git a/ckanext/dcor_theme/tests/test_css.py b/ckanext/dcor_theme/tests/test_css.py new file mode 100644 index 0000000..e1b7f73 --- /dev/null +++ b/ckanext/dcor_theme/tests/test_css.py @@ -0,0 +1,44 @@ +import re + +import pytest + +from ckanext.dcor_theme.css import make_dcor_main_css as mdc + + +def hex_to_rgb(color): + return tuple(int(color[ii:ii + 2], 16) for ii in (1, 3, 5)) + + +@pytest.mark.parametrize("css,expected", [ + # hex notation (CKAN <= 2.11.6) + ["color: #1a5668;", f"color: {mdc.color_map['#1a5668']};"], + # rgb notation with floats (CKAN >= 2.11.7) + ["color: rgb(25.6, 85.6, 104);", f"color: {mdc.color_map['#1a5668']};"], + ["color: rgb(0, 34.6844262295, 45.5);", + f"color: {mdc.color_map['#00232e']};"], + # a channel exactly 0.5 off (110.5 vs. 111) + ["color: rgb(27.2, 90.95, 110.5);", f"color: {mdc.color_map['#1b5b6f']};"], + # other colors are not touched + ["color: rgb(212.47, 62.50, 58.03);", "color: rgb(212.47, 62.50, 58.03);"], + ["color: #d43f3a;", "color: #d43f3a;"], +]) +def test_replace_colors(css, expected): + assert mdc.replace_colors(css) == expected + + +def test_replace_colors_ckan_main_css(): + """No mapped CKAN color remains in the installed CKAN's main.css""" + css = mdc.replace_colors(mdc.ckan_css_path.read_text()) + for color in mdc.color_map: + assert color not in css + ref = hex_to_rgb(color) + for match in mdc.RGB_RE.finditer(css): + rgb = [float(value) for value in match.groups()] + assert not all(abs(a - b) <= 1 for a, b in zip(rgb, ref)), \ + f"{match.group(0)} ({color}) was not replaced" + # the DCOR colors are there + assert css.count(mdc.color_map["#1a5668"]) >= 9 + + +def test_rgb_regexp(): + assert re.fullmatch(mdc.RGB_RE, "rgb(1, 2.5, 3)")