Created
July 2, 2022 18:26
-
-
Save rcassani/f7ef73868b903eeb4370febfaf0cafb8 to your computer and use it in GitHub Desktop.
Picking random colors in RGB space and outer rim of top of the HSV cone
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Random colors for RGB LEDs | |
Random RGB (R=U[0,1), G=U[0,1), B=U[0,1)) can lead to dark and light colors | |
which may not be goal when picking a random color for a RGB LED | |
This shows how to pick a color in the HSV space (H=U[0,1), S=U[0.8,1), V=1]) | |
and convert it to RGB. Note that the Saturation is sampled such that colors | |
are picked from the rim of the Value=1 disk in the HSV space, this to say, | |
close-to-white colors are avoided. | |
@author: rcassani | |
""" | |
from matplotlib import pyplot as plt | |
import matplotlib.patches as mpatch | |
import numpy as np | |
def hsv_to_rgb(h, s, v): | |
# HSV (1.0, 1.0, 1.0) to RGB (1.0, 1.0, 1.0) | |
# See matplotlib.colors import hsv_to_rgb | |
i = (h * 6.0).astype(int) | |
f = (h * 6.0) - i | |
p = v * (1.0 - s) | |
q = v * (1.0 - s * f) | |
t = v * (1.0 - s * (1.0 - f)) | |
i = i % 6 | |
if i == 0: return (v, t, p) | |
if i == 1: return (q, v, p) | |
if i == 2: return (p, v, t) | |
if i == 3: return (p, q, v) | |
if i == 4: return (t, p, v) | |
if i == 5: return (v, p, q) | |
# Create figure and subplots | |
plt.close('all') | |
fig = plt.figure() | |
ax_rgb = plt.subplot(1,2,1) | |
ax_hsv = plt.subplot(1,2,2) | |
# Number of colors (n_cols × n_rows) | |
n_cols = 25 | |
n_rows = 25 | |
np.random.seed(10) | |
rng = np.random.default_rng() | |
for i_col in range(n_cols): | |
for i_row in range(n_cols): | |
# Random RGB | |
rgb_rnd = rng.random(3) # U[0.0, 1.0) | |
# Random HSV to RGB | |
rgb_hsv = hsv_to_rgb(rng.random(1)[0], # h | |
rng.uniform(0.7, 1.0), # s | |
1) # v | |
# Position of patches | |
x_pos = i_col * 2 | |
y_pos = i_row * 2 | |
# Add patches | |
ax_rgb.add_patch(mpatch.Rectangle((x_pos, y_pos), 2, 2, color=rgb_rnd)) | |
ax_hsv.add_patch(mpatch.Rectangle((x_pos, y_pos), 2, 2, color=rgb_hsv)) | |
# Configure axes | |
ax_rgb.title.set_text('Random RGB color: (R=U[0,1), G=U[0,1), B=U[0,1))') | |
ax_rgb.set_xlim(0, 2 * n_cols) | |
ax_rgb.set_ylim(2 * n_rows, 0) | |
ax_rgb.set_aspect('equal') | |
ax_rgb.axis('off') | |
ax_hsv.title.set_text('Random HSV color: (H=U[0,1), S=U[0.8,1), V=1])') | |
ax_hsv.set_xlim(0, 2 * n_cols) | |
ax_hsv.set_ylim(2 * n_rows, 0) | |
ax_hsv.set_aspect('equal') | |
ax_hsv.axis('off') | |
plt.show() |
Author
rcassani
commented
Jul 2, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment