python
import colorsys
from collections.abc import Iterable
def mix_hls_in_rgb(
hls_colors: Iterable[tuple],
ratio: float = 0.5,
fmt: str = "hex"
) -> str:
"""
hls_colors : iterable で長さ 2、要素は (h,l,s) それぞれ 0–1 float
例 [(h1,l1,s1), (h2,l2,s2)]
ratio : 0.0 → 100% 1 色目, 1.0 → 100% 2 色目
fmt : 'hex' なら '#RRGGBB', それ以外なら 'rgb(r,g,b)'
"""
# --- 入力チェック & アンパック -------------------------------------------
if not isinstance(hls_colors, Iterable):
raise TypeError("hls_colors must be an iterable containing two (h,l,s) tuples")
try:
(h1, l1, s1), (h2, l2, s2) = hls_colors
except ValueError:
raise ValueError("hls_colors must contain exactly two (h,l,s) tuples")
r = max(0.0, min(1.0, float(ratio))) # 0–1 クランプ
# --- HLS → RGB (浮動小数, 0–1) -----------------------------------------
rgb1 = colorsys.hls_to_rgb(h1, l1, s1) # (r1,g1,b1)
rgb2 = colorsys.hls_to_rgb(h2, l2, s2) # (r2,g2,b2)
# --- RGB 空間で線形補間 -------------------------------------------------
r_mix = rgb1[0] + (rgb2[0] - rgb1[0]) * r
g_mix = rgb1[1] + (rgb2[1] - rgb1[1]) * r
b_mix = rgb1[2] + (rgb2[2] - rgb1[2]) * r
# --- 0–255 integer へ & 文字列化 ----------------------------------------
r_i, g_i, b_i = [round(x * 255) for x in (r_mix, g_mix, b_mix)]
if fmt == "hex":
return f"#{r_i:02X}{g_i:02X}{b_i:02X}"
else:
return f"rgb({r_i}, {g_i}, {b_i})"
python
hls1 = (0.25, 0.6, 1.0) # 蛍光グリーン (Hue ≒ 90°)
hls2 = (0.92, 0.6, 1.0) # 蛍光ピンク (Hue ≒ 331°)
# iterable で渡す
print(mix_hls_in_rgb([hls1, hls2], 0.3)) # => '#83FF7F'
print(mix_hls_in_rgb((hls1, hls2), 0.7, 'css')) # => 'rgb(255, 90, 191)'