Source code for bitmap

import colors

[docs]class Bitmap: """A rectangular area of colored pixels. This class contains all the pixel-access functions (lines, fill, text, dots, etc). Bitmaps are a 2D list of pixels. Each pixel is a tuple of 3 values: red, blue, green in that order. """ def __init__(self, width, height, transparent_color=colors.TRANSPARENT): """Create a new bitmap. Args: width (int): Pixel width of the bitmap. height (int): Pixel height of the bitmap. transparent_color (bool, optional): The pixel value considered to be transparent (default is colors.TRANSPARENT). """ # Implemented in C pass @property def width(self): """Get the width of the bitmap in pixels.""" # Implemented in C pass @property def height(self): """Get the height of the bitmap in pixels.""" # Implemented in C pass @property def transparent_color(self): """Get or set the transparent color value. The default value is None for no-transparency. """ # Implemented in C pass
[docs] def set_pixel(self, x, y, color): """Set a single pixel value in the bitmap. Args: x (int): The x coordinate, y (int): The y coordinate, color (tuple): The color value. Accepts either 24-bit or 16-bit RGB. Will truncate to 16-bit. """ # Implemented in C pass
[docs] def get_pixel(self, x, y): """Get the color of a single pixel from the bitmap. Note that this may be different than the color tuple set by `set_pixel()` since colors are stored in 16-bit format natively. To compare the color returned by this function with a 24-bit RGB color, use the `color16()` function. Example: :: set_pixel(35, 35, WHITE) my_color = get_pixel(35, 35) if my_color == color16(WHITE): # Found my color! Args: x (int): The x coordinate. y (int): The y coordinate. Returns: tuple: The color value. """ # Implemented in C pass
[docs] def clear(self): """Clear the Bitmap to BLACK or transparent if set. """ # Implemented in C pass
[docs] def fill(self, color): """Fill the Bitmap with the given color. Args: color (tuple): The fill color. """ # Implemented in C pass
[docs] def draw_jpg(self, filename, start_x, start_y, x, y, width, height): """Draw a JPG file into the bitmap. Note: JPG file format support is limited to * Sampling: 4:4:4 (recommended), 4:2:0, or 4:2:2 * Cb/Cr sampling factor: 1 * Y/Cb/Cr or Grayscale Args: filename (str): name of the JPG file. start_x (int, optional): starting corner in the JPG image. Default is 0. start_y (int, optional): starting corner in the JPG image. Default is 0. x (int, optional): starting corner on the bitmap. Default is 0. y (int, optional): starting corner on the bitmap. Default is 0. width (int, optional): width of the JPG area to use. Default is None for all. height (int, optional): height of the JPG area to use. Default is None for all. """ # Implemented in C pass
[docs] def draw_rect(self, x1, y1, width, height, color): """Draw a rectangle on the bitmap. Args: x1 (int): Upper left corner of rectangle. y1 (int): Upper left corner of rectangle. width (int): Width of rectangle. height (int): Height of rectangle. color (tuple): Color of the rectangle. """ # Implemented in C pass
[docs] def fill_rect(self, x1, y1, width, height, color): """Draw a filled rectangle on the bitmap. Args: x1 (int): Upper left corner of rectangle. y1 (int): Upper left corner of rectangle. width (int): Width of rectangle. height (int): Height of rectangle. color (tuple): Color of the rectangle. """ # Implemented in C pass
[docs] def draw_circle(self, x, y, radius, color): """Draw a circle on the Bitmap. Args: x (int): The starting x coordinate. y (int): The starting y coordinate. radius (int): The circle radius. color (tuple): The circle color. """ # Implemented in C pass
[docs] def fill_circle(self, x, y, radius, color): """Draw a filled circle on the Bitmap. Args: x (int): The starting x coordinate. y (int): The starting y coordinate. radius (int): The circle radius. color (tuple): The circle color. """ # Implemented in C pass
[docs] def draw_line(self, x1, y1, x2, y2, color): """Draw a line on the Bitmap. Args: x1 (int): The starting x coordinate. y1 (int): The starting y coordinate. x2 (int): The ending x coordinate. y2 (int): The ending y coordinate. color (tuple): The line color. """ # Implemented in C pass
[docs] def draw_image(self, bitmap, x=0, y=0, start_x=0,start_y=0,end_x=None,end_y=None): """Copy one bitmap onto another. Args: bitmap (Bitmap): The bitmap to copy from. x (int): The destination x coordinate. y (int): The destination y coordinate. start_x (int, optional): The starting window on the read-from bitmap. Default is 0. start_y (int, optional): The starting window on the read-from bitmap. Default is 0. end_x (int, optional): The ending corner on the read-from bitmap. Default is width. end_y (int, optional): The ending corner scale (int, optional): The scaling factor. Default is 1. """ # Implemented in C pass
[docs] def draw_text(self, text, x=0, y=0, color=colors.WHITE, scale=1, background=None,): """Draw text or an image onto this Bitmap. This doesn't do any scrolling or wrap-around if text won't fit. Args: text (object): The text to display. x (int, optional): The x coordinate of upper left corner of text. Default is 0. y (int, optional): The y coordinate of upper left corner of text. Default is 0. color (tuple, optional): The text color. Default is (WHITE). scale (int, optional): The scaling factor. Default is 1. background (int, optional): The background color. Default is None (Transparent). """ # Implemented in C pass
[docs] @staticmethod def from_arr(data): """Create a Bitmap from a 2D array of integers. Args: data (list): 2D list of integers (default color mapping). """ # Implemented in C pass
[docs] @staticmethod def from_ascii(ascii_img, transparent_color, scale, color_map): """Create a Bitmap from a list of ASCII Art row-strings and a color map. Note: This function doesn't do error-checking, as it expects to be called with a properly-formed input generated by ascii_art.py. Args: ascii_img (list): list of strings "....###..$$..." to be translated into pixels. transparent_color (tuple): The pixel value considered to be transparent. scale (int): The scaling factor. color_map: dictionary mapping string chars to color tuples. """ # Implemented in C pass