0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

feat(utils.colors): golden_angle_color utility can return colors as a list of QColor, RGB or HEC

This commit is contained in:
wyzula-jan
2024-02-16 20:16:19 +01:00
parent 8a4aeb8dfe
commit 51259097fa

View File

@ -1,6 +1,8 @@
from typing import Literal
import numpy as np import numpy as np
import pyqtgraph as pg import pyqtgraph as pg
from pyqtgraph import mkColor from qtpy.QtGui import QColor
class Colors: class Colors:
@ -10,6 +12,9 @@ class Colors:
Args: Args:
num (int): Number of angles num (int): Number of angles
Returns:
list: List of angles calculated using the golden ratio.
""" """
phi = 2 * np.pi * ((1 + np.sqrt(5)) / 2) phi = 2 * np.pi * ((1 + np.sqrt(5)) / 2)
angles = [] angles = []
@ -21,30 +26,40 @@ class Colors:
return angles return angles
@staticmethod @staticmethod
def golden_angle_color(colormap: str, num: int) -> list: def golden_angle_color(
colormap: str, num: int, format: Literal["QColor", "HEX", "RGB"] = "QColor"
) -> list:
""" """
Extract num colors for from the specified colormap following golden angle distribution. Extract num colors from the specified colormap following golden angle distribution and return them in the specified format.
Args: Args:
colormap (str): Name of the colormap colormap (str): Name of the colormap.
num (int): Number of requested colors num (int): Number of requested colors.
format (Literal["QColor","HEX","RGB"]): The format of the returned colors ('RGB', 'HEX', 'QColor').
Returns: Returns:
list: List of colors with length <num> list: List of colors in the specified format.
Raises: Raises:
ValueError: If the number of requested colors is greater than the number of colors in the colormap. ValueError: If the number of requested colors is greater than the number of colors in the colormap.
""" """
cmap = pg.colormap.get(colormap) cmap = pg.colormap.get(colormap)
cmap_colors = cmap.color cmap_colors = cmap.getColors(mode="float")
if num > len(cmap_colors): if num > len(cmap_colors):
raise ValueError( raise ValueError(
f"Number of colors requested ({num}) is greater than the number of colors in the colormap ({len(cmap_colors)})" f"Number of colors requested ({num}) is greater than the number of colors in the colormap ({len(cmap_colors)})"
) )
angles = Colors.golden_ratio(len(cmap_colors)) angles = Colors.golden_ratio(len(cmap_colors))
color_selection = np.round(np.interp(angles, (-np.pi, np.pi), (0, len(cmap_colors)))) color_selection = np.round(np.interp(angles, (-np.pi, np.pi), (0, len(cmap_colors))))
colors = [ colors = []
mkColor(tuple((cmap_colors[int(ii)] * 255).astype(int))) for ii in color_selection[:num] for ii in color_selection[:num]:
] color = cmap_colors[int(ii)]
if format.upper() == "HEX":
colors.append(QColor.fromRgbF(*color).name())
elif format.upper() == "RGB":
colors.append(tuple((np.array(color) * 255).astype(int)))
elif format.upper() == "QCOLOR":
colors.append(QColor.fromRgbF(*color))
else:
raise ValueError("Unsupported format. Please choose 'RGB', 'HEX', or 'QColor'.")
return colors return colors