3131__version__ = "0.0.0+auto.0"
3232__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Shapes.git"
3333
34- _OUTLINE_COLOR_INDEX = 1
3534
3635class Polygon (displayio .TileGrid ):
3736 # pylint: disable=too-many-arguments,invalid-name
@@ -41,14 +40,21 @@ class Polygon(displayio.TileGrid):
4140 :param int|None outline: The outline of the polygon. Can be a hex value for a color or
4241 ``None`` for no outline.
4342 :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)
4446 """
4547
48+ _OUTLINE = 1
49+ _FILL = 2
50+
4651 def __init__ (
4752 self ,
4853 points : List [Tuple [int , int ]],
4954 * ,
5055 outline : Optional [int ] = None ,
5156 close : Optional [bool ] = True ,
57+ colors : Optional [int ] = 2 ,
5258 ) -> None :
5359 if close :
5460 points .append (points [0 ])
@@ -67,7 +73,7 @@ def __init__(
6773 width = max (xs ) - min (xs ) + 1
6874 height = max (ys ) - min (ys ) + 1
6975
70- self ._palette = displayio .Palette (2 )
76+ self ._palette = displayio .Palette (colors + 1 )
7177 self ._palette .make_transparent (0 )
7278 self ._bitmap = displayio .Bitmap (width , height , 2 )
7379
@@ -82,6 +88,7 @@ def __init__(
8288 point_a [1 ] - y_offset ,
8389 point_b [0 ] - x_offset ,
8490 point_b [1 ] - y_offset ,
91+ self ._OUTLINE ,
8592 )
8693
8794 super ().__init__ (
@@ -95,17 +102,18 @@ def _line(
95102 y0 : int ,
96103 x1 : int ,
97104 y1 : int ,
105+ color : int ,
98106 ) -> None :
99107 if x0 == x1 :
100108 if y0 > y1 :
101109 y0 , y1 = y1 , y0
102110 for _h in range (y0 , y1 + 1 ):
103- self ._bitmap [x0 , _h ] = _OUTLINE_COLOR_INDEX
111+ self ._bitmap [x0 , _h ] = color
104112 elif y0 == y1 :
105113 if x0 > x1 :
106114 x0 , x1 = x1 , x0
107115 for _w in range (x0 , x1 + 1 ):
108- self ._bitmap [_w , y0 ] = _OUTLINE_COLOR_INDEX
116+ self ._bitmap [_w , y0 ] = color
109117 else :
110118 steep = abs (y1 - y0 ) > abs (x1 - x0 )
111119 if steep :
@@ -128,9 +136,9 @@ def _line(
128136
129137 for x in range (x0 , x1 + 1 ):
130138 if steep :
131- self ._bitmap [y0 , x ] = _OUTLINE_COLOR_INDEX
139+ self ._bitmap [y0 , x ] = color
132140 else :
133- self ._bitmap [x , y0 ] = _OUTLINE_COLOR_INDEX
141+ self ._bitmap [x , y0 ] = color
134142 err -= dy
135143 if err < 0 :
136144 y0 += ystep
@@ -142,13 +150,13 @@ def _line(
142150 def outline (self ) -> Optional [int ]:
143151 """The outline of the polygon. Can be a hex value for a color or
144152 ``None`` for no outline."""
145- return self ._palette [_OUTLINE_COLOR_INDEX ]
153+ return self ._palette [self . _OUTLINE ]
146154
147155 @outline .setter
148156 def outline (self , color : Optional [int ]) -> None :
149157 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 )
152160 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