namespace RPiRgbLEDMatrix; /// /// Represents a canvas whose pixels can be manipulated. /// public class RGBLedCanvas { // This is a wrapper for canvas no need to implement IDisposable here // because RGBLedMatrix has ownership and takes care of disposing canvases internal IntPtr _canvas; // this is not called directly by the consumer code, // consumer uses factory methods in RGBLedMatrix internal RGBLedCanvas(IntPtr canvas) { _canvas = canvas; led_canvas_get_size(_canvas, out var width, out var height); Width = width; Height = height; } /// /// The width of the canvas in pixels. /// public int Width { get; private set; } /// /// The height of the canvas in pixels. /// public int Height { get; private set; } /// /// Sets the color of a specific pixel. /// /// The X coordinate of the pixel. /// The Y coordinate of the pixel. /// New pixel color. public void SetPixel(int x, int y, Color color) => led_canvas_set_pixel(_canvas, x, y, color.R, color.G, color.B); /// /// Copies the colors from the specified buffer to a rectangle on the canvas. /// /// The X coordinate of the top-left pixel of the rectangle. /// The Y coordinate of the top-left pixel of the rectangle. /// Width of the rectangle. /// Height of the rectangle. /// Buffer containing the colors to copy. public void SetPixels(int x, int y, int width, int height, Span colors) { if (colors.Length < width * height) throw new ArgumentOutOfRangeException(nameof(colors)); led_canvas_set_pixels(_canvas, x, y, width, height, ref colors[0]); } /// /// Sets the color of the entire canvas. /// /// New canvas color. public void Fill(Color color) => led_canvas_fill(_canvas, color.R, color.G, color.B); /// /// Cleans the entire canvas. /// public void Clear() => led_canvas_clear(_canvas); /// /// Draws a circle of the specified color. /// /// The X coordinate of the center. /// The Y coordinate of the center. /// The radius of the circle, in pixels. /// The color of the circle. public void DrawCircle(int x, int y, int radius, Color color) => draw_circle(_canvas, x, y, radius, color.R, color.G, color.B); /// /// Draws a line of the specified color. /// /// The X coordinate of the first point. /// The Y coordinate of the first point. /// The X coordinate of the second point. /// The Y coordinate of the second point. /// The color of the line. public void DrawLine(int x0, int y0, int x1, int y1, Color color) => draw_line(_canvas, x0, y0, x1, y1, color.R, color.G, color.B); /// /// Draws the text with the specified color. /// /// Font to draw text with. /// The X coordinate of the starting point. /// The Y coordinate of the starting point. /// The color of the text. /// Text to draw. /// Additional spacing between characters. /// Whether to draw the text vertically. /// How many pixels was advanced on the screen. public int DrawText(RGBLedFont font, int x, int y, Color color, string text, int spacing = 0, bool vertical = false) => font.DrawText(_canvas, x, y, color, text, spacing, vertical); }