|
| 1 | +# SPDX-FileCopyrightText: 2023 Jose D. Montoya |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | + |
| 6 | +""" |
| 7 | +
|
| 8 | +`umap` |
| 9 | +================================================================================ |
| 10 | +
|
| 11 | +CircuitPython color map graph |
| 12 | +
|
| 13 | +* Author(s): Jose D. Montoya |
| 14 | +
|
| 15 | +
|
| 16 | +""" |
| 17 | +try: |
| 18 | + from circuitpython_uplot.uplot import Uplot |
| 19 | +except ImportError: |
| 20 | + pass |
| 21 | + |
| 22 | +from ulab import numpy as np |
| 23 | +import displayio |
| 24 | +from vectorio import Rectangle |
| 25 | + |
| 26 | +__version__ = "0.0.0+auto.0" |
| 27 | +__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git" |
| 28 | + |
| 29 | + |
| 30 | +# pylint: disable=too-few-public-methods, invalid-name, duplicate-code, too-many-locals, too-many-arguments |
| 31 | +# pylint: disable=unused-variable |
| 32 | +class umap: |
| 33 | + """ |
| 34 | + Main class to display different graphics |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + plot: Uplot, |
| 40 | + data_points: np.ndarray, |
| 41 | + initial_color: int, |
| 42 | + final_color: int, |
| 43 | + ) -> None: |
| 44 | + |
| 45 | + """ |
| 46 | +
|
| 47 | + :param plot: Plot object for the scatter to be drawn |
| 48 | + :param data_points: data points to create the color map |
| 49 | + :param initial_color: initial color to create the color map |
| 50 | + :param final_color: final color to create the color map |
| 51 | +
|
| 52 | + """ |
| 53 | + |
| 54 | + xmin = np.min(data_points) |
| 55 | + xmax = np.max(data_points) |
| 56 | + |
| 57 | + xnorm = np.array(plot.transform(xmin, xmax, 0.0, 1.0, data_points)) |
| 58 | + |
| 59 | + box_iny = data_points.shape[0] |
| 60 | + box_inx = data_points.shape[1] |
| 61 | + |
| 62 | + width = plot._newxmax - plot._newxmin |
| 63 | + height = plot._newymin - plot._newymax |
| 64 | + xdist = width // box_inx |
| 65 | + ydist = height // box_iny |
| 66 | + |
| 67 | + palette = displayio.Palette(box_inx * box_iny) |
| 68 | + start_color = initial_color |
| 69 | + end_color = final_color |
| 70 | + counter = 0 |
| 71 | + for row in xnorm: |
| 72 | + for element in row: |
| 73 | + palette[counter] = color_fade(start_color, end_color, element) |
| 74 | + counter = counter + 1 |
| 75 | + |
| 76 | + deltax = plot._newxmin |
| 77 | + deltay = plot._newymax |
| 78 | + color = 0 |
| 79 | + for j in range(box_iny): |
| 80 | + for i in range(box_inx): |
| 81 | + plot.append( |
| 82 | + Rectangle( |
| 83 | + pixel_shader=palette, |
| 84 | + x=deltax, |
| 85 | + y=deltay, |
| 86 | + width=xdist, |
| 87 | + height=ydist, |
| 88 | + color_index=color, |
| 89 | + ) |
| 90 | + ) |
| 91 | + deltax = deltax + xdist |
| 92 | + color = color + 1 |
| 93 | + |
| 94 | + deltax = plot._newxmin |
| 95 | + deltay = deltay + ydist |
| 96 | + |
| 97 | + |
| 98 | +def color_to_tuple(value): |
| 99 | + """Converts a color from a 24-bit integer to a tuple. |
| 100 | + :param value: RGB LED desired value - can be a RGB tuple or a 24-bit integer. |
| 101 | + """ |
| 102 | + if isinstance(value, tuple): |
| 103 | + return value |
| 104 | + if isinstance(value, int): |
| 105 | + if value >> 24: |
| 106 | + raise ValueError("Only bits 0->23 valid for integer input") |
| 107 | + r = value >> 16 |
| 108 | + g = (value >> 8) & 0xFF |
| 109 | + b = value & 0xFF |
| 110 | + return [r, g, b] |
| 111 | + |
| 112 | + raise ValueError("Color must be a tuple or 24-bit integer value.") |
| 113 | + |
| 114 | + |
| 115 | +def color_fade(start_color: int, end_color: int, fraction: float): |
| 116 | + """Linear extrapolation of a color between two RGB colors (tuple or 24-bit integer). |
| 117 | + :param start_color: starting color |
| 118 | + :param end_color: ending color |
| 119 | + :param fraction: Floating point number ranging from 0 to 1 indicating what |
| 120 | + fraction of interpolation between start_color and end_color. |
| 121 | + """ |
| 122 | + |
| 123 | + start_color = color_to_tuple(start_color) |
| 124 | + end_color = color_to_tuple(end_color) |
| 125 | + if fraction >= 1: |
| 126 | + return end_color |
| 127 | + if fraction <= 0: |
| 128 | + return start_color |
| 129 | + |
| 130 | + faded_color = [0, 0, 0] |
| 131 | + for i in range(3): |
| 132 | + faded_color[i] = start_color[i] - int( |
| 133 | + (start_color[i] - end_color[i]) * fraction |
| 134 | + ) |
| 135 | + return faded_color |
0 commit comments