Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
38 changes: 35 additions & 3 deletions ckanext/dcor_theme/css/make_dcor_main_css.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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")):
Expand Down
44 changes: 44 additions & 0 deletions ckanext/dcor_theme/tests/test_css.py
Original file line number Diff line number Diff line change
@@ -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)")
Loading