Canvas class¶
-
class
Canvas.
Canvas
(width: int, height: int, rotation: int)[source]¶ A canvas makes it easy to display using the matrix by providing a translation layer between pixels on a cartesian coordinate system and color data readable by the arduino and the WS2812B RGB leds.
The canvas uses Color instances to represent all colors. See Color class
The user functions are:
- draw_pixel(x, y, color)
- draw_line(x_start, y_start, x_end, y_end, color)
- draw_rect(x, y, width, height, color)
- draw_text(x, y, text, color, ignore_height_warning=False)
- set_font(path, size)
- clear(color)
-
clear
(color: helpers.Color.Color = <helpers.Color.Color object>)[source]¶ Set all pixels to some color
Parameters: color – the color that should be applied
-
draw_line
(x_start: int, y_start: int, x_end: int, y_end: int, color: helpers.Color.Color)[source]¶ An implementation of bresenhams line drawing algorithm. Draws a line from <x_start, y_start> to <x_end, y_end> in the given color.
Parameters: - x_start – x position where the line should start
- y_start – y position where the line should end
- x_end – x position where the line should start
- y_end – y position where the line should end
- color – color the line should be drawn in
Returns: nothing
-
draw_pixel
(x: int, y: int, color: helpers.Color.Color)[source]¶ Set a pixel to a color. Most basic canvas function.
Parameters: - x – x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width
- y – y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height
- color – the description of the color that should be set
-
draw_rect
(x: int, y: int, width: int, height: int, color: helpers.Color.Color)[source]¶ Draw a rectangle, starting from x and y and stopping after having drawn width/height pixels
Parameters: - x – x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width
- y – y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height
- width – how wide the rectangle should be.
- height – how high the rectangle should be
- color – the color the rectangle should have
Returns: nothing
-
draw_rectangle
(rect: Canvas.Rect, color: helpers.Color.Color = None)[source]¶ Like draw_rect, but with an instance of the Rect class
Parameters: - rect – the specification of the rectangle to be drawn
- color – the color the rectangle should have. if None, the rect color will be used if given, but one of the two must exist. This also means that this parameter overrides the rect color!
Returns: nothing
-
draw_text
(text, x: int, y: int, color: helpers.Color.Color)[source]¶ Draw text on the canvas. Rendering over the borders is cut off, so you do not need boundary checking.
Parameters: - text – the text to be rendered, or a font object generated using render_text”.
- x – the top-left starting position of the text
- y – the top-left starting position of the text
- color – color of the text
Returns: width of the text to be rendered
-
get_buffer_for_arduino
() → bytearray[source]¶ This method can be used to retrieve the internal data buffer. Modifications will probably do weird shit. Mostly useful for pushing the data out to the arduino, who actually understands what all the numbers mean.
Returns: a bytearray with all color values
-
get_color
(x, y) → helpers.Color.Color[source]¶ Get a Color instance describing the color of the led at x,y
Parameters: - x – x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width
- y – y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height
Returns: a Color instance
-
get_color_rgb
(x, y) → Tuple[int, int, int][source]¶ Return an rgb tuple describing the color of the led at x, y. You should not normally use this method, as it only shaves off the overhead of creating the color object
Parameters: - x – x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width
- y – y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height
Returns: rgb integer tuple for the color (e.g. 0-255)
-
get_last_font_size
()[source]¶ Get the font size of the font that was loaded last, or None :return: integer font size or None, if no font has been set so far
-
get_pixel_index
(x, y)[source]¶ Convert a cartesian coordinate for an led into the index that represents red for that led in the buffer.
Currently, the chaining is assumed to be in a zig-zag, as follows:
col0 col1 col2 col3 col4 col5 col6 col7 col8 col9 row0 99 98 97 96 95 94 93 92 91 90 row1 80 81 82 83 84 85 86 87 88 89 row2 79 78 77 76 75 74 73 72 71 70 row3 60 61 62 63 64 65 66 67 68 69 row4 59 58 57 56 55 54 53 52 51 50 row5 40 41 42 43 44 45 46 47 48 49 row6 39 38 37 36 35 34 33 32 31 30 row7 20 21 22 23 24 25 26 27 28 29 row8 19 18 17 16 15 14 13 12 11 10 row9 0 1 2 3 4 5 6 7 8 9 Parameters: - x – x coordinate of the led in the matrix (counted left-to-right)
- y – y coordinate of the led in the matrix (counted top-to-bottom)
Returns: index of the red value of that led (g, b, are +1, +2 of that position respectively) in the buffer
-
get_red_index
(x, y)[source]¶ Pretty much like get_pixel_index, but this function returns the position of the red value of the given led in the byte buffer.
Parameters: - x – x coordinate of led in cartesian system
- y – y coordinate of led in cartesian system
Returns: position of “red” in the background buffer
-
static
render_text
(text: str, font_path: str = 'Inconsolata.otf', size: int = 15) → helpers.fonts.Bitmap[source]¶ Render a piece of text. Doing this once and then drawing with it is significantly faster than using draw_text with strings
Parameters: - text – the text to be rendered
- font_path – path to the font to be used for rendering
- size – font size. 13 is large.
Returns: a pre-rendered text object