]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - vectorio/_rectangle.py
Fix indexed bitmaps by correcting argument order at call
[hackapet/Adafruit_Blinka_Displayio.git] / vectorio / _rectangle.py
1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 `vectorio.rectangle`
7 ================================================================================
8
9 vectorio Rectangle for Blinka
10
11 **Software and Dependencies:**
12
13 * Adafruit Blinka:
14   https://github.com/adafruit/Adafruit_Blinka/releases
15
16 * Author(s): Melissa LeBlanc-Williams
17
18 """
19
20 from typing import Union
21 from displayio._colorconverter import ColorConverter
22 from displayio._palette import Palette
23 from displayio._area import Area
24 from ._vectorshape import _VectorShape
25
26 __version__ = "0.0.0+auto.0"
27 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
28
29
30 class Rectangle(_VectorShape):
31     """Vectorio Rectangle"""
32
33     def __init__(
34         self,
35         *,
36         pixel_shader: Union[ColorConverter, Palette],
37         width: int,
38         height: int,
39         x: int,
40         y: int,
41         color_index: int = 0,
42     ):
43         """Represents a rectangle by defining its bounds
44
45         :param Union[~displayio.ColorConverter,~displayio.Palette] pixel_shader:
46             The pixel shader that produces colors from values
47         :param int width: The number of pixels wide
48         :param int height: The number of pixels high
49         :param int x: Initial x position of the top left corner.
50         :param int y: Initial y position of the top left corner.
51         :param int color_index: Initial color_index to use when selecting color from the palette.
52         """
53         self._width = 1
54         self._height = 1
55         self._color_index = color_index + 1
56
57         super().__init__(pixel_shader, x, y)
58         self.width = width
59         self.height = height
60
61     @property
62     def width(self) -> int:
63         """The width of the rectangle in pixels"""
64         return self._width
65
66     @width.setter
67     def width(self, value: int) -> None:
68         if value < 1:
69             raise ValueError("width must be >= 1")
70
71         self._width = abs(value)
72         self._shape_set_dirty()
73
74     @property
75     def height(self) -> int:
76         """The height of the rectangle in pixels"""
77         return self._height
78
79     @height.setter
80     def height(self, value: int) -> None:
81         if value < 1:
82             raise ValueError("height must be >= 1")
83         self._height = abs(value)
84         self._shape_set_dirty()
85
86     @property
87     def color_index(self) -> int:
88         """The color_index of the rectangle as 0 based index of the palette."""
89         return self._color_index - 1
90
91     @color_index.setter
92     def color_index(self, value: int) -> None:
93         self._color_index = abs(value + 1)
94         self._shape_set_dirty()
95
96     def _get_pixel(self, x: int, y: int) -> int:
97         if 0 <= x < self._width and 0 <= y < self._height:
98             return self._color_index
99         return 0
100
101     def _get_area(self, out_area: Area) -> None:
102         out_area.x1 = 0
103         out_area.y1 = 0
104         out_area.x2 = self._width
105         out_area.y2 = self._height