-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df23352
commit 75e5b2e
Showing
9 changed files
with
231 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# SPDX-FileCopyrightText: 2023 Jose D. Montoya | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
|
||
""" | ||
`umap` | ||
================================================================================ | ||
CircuitPython color map graph | ||
* Author(s): Jose D. Montoya | ||
""" | ||
try: | ||
from circuitpython_uplot.uplot import Uplot | ||
except ImportError: | ||
pass | ||
|
||
from ulab import numpy as np | ||
import displayio | ||
from vectorio import Rectangle | ||
|
||
__version__ = "0.0.0+auto.0" | ||
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git" | ||
|
||
|
||
# pylint: disable=too-few-public-methods, invalid-name, duplicate-code, too-many-locals, too-many-arguments | ||
# pylint: disable=unused-variable | ||
class umap: | ||
""" | ||
Main class to display different graphics | ||
""" | ||
|
||
def __init__( | ||
self, | ||
plot: Uplot, | ||
data_points: np.ndarray, | ||
initial_color: int, | ||
final_color: int, | ||
) -> None: | ||
|
||
""" | ||
:param plot: Plot object for the scatter to be drawn | ||
:param data_points: data points to create the color map | ||
:param initial_color: initial color to create the color map | ||
:param final_color: final color to create the color map | ||
""" | ||
|
||
xmin = np.min(data_points) | ||
xmax = np.max(data_points) | ||
|
||
xnorm = np.array(plot.transform(xmin, xmax, 0.0, 1.0, data_points)) | ||
|
||
box_iny = data_points.shape[0] | ||
box_inx = data_points.shape[1] | ||
|
||
width = plot._newxmax - plot._newxmin | ||
height = plot._newymin - plot._newymax | ||
xdist = width // box_inx | ||
ydist = height // box_iny | ||
|
||
palette = displayio.Palette(box_inx * box_iny) | ||
start_color = initial_color | ||
end_color = final_color | ||
counter = 0 | ||
for row in xnorm: | ||
for element in row: | ||
palette[counter] = color_fade(start_color, end_color, element) | ||
counter = counter + 1 | ||
|
||
deltax = plot._newxmin | ||
deltay = plot._newymax | ||
color = 0 | ||
for j in range(box_iny): | ||
for i in range(box_inx): | ||
plot.append( | ||
Rectangle( | ||
pixel_shader=palette, | ||
x=deltax, | ||
y=deltay, | ||
width=xdist, | ||
height=ydist, | ||
color_index=color, | ||
) | ||
) | ||
deltax = deltax + xdist | ||
color = color + 1 | ||
|
||
deltax = plot._newxmin | ||
deltay = deltay + ydist | ||
|
||
|
||
def color_to_tuple(value): | ||
"""Converts a color from a 24-bit integer to a tuple. | ||
:param value: RGB LED desired value - can be a RGB tuple or a 24-bit integer. | ||
""" | ||
if isinstance(value, tuple): | ||
return value | ||
if isinstance(value, int): | ||
if value >> 24: | ||
raise ValueError("Only bits 0->23 valid for integer input") | ||
r = value >> 16 | ||
g = (value >> 8) & 0xFF | ||
b = value & 0xFF | ||
return [r, g, b] | ||
|
||
raise ValueError("Color must be a tuple or 24-bit integer value.") | ||
|
||
|
||
def color_fade(start_color: int, end_color: int, fraction: float): | ||
"""Linear extrapolation of a color between two RGB colors (tuple or 24-bit integer). | ||
:param start_color: starting color | ||
:param end_color: ending color | ||
:param fraction: Floating point number ranging from 0 to 1 indicating what | ||
fraction of interpolation between start_color and end_color. | ||
""" | ||
|
||
start_color = color_to_tuple(start_color) | ||
end_color = color_to_tuple(end_color) | ||
if fraction >= 1: | ||
return end_color | ||
if fraction <= 0: | ||
return start_color | ||
|
||
faded_color = [0, 0, 0] | ||
for i in range(3): | ||
faded_color[i] = start_color[i] - int( | ||
(start_color[i] - end_color[i]) * fraction | ||
) | ||
return faded_color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SPDX-FileCopyrightText: 2023 Jose David M. | ||
|
||
SPDX-License-Identifier: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import time | ||
import board | ||
from ulab import numpy as np | ||
from circuitpython_uplot.uplot import Uplot | ||
from circuitpython_uplot.umap import umap | ||
|
||
|
||
# Setting up the display | ||
display = board.DISPLAY | ||
|
||
# Adding the plot area | ||
plot = Uplot(0, 0, display.width, display.height, show_box=False) | ||
|
||
# Setting some date to plot | ||
x = np.array( | ||
[ | ||
[1, 3, 9, 25], | ||
[12, 8, 4, 2], | ||
[18, 3, 7, 5], | ||
[2, 10, 9, 22], | ||
[8, 8, 14, 12], | ||
[3, 13, 17, 15], | ||
], | ||
dtype=np.int16, | ||
) | ||
|
||
# Plotting and showing the plot | ||
umap(plot, x, 0xFF0000, 0x0000FF) | ||
|
||
# Plotting and showing the plot | ||
display.show(plot) | ||
|
||
# Adding some wait time | ||
while True: | ||
time.sleep(1) |