31
31
__version__ = "0.0.0+auto.0"
32
32
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Shapes.git"
33
33
34
- _OUTLINE_COLOR_INDEX = 1
35
34
36
35
class Polygon (displayio .TileGrid ):
37
36
# pylint: disable=too-many-arguments,invalid-name
@@ -41,14 +40,21 @@ class Polygon(displayio.TileGrid):
41
40
:param int|None outline: The outline of the polygon. Can be a hex value for a color or
42
41
``None`` for no outline.
43
42
:param bool close: (Optional) Wether to connect first and last point. (True)
43
+ :param int colors: (Optional) Number of colors to use. Most polygons would use two, one for
44
+ outline and one for fill. If you're not filling your polygon, set this to 1
45
+ for smaller memory footprint. (2)
44
46
"""
45
47
48
+ _OUTLINE = 1
49
+ _FILL = 2
50
+
46
51
def __init__ (
47
52
self ,
48
53
points : List [Tuple [int , int ]],
49
54
* ,
50
55
outline : Optional [int ] = None ,
51
56
close : Optional [bool ] = True ,
57
+ colors : Optional [int ] = 2 ,
52
58
) -> None :
53
59
if close :
54
60
points .append (points [0 ])
@@ -67,7 +73,7 @@ def __init__(
67
73
width = max (xs ) - min (xs ) + 1
68
74
height = max (ys ) - min (ys ) + 1
69
75
70
- self ._palette = displayio .Palette (2 )
76
+ self ._palette = displayio .Palette (colors + 1 )
71
77
self ._palette .make_transparent (0 )
72
78
self ._bitmap = displayio .Bitmap (width , height , 2 )
73
79
@@ -82,6 +88,7 @@ def __init__(
82
88
point_a [1 ] - y_offset ,
83
89
point_b [0 ] - x_offset ,
84
90
point_b [1 ] - y_offset ,
91
+ self ._OUTLINE ,
85
92
)
86
93
87
94
super ().__init__ (
@@ -95,17 +102,18 @@ def _line(
95
102
y0 : int ,
96
103
x1 : int ,
97
104
y1 : int ,
105
+ color : int ,
98
106
) -> None :
99
107
if x0 == x1 :
100
108
if y0 > y1 :
101
109
y0 , y1 = y1 , y0
102
110
for _h in range (y0 , y1 + 1 ):
103
- self ._bitmap [x0 , _h ] = _OUTLINE_COLOR_INDEX
111
+ self ._bitmap [x0 , _h ] = color
104
112
elif y0 == y1 :
105
113
if x0 > x1 :
106
114
x0 , x1 = x1 , x0
107
115
for _w in range (x0 , x1 + 1 ):
108
- self ._bitmap [_w , y0 ] = _OUTLINE_COLOR_INDEX
116
+ self ._bitmap [_w , y0 ] = color
109
117
else :
110
118
steep = abs (y1 - y0 ) > abs (x1 - x0 )
111
119
if steep :
@@ -128,9 +136,9 @@ def _line(
128
136
129
137
for x in range (x0 , x1 + 1 ):
130
138
if steep :
131
- self ._bitmap [y0 , x ] = _OUTLINE_COLOR_INDEX
139
+ self ._bitmap [y0 , x ] = color
132
140
else :
133
- self ._bitmap [x , y0 ] = _OUTLINE_COLOR_INDEX
141
+ self ._bitmap [x , y0 ] = color
134
142
err -= dy
135
143
if err < 0 :
136
144
y0 += ystep
@@ -142,13 +150,13 @@ def _line(
142
150
def outline (self ) -> Optional [int ]:
143
151
"""The outline of the polygon. Can be a hex value for a color or
144
152
``None`` for no outline."""
145
- return self ._palette [_OUTLINE_COLOR_INDEX ]
153
+ return self ._palette [self . _OUTLINE ]
146
154
147
155
@outline .setter
148
156
def outline (self , color : Optional [int ]) -> None :
149
157
if color is None :
150
- self ._palette [_OUTLINE_COLOR_INDEX ] = 0
151
- self ._palette .make_transparent (_OUTLINE_COLOR_INDEX )
158
+ self ._palette [self . _OUTLINE ] = 0
159
+ self ._palette .make_transparent (self . _OUTLINE )
152
160
else :
153
- self ._palette [_OUTLINE_COLOR_INDEX ] = color
154
- self ._palette .make_opaque (_OUTLINE_COLOR_INDEX )
161
+ self ._palette [self . _OUTLINE ] = color
162
+ self ._palette .make_opaque (self . _OUTLINE )
0 commit comments