Skip to content

Commit 8880089

Browse files
ilaifMidnighter
authored andcommitted
[FIX] color serialization to be in a full hex format
1 parent 5876bf4 commit 8880089

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/structurizr/view/color.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pydantic.color
2+
3+
4+
class Color(pydantic.color.Color):
5+
def as_hex(self) -> str:
6+
"""
7+
Hex string representing the color
8+
"""
9+
values = [pydantic.color.float_to_255(c) for c in self._rgba[:3]]
10+
if self._rgba.alpha is not None:
11+
values.append(pydantic.color.float_to_255(self._rgba.alpha))
12+
13+
as_hex = "".join(f"{v:02x}" for v in values)
14+
return "#" + as_hex
15+
16+
def __str__(self) -> str:
17+
return self.as_hex()

src/structurizr/view/element_style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import Optional
2020

2121
from pydantic import Field
22-
from pydantic.color import Color
22+
from .color import Color
2323

2424
from ..abstract_base import AbstractBase
2525
from ..base_model import BaseModel

src/structurizr/view/relationship_style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import Optional
2020

2121
from pydantic import Field
22-
from pydantic.color import Color
22+
from .color import Color
2323

2424
from ..abstract_base import AbstractBase
2525
from ..base_model import BaseModel

0 commit comments

Comments
 (0)