import colorsys
def mix_rgb_iterable_to_rgb_string(rgb_iterable, ratios=None):
"""
RGBカラー空間の複数の色を、指定された混合比率で混ぜて、RGBの文字列を生成します。
Args:
rgb_iterable: RGB値のiterable。各要素は(r, g, b)タプルで、各要素は0.0から1.0の範囲。
ratios: 各色の混合比率のiterable。デフォルトは各色が均等に混合されるように設定されます。
Noneの場合、rgb_iterableの要素数に基づいて均等な比率が自動的に生成されます。
比率の合計は1.0である必要はありません。合計が1.0になるように正規化されます。
Returns:
混合後の色のRGB値を表す文字列 (例: "rgb(255, 128, 0)")。
入力が空の場合は"rgb(0, 0, 0)"を返します。
Raises:
ValueError: rgb_iterableの要素とratiosの要素数が一致しない場合。
ValueError: RGB値が0.0から1.0の範囲外の場合。
ValueError: ratiosの値が0.0より小さい場合。
"""
rgb_list = list(rgb_iterable)
if not rgb_list:
return "rgb(0, 0, 0)"
num_colors = len(rgb_list)
# ratiosがNoneの場合、均等な比率を生成
if ratios is None:
ratios = [1.0 / num_colors] * num_colors
else:
ratios = list(ratios) # iterableをリストに変換
if len(ratios) != num_colors:
raise ValueError("The number of colors and ratios must be the same.")
# 入力値の検証
for rgb in rgb_list:
if not all(0.0 <= x <= 1.0 for x in rgb):
raise ValueError("RGB values must be between 0.0 and 1.0")
for ratio in ratios:
if ratio < 0.0:
raise ValueError("Ratios must be non-negative.")
# 比率の正規化
total_ratio = sum(ratios)
if total_ratio == 0:
ratios = [1.0 / num_colors] * num_colors # すべての比率が0の場合、均等な比率を使用
else:
ratios = [r / total_ratio for r in ratios]
# RGB値を線形に混合
r = sum(rgb[0] * ratio for rgb, ratio in zip(rgb_list, ratios))
g = sum(rgb[1] * ratio for rgb, ratio in zip(rgb_list, ratios))
b = sum(rgb[2] * ratio for rgb, ratio in zip(rgb_list, ratios))
# RGB値を0-255の整数に変換し、文字列を作成
r_int = int(r * 255)
g_int = int(g * 255)
b_int = int(b * 255)
return f"rgb({r_int}, {g_int}, {b_int})"
if __name__ == '__main__':
# 例: 赤、緑、青を均等に混合
colors = [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)]
mixed_color = mix_rgb_iterable_to_rgb_string(colors)
print(f"赤、緑、青を均等に混合した色: {mixed_color}") # 出力例: 赤、緑、青を均等に混合した色: rgb(85, 85, 85)
# 例: 赤、緑、青を異なる比率で混合
colors = [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)]
ratios = [0.5, 0.25, 0.25]
mixed_color = mix_rgb_iterable_to_rgb_string(colors, ratios)
print(f"赤、緑、青を異なる比率で混合した色: {mixed_color}") # 出力例: 赤、緑、青を異なる比率で混合した色: rgb(127, 63, 63)
# 例: ジェネレータ式を使用
colors = ((i/255, (255-i)/255, 0.5) for i in range(50, 100, 10)) # 赤から緑へグラデーションする色を生成
mixed_color = mix_rgb_iterable_to_rgb_string(colors)
print(f"ジェネレータ式で生成した色を混合した色: {mixed_color}") # 出力例: ジェネレータ式で生成した色を混合した色: rgb(141, 113, 127)
# 例: 空の入力
colors = []
mixed_color = mix_rgb_iterable_to_rgb_string(colors)
print(f"空の入力を混合した色: {mixed_color}") # 出力例: 空の入力を混合した色: rgb(0, 0, 0)
# 例: ratiosの長さが異なる場合のエラー処理
colors = [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0)]
ratios = [0.5, 0.25, 0.25]
try:
mixed_color = mix_rgb_iterable_to_rgb_string(colors, ratios)
except ValueError as e:
print(f"エラーが発生しました: {e}") # 出力例: エラーが発生しました: The number of colors and ratios must be the same.
# 例: 無効なRGB値
colors = [(1.0, 0.0, 0.0), (0.0, 1.5, 0.0)]
try:
mixed_color = mix_rgb_iterable_to_rgb_string(colors)
except ValueError as e:
print(f"エラーが発生しました: {e}") # 出力例: エラーが発生しました: RGB values must be between 0.0 and 1.0
import colorsys
def mix_hls_iterable_to_rgb_string(hls_iterable, ratios=None):
"""
HLSカラー空間の複数の色を、指定された混合比率で混ぜて、RGBの文字列を生成します。
Args:
hls_iterable: HLS値のiterable。各要素は(hue, lightness, saturation)タプルで、各要素は0.0から1.0の範囲。
ratios: 各色の混合比率のiterable。デフォルトは各色が均等に混合されるように設定されます。
Noneの場合、hls_iterableの要素数に基づいて均等な比率が自動的に生成されます。
比率の合計は1.0である必要はありません。合計が1.0になるように正規化されます。
Returns:
混合後の色のRGB値を表す文字列 (例: "rgb(255, 128, 0)")。
入力が空の場合は"rgb(0, 0, 0)"を返します。
Raises:
ValueError: hls_iterableの要素とratiosの要素数が一致しない場合。
ValueError: HLS値が0.0から1.0の範囲外の場合。
ValueError: ratiosの値が0.0より小さい場合。
"""
hls_list = list(hls_iterable)
if not hls_list:
return "rgb(0, 0, 0)"
num_colors = len(hls_list)
# ratiosがNoneの場合、均等な比率を生成
if ratios is None:
ratios = [1.0 / num_colors] * num_colors
else:
ratios = list(ratios) # iterableをリストに変換
if len(ratios) != num_colors:
raise ValueError("The number of colors and ratios must be the same.")
# 入力値の検証
for hls in hls_list:
if not all(0.0 <= x <= 1.0 for x in hls):
raise ValueError("HLS values must be between 0.0 and 1.0")
for ratio in ratios:
if ratio < 0.0:
raise ValueError("Ratios must be non-negative.")
# 比率の正規化
total_ratio = sum(ratios)
if total_ratio == 0:
ratios = [1.0 / num_colors] * num_colors # すべての比率が0の場合、均等な比率を使用
else:
ratios = [r / total_ratio for r in ratios]
# HLS値を線形に混合
h = sum(hls[0] * ratio for hls, ratio in zip(hls_list, ratios)) % 1.0 # Hueは周期的な値なので、1を超えたら0に戻す
l = sum(hls[1] * ratio for hls, ratio in zip(hls_list, ratios))
s = sum(hls[2] * ratio for hls, ratio in zip(hls_list, ratios))
# HLSからRGBに変換
r, g, b = colorsys.hls_to_rgb(h, l, s)
# RGB値を0-255の整数に変換し、文字列を作成
r_int = int(r * 255)
g_int = int(g * 255)
b_int = int(b * 255)
return f"rgb({r_int}, {g_int}, {b_int})"
if __name__ == '__main__':
# 例: 赤、緑、青を均等に混合 (HLS)
colors = [(0.0, 0.5, 1.0), (0.333, 0.5, 1.0), (0.666, 0.5, 1.0)] # 赤、緑、青のHLS値
mixed_color = mix_hls_iterable_to_rgb_string(colors)
print(f"赤、緑、青を均等に混合した色 (HLS): {mixed_color}")
# 例: 赤、緑、青を異なる比率で混合 (HLS)
colors = [(0.0, 0.5, 1.0), (0.333, 0.5, 1.0), (0.666, 0.5, 1.0)]
ratios = [0.5, 0.25, 0.25]
mixed_color = mix_hls_iterable_to_rgb_string(colors, ratios)
print(f"赤、緑、青を異なる比率で混合した色 (HLS): {mixed_color}")
# 例: ジェネレータ式を使用 (HLS)
colors = ((i/255, 0.5, 1.0) for i in range(0, 255, 50)) # Hueを変化させるHLS値を生成
mixed_color = mix_hls_iterable_to_rgb_string(colors)
print(f"ジェネレータ式で生成したHLS値を混合した色: {mixed_color}")
# 例: 空の入力
colors = []
mixed_color = mix_hls_iterable_to_rgb_string(colors)
print(f"空の入力を混合した色: {mixed_color}")
# 例: ratiosの長さが異なる場合のエラー処理
colors = [(0.0, 0.5, 1.0), (0.333, 0.5, 1.0)]
ratios = [0.5, 0.25, 0.25]
try:
mixed_color = mix_hls_iterable_to_rgb_string(colors, ratios)
except ValueError as e:
print(f"エラーが発生しました: {e}")
# 例: 無効なHLS値
colors = [(0.0, 0.5, 1.0), (0.333, 1.5, 1.0)]
try:
mixed_color = mix_hls_iterable_to_rgb_string(colors)
except ValueError as e:
print(f"エラーが発生しました: {e}")