datatypes.data_linestyle

 1from dataclasses import dataclass
 2from typing import Literal
 3
 4import drawBot
 5
 6from lib import color
 7
 8LineCap = Literal["butt", "round"]
 9LineDash = float | int | tuple[float | int, ...] | list[float | int] | None
10"""LineDash can be a single number (e.g. 5 for 5pt dashes), a tuple/list of numbers (e.g. (5, 3) for 5pt dashes with 3pt gaps), or None/0 for solid lines."""
11
12
13@dataclass
14class DLineStyle:
15    """Reusable line style settings that can be activated on demand."""
16
17    strokeWidth: float = 1
18    strokeColor: tuple | list | color.KColorSwatch = (0,)
19    lineDash: LineDash = None
20    lineCap: LineCap = "butt"
21    clearFill: bool = True
22
23    def _applyStrokeColor(self):
24        value = self.strokeColor
25
26        if isinstance(value, color.KColorSwatch):
27            value.setStroke()
28            return
29
30        if isinstance(value, list):
31            value = tuple(value)
32
33        if not isinstance(value, tuple):
34            raise TypeError("strokeColor must be tuple/list or support setStroke().")
35
36        if len(value) == 5:
37            drawBot.cmykStroke(*value)
38        elif len(value) in [1, 3, 4]:
39            drawBot.stroke(*value)
40        else:
41            raise ValueError("strokeColor tuple must be RGB/RGBA or CMYK/CMYKA.")
42
43    def _normalizeDash(self):
44        value = self.lineDash
45
46        if value in [None, 0]:
47            return None
48
49        if isinstance(value, (int, float)):
50            return None if value <= 0 else (value,)
51
52        if isinstance(value, list):
53            value = tuple(value)
54
55        if isinstance(value, tuple):
56            return value
57
58        return None
59
60    def apply(self):
61        """Activate this style by setting DrawBot line properties."""
62        if self.clearFill:
63            drawBot.fill(None)
64
65        self._applyStrokeColor()
66        drawBot.strokeWidth(self.strokeWidth)
67        drawBot.lineCap(self.lineCap)
68
69        dash = self._normalizeDash()
70        drawBot.lineDash(*dash) if dash else drawBot.lineDash(None)
71
72        return self
LineCap = typing.Literal['butt', 'round']
LineDash = float | int | tuple[float | int, ...] | list[float | int] | None

LineDash can be a single number (e.g. 5 for 5pt dashes), a tuple/list of numbers (e.g. (5, 3) for 5pt dashes with 3pt gaps), or None/0 for solid lines.

@dataclass
class DLineStyle:
14@dataclass
15class DLineStyle:
16    """Reusable line style settings that can be activated on demand."""
17
18    strokeWidth: float = 1
19    strokeColor: tuple | list | color.KColorSwatch = (0,)
20    lineDash: LineDash = None
21    lineCap: LineCap = "butt"
22    clearFill: bool = True
23
24    def _applyStrokeColor(self):
25        value = self.strokeColor
26
27        if isinstance(value, color.KColorSwatch):
28            value.setStroke()
29            return
30
31        if isinstance(value, list):
32            value = tuple(value)
33
34        if not isinstance(value, tuple):
35            raise TypeError("strokeColor must be tuple/list or support setStroke().")
36
37        if len(value) == 5:
38            drawBot.cmykStroke(*value)
39        elif len(value) in [1, 3, 4]:
40            drawBot.stroke(*value)
41        else:
42            raise ValueError("strokeColor tuple must be RGB/RGBA or CMYK/CMYKA.")
43
44    def _normalizeDash(self):
45        value = self.lineDash
46
47        if value in [None, 0]:
48            return None
49
50        if isinstance(value, (int, float)):
51            return None if value <= 0 else (value,)
52
53        if isinstance(value, list):
54            value = tuple(value)
55
56        if isinstance(value, tuple):
57            return value
58
59        return None
60
61    def apply(self):
62        """Activate this style by setting DrawBot line properties."""
63        if self.clearFill:
64            drawBot.fill(None)
65
66        self._applyStrokeColor()
67        drawBot.strokeWidth(self.strokeWidth)
68        drawBot.lineCap(self.lineCap)
69
70        dash = self._normalizeDash()
71        drawBot.lineDash(*dash) if dash else drawBot.lineDash(None)
72
73        return self

Reusable line style settings that can be activated on demand.

DLineStyle( strokeWidth: float = 1, strokeColor: tuple | list | lib.color.KColorSwatch = (0,), lineDash: float | int | tuple[float | int, ...] | list[float | int] | None = None, lineCap: Literal['butt', 'round'] = 'butt', clearFill: bool = True)
strokeWidth: float = 1
strokeColor: tuple | list | lib.color.KColorSwatch = (0,)
lineDash: float | int | tuple[float | int, ...] | list[float | int] | None = None
lineCap: Literal['butt', 'round'] = 'butt'
clearFill: bool = True
def apply(self):
61    def apply(self):
62        """Activate this style by setting DrawBot line properties."""
63        if self.clearFill:
64            drawBot.fill(None)
65
66        self._applyStrokeColor()
67        drawBot.strokeWidth(self.strokeWidth)
68        drawBot.lineCap(self.lineCap)
69
70        dash = self._normalizeDash()
71        drawBot.lineDash(*dash) if dash else drawBot.lineDash(None)
72
73        return self

Activate this style by setting DrawBot line properties.