]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - vectorio/_rectangle.py
Add vectorio support
[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     ):
42         """Represents a rectangle by defining its bounds
43
44         :param Union[~displayio.ColorConverter,~displayio.Palette] pixel_shader:
45             The pixel shader that produces colors from values
46         :param int width: The number of pixels wide
47         :param int height: The number of pixels high
48         :param int x: Initial x position of the top left corner.
49         :param int y: Initial y position of the top left corner.
50         :param int color_index: Initial color_index to use when selecting color from the palette.
51         """
52         self._width = 1
53         self._height = 1
54         self._color_index = 1
55
56         super().__init__(pixel_shader, x, y)
57         self.width = width
58         self.height = height
59
60     @property
61     def width(self) -> int:
62         """The width of the rectangle in pixels"""
63         return self._width
64
65     @width.setter
66     def width(self, value: int) -> None:
67         if value < 1:
68             raise ValueError("width must be >= 1")
69
70         self._width = abs(value)
71         self._shape_set_dirty()
72
73     @property
74     def height(self) -> int:
75         """The height of the rectangle in pixels"""
76         return self._height
77
78     @height.setter
79     def height(self, value: int) -> None:
80         if value < 1:
81             raise ValueError("height must be >= 1")
82         self._height = abs(value)
83         self._shape_set_dirty()
84
85     @property
86     def color_index(self) -> int:
87         """The color_index of the rectangle as 0 based index of the palette."""
88         return self._color_index - 1
89
90     @color_index.setter
91     def color_index(self, value: int) -> None:
92         self._color_index = abs(value + 1)
93         self._shape_set_dirty()
94
95     def _get_pixel(self, x: int, y: int) -> int:
96         if 0 <= x < self._width and 0 <= y < self._height:
97             return self._color_index
98         return 0
99
100     def _get_area(self, out_area: Area) -> None:
101         out_area.x1 = 0
102         out_area.y1 = 0
103         out_area.x2 = self._width
104         out_area.y2 = self._height