Skip to content

Commit

Permalink
feat: hex to rgb (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav authored Dec 3, 2023
1 parent ed2fadc commit b5ab5e2
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/unfold/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.utils.module_loading import import_string

from .settings import get_config
from .utils import hex_to_rgb
from .widgets import INPUT_CLASSES


Expand Down Expand Up @@ -68,7 +69,9 @@ def each_context(self, request: HttpRequest) -> Dict[str, Any]:
"show_view_on_site": get_config(self.settings_name)[
"SHOW_VIEW_ON_SITE"
],
"colors": get_config(self.settings_name)["COLORS"],
"colors": self._process_colors(
get_config(self.settings_name)["COLORS"]
),
"tab_list": self.get_tabs_list(request),
"styles": [
self._get_value(style, request)
Expand Down Expand Up @@ -334,3 +337,15 @@ def _replace_values(self, target: Dict, source: Dict, request: HttpRequest):
target[key] = source[key]

return target

def _process_colors(
self, colors: Dict[str, Dict[str, str]]
) -> Dict[str, Dict[str, str]]:
for name, weights in colors.items():
for weight, value in weights.items():
if value[0] != "#":
continue

colors[name][weight] = " ".join(str(item) for item in hex_to_rgb(value))

return colors
12 changes: 11 additions & 1 deletion src/unfold/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import decimal
import json
from typing import Any, Iterable
from typing import Any, Iterable, List

from django.db import models
from django.template.loader import render_to_string
Expand Down Expand Up @@ -111,3 +111,13 @@ def display_for_field(value: Any, field: Any, empty_value_display: str) -> str:
return display_for_value(value, empty_value_display)
else:
return display_for_value(value, empty_value_display)


def hex_to_rgb(hex_color: str) -> List[int]:
hex_color = hex_color.lstrip("#")

r = int(hex_color[0:2], 16)
g = int(hex_color[2:4], 16)
b = int(hex_color[4:6], 16)

return (r, g, b)
96 changes: 96 additions & 0 deletions tests/test_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from django.contrib.auth.models import AnonymousUser
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from unfold.settings import CONFIG_DEFAULTS, get_config
from unfold.sites import UnfoldAdminSite


class ColorsTestCase(TestCase):
@override_settings(
UNFOLD={
**CONFIG_DEFAULTS,
**{
"COLORS": {
"primary": {
50: "#f0f9ff",
100: "#e0f2fe",
200: "#bae6fd",
300: "#7dd3fc",
400: "#38bdf8",
500: "#0ea5e9",
600: "#0284c7",
700: "#0369a1",
800: "#075985",
900: "#0c4a6e",
950: "#082f49",
}
},
},
}
)
def test_colors_hex_to_rgb(self):
admin_site = UnfoldAdminSite()
request = RequestFactory().get("/rand")
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertTrue("colors" in context)
self.assertTrue("primary" in context["colors"])

self.assertEqual(context["colors"]["primary"][50], "240 249 255")
self.assertEqual(context["colors"]["primary"][100], "224 242 254")
self.assertEqual(context["colors"]["primary"][200], "186 230 253")
self.assertEqual(context["colors"]["primary"][300], "125 211 252")
self.assertEqual(context["colors"]["primary"][400], "56 189 248")
self.assertEqual(context["colors"]["primary"][500], "14 165 233")
self.assertEqual(context["colors"]["primary"][600], "2 132 199")
self.assertEqual(context["colors"]["primary"][700], "3 105 161")
self.assertEqual(context["colors"]["primary"][800], "7 89 133")
self.assertEqual(context["colors"]["primary"][900], "12 74 110")
self.assertEqual(context["colors"]["primary"][950], "8 47 73")

get_config.cache_clear()

@override_settings(
UNFOLD={
**CONFIG_DEFAULTS,
**{
"COLORS": {
"primary": {
50: "240 249 255",
100: "224 242 254",
200: "186 230 253",
300: "125 211 252",
400: "56 189 248",
500: "14 165 233",
600: "2 132 199",
700: "3 105 161",
800: "7 89 133",
900: "12 74 110",
950: "8 47 73",
}
},
},
}
)
def test_colors_rgb(self):
admin_site = UnfoldAdminSite()
request = RequestFactory().get("/rand")
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertTrue("colors" in context)
self.assertTrue("primary" in context["colors"])

self.assertEqual(context["colors"]["primary"][50], "240 249 255")
self.assertEqual(context["colors"]["primary"][100], "224 242 254")
self.assertEqual(context["colors"]["primary"][200], "186 230 253")
self.assertEqual(context["colors"]["primary"][300], "125 211 252")
self.assertEqual(context["colors"]["primary"][400], "56 189 248")
self.assertEqual(context["colors"]["primary"][500], "14 165 233")
self.assertEqual(context["colors"]["primary"][600], "2 132 199")
self.assertEqual(context["colors"]["primary"][700], "3 105 161")
self.assertEqual(context["colors"]["primary"][800], "7 89 133")
self.assertEqual(context["colors"]["primary"][900], "12 74 110")
self.assertEqual(context["colors"]["primary"][950], "8 47 73")

get_config.cache_clear()

0 comments on commit b5ab5e2

Please sign in to comment.