Skip to content

Commit

Permalink
adding color map
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Mar 6, 2023
1 parent df23352 commit 75e5b2e
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 9 deletions.
135 changes: 135 additions & 0 deletions circuitpython_uplot/umap.py
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
6 changes: 3 additions & 3 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

.. If you created a package, create one automodule per module in the package.
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
.. use this format as the module name: "adafruit_foo.foo"
.. automodule:: circuitpython_uplot.uplot
:members:

Expand All @@ -21,3 +18,6 @@

.. automodule:: circuitpython_uplot.ufillbetween
:members:

.. automodule:: circuitpython_uplot.umap
:members:
10 changes: 10 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,13 @@ example of uboxplot integration with uplot
:caption: examples/uplot_uboxplot.py
:linenos:
.. image:: ../docs/uplot_ex16.jpg

Umap Example
---------------------------

umap simple example

.. literalinclude:: ../examples/uplot_umap.py
:caption: examples/uplot_umap.py
:linenos:
.. image:: ../docs/uplot_ex17.jpg
35 changes: 35 additions & 0 deletions docs/quick_start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,38 @@ it will fill the area between two curves:
ufillbetween(plot, x, y1, y2)
display.show(plot)
===============
Color Map
===============

Allows you to graph color maps. You just need to give the values in a ulab.numpy.array.
You can choose the initial and final colors for the color map.

.. code-block:: python
import board
from ulab import numpy as np
from circuitpython_uplot.uplot import Uplot
from circuitpython_uplot.umap import umap
display = board.DISPLAY
plot = Uplot(0, 0, display.width, display.height, show_box=False)
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,
)
umap(plot, x, 0xFF0000, 0x0000FF)
display.show(plot)
Binary file added docs/uplot_ex17.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/uplot_ex17.jpg.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2023 Jose David M.

SPDX-License-Identifier: MIT
7 changes: 2 additions & 5 deletions examples/uplot_readme_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
# Setting up tick parameters
plot3.tick_params(tickheight=12, tickcolor=0xFF00FF, tickgrid=True)

# Seeting some date to plot
# Seeting some data to plot
x = np.linspace(-4, 4, num=50)
constant = 1.0 / np.sqrt(2 * np.pi)
y = constant * np.exp((-(x**2)) / 2.0)
Expand All @@ -53,12 +53,9 @@

plot.append(plot4)

# Setting up the display

plot5 = Uplot(0, 180, 120, 120)

# Setting up tick parameters

plot5.axs_params(axstype="cartesian")
a = np.linspace(3, 98)
b = [choice(a) for _ in a]
Expand Down Expand Up @@ -93,5 +90,5 @@

plot.append(plot7)


# Plotting and showing the plot
display.show(plot)
5 changes: 4 additions & 1 deletion examples/uplot_ucartesian_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
constant = 1.0 / np.sqrt(2 * np.pi)
y = constant * np.exp((-(x**2)) / 2.0)

# Drawing the graphm
# Drawing the graph
ucartesian(plot, x, y, rangex=[-5, 5], rangey=[0, 1], line_color=0xFF0000)

# Creating some points to graph
Expand All @@ -26,6 +26,9 @@
y = constant * np.exp((-(x**2)) / 2.0)
ucartesian(plot, x, y, rangex=[-5, 5], rangey=[0, 1], line_color=0x00FF00)

# Plotting and showing the plot
display.show(plot)

# Adding some wait time
while True:
time.sleep(1)
39 changes: 39 additions & 0 deletions examples/uplot_umap.py
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)

0 comments on commit 75e5b2e

Please sign in to comment.