buildzr 0.0.7__py3-none-any.whl → 0.0.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
buildzr/__about__.py CHANGED
@@ -1 +1 @@
1
- VERSION = "0.0.7"
1
+ VERSION = "0.0.9"
buildzr/dsl/__init__.py CHANGED
@@ -9,10 +9,13 @@ from .dsl import (
9
9
  SystemContextView,
10
10
  ContainerView,
11
11
  ComponentView,
12
+ StyleElements,
13
+ StyleRelationships,
12
14
  )
13
15
  from .relations import (
14
16
  desc,
15
17
  With,
16
18
  )
17
19
  from .explorer import Explorer
18
- from .expression import Expression
20
+ from .expression import Expression
21
+ from .color import Color
buildzr/dsl/color.py ADDED
@@ -0,0 +1,121 @@
1
+ import re
2
+ from typing import Tuple, Optional, Union, Literal
3
+
4
+ class Color:
5
+ r: int
6
+ g: int
7
+ b: int
8
+
9
+ _ENGLISH_COLORS = {
10
+ 'black': '#000000',
11
+ 'white': '#ffffff',
12
+ 'red': '#ff0000',
13
+ 'green': '#00ff00',
14
+ 'blue': '#0000ff',
15
+ 'yellow': '#ffff00',
16
+ 'cyan': '#00ffff',
17
+ 'magenta': '#ff00ff',
18
+ 'gray': '#808080',
19
+ 'grey': '#808080',
20
+ 'orange': '#ffa500',
21
+ 'purple': '#800080',
22
+ 'pink': '#ffc0cb',
23
+ 'brown': '#a52a2a',
24
+ 'lime': '#00ff00',
25
+ 'navy': '#000080',
26
+ 'teal': '#008080',
27
+ 'olive': '#808000',
28
+ 'maroon': '#800000',
29
+ 'silver': '#c0c0c0',
30
+ 'gold': '#ffd700',
31
+ }
32
+
33
+ def __init__(
34
+ self,
35
+ value: Union[
36
+ 'Color',
37
+ str,
38
+ Tuple[int, int, int],
39
+ Literal[
40
+ 'black', 'white', 'red', 'green', 'blue', 'yellow', 'cyan', 'magenta', 'gray', 'grey', 'orange', 'purple', 'pink', 'brown', 'lime', 'navy', 'teal', 'olive', 'maroon', 'silver', 'gold'
41
+ ]
42
+ ]
43
+ ):
44
+ if isinstance(value, Color):
45
+ self.r, self.g, self.b = value.r, value.g, value.b
46
+ elif isinstance(value, tuple):
47
+ if len(value) == 3:
48
+ self.r, self.g, self.b = value
49
+ else:
50
+ raise ValueError("Tuple must be (r, g, b)")
51
+ elif isinstance(value, str):
52
+ self.r, self.g, self.b = self._parse_color(value)
53
+ else:
54
+ raise TypeError("Invalid type for Color constructor")
55
+
56
+ @classmethod
57
+ def is_valid_color(cls, value: Union[str, Tuple[int, int, int], 'Color']) -> bool:
58
+ try:
59
+ if isinstance(value, tuple):
60
+ if len(value) == 3 and all(isinstance(x, int) and 0 <= x <= 255 for x in value):
61
+ return True
62
+ return False
63
+ elif isinstance(value, str):
64
+ v = value.strip().lower()
65
+ if v in cls._ENGLISH_COLORS:
66
+ return True
67
+ if v.startswith('#'):
68
+ try:
69
+ cls._parse_hex(v)
70
+ return True
71
+ except Exception:
72
+ return False
73
+ if v.startswith('rgb'):
74
+ try:
75
+ cls._parse_rgb(v)
76
+ return True
77
+ except Exception:
78
+ return False
79
+ return False
80
+ return False
81
+ except Exception:
82
+ return False
83
+
84
+ @classmethod
85
+ def _parse_color(cls, value: str) -> Tuple[int, int, int]:
86
+ value = value.strip().lower()
87
+ if value in cls._ENGLISH_COLORS:
88
+ value = cls._ENGLISH_COLORS[value]
89
+ if value.startswith('#'):
90
+ return cls._parse_hex(value)
91
+ if value.startswith('rgb'):
92
+ return cls._parse_rgb(value)
93
+ raise ValueError(f"Unknown color format: {value}")
94
+
95
+ @classmethod
96
+ def _parse_hex(cls, value: str) -> Tuple[int, int, int]:
97
+ value = value.lstrip('#')
98
+ if len(value) == 3:
99
+ value = ''.join([c*2 for c in value])
100
+ if len(value) == 6:
101
+ r, g, b = value[0:2], value[2:4], value[4:6]
102
+ return int(r, 16), int(g, 16), int(b, 16)
103
+ raise ValueError(f"Invalid hex color: #{value}")
104
+
105
+ @classmethod
106
+ def _parse_rgb(cls, value: str) -> Tuple[int, int, int]:
107
+ # Match rgb(r, g, b)
108
+ match = re.match(r"rgb\(([^)]+)\)", value)
109
+ if not match:
110
+ raise ValueError(f"Invalid rgb color: {value}")
111
+ parts = [x.strip() for x in match.group(1).split(',')]
112
+ if len(parts) == 3:
113
+ r, g, b = map(int, parts)
114
+ return r, g, b
115
+ raise ValueError(f"Invalid rgb color: {value}")
116
+
117
+ def to_hex(self) -> str:
118
+ return f"#{self.r:02x}{self.g:02x}{self.b:02x}"
119
+
120
+ def __str__(self) -> str:
121
+ return f"rgb({self.r}, {self.g}, {self.b})"