import numpy as npimport matplotlib.pyplot as pltimport colorsysimport matplotlib.colors as mcolors
def get_hls(color):""" Returns the HLS values of a given color. Args: color (str or tuple): A color name (e.g., 'xkcd:hot pink', 'tab:orange'), a hex code (e.g., '#FF69B4'), or an RGB triplet (e.g., (255, 105, 180)). Returns: tuple: A tuple containing the HLS values (hue, lightness, saturation) of the given color. """try:# Convert the input color to RGBifisinstance(color, str):# If the input is a color name, convert it to RGB rgb = mcolors.to_rgb(color)elifisinstance(color, tuple) andlen(color) ==3:# If the input is an RGB triplet, use it directly rgb = colorelse:# If the input is a hex code, convert it to RGB rgb = mcolors.to_rgb(color)# Convert RGB to HSL r, g, b = rgb hls = colorsys.rgb_to_hls(r, g, b)return hlsexceptValueErroras e:print(f"Error: {e}")returnNonedef get_hls2(color):""" Returns the HLS values of a given color. Args: color (str or tuple): A color name (e.g., 'xkcd:hot pink', 'tab:orange'), a hex code (e.g., '#FF69B4'), or an RGB triplet (e.g., (255, 105, 180)). Returns: tuple: A tuple containing the HLS values (hue, lightness, saturation) of the given color. """try:# Convert the input color to RGBifisinstance(color, str):# If the input is a color name, convert it to RGB rgb = mcolors.to_rgb(color) rgb = [int(val *255) for val in rgb]elifisinstance(color, tuple) andlen(color) ==3:# If the input is an RGB triplet, use it directly rgb = colorelse:# If the input is a hex code, convert it to RGB rgb = mcolors.to_rgb(color) rgb = [int(val *255) for val in rgb]# Convert RGB to HSL r, g, b = rgb hls = colorsys.rgb_to_hls(r /255, g /255, b /255)return hlsexceptValueErroras e:print(f"Error: {e}")returnNonedef new_range(l): r = np.arange(-1.0, 1.1, 0.1) + lreturn r[(r<=1.0) & (r>=0.0)]def rgb_to_hex(rgb):return'#{:02x}{:02x}{:02x}'.format(int(rgb[0]*255), int(rgb[1]*255), int(rgb[2]*255))def plot_panel(c, type, ax):""" type=0 : hue type=1 : lightness type=2 : saturation """ r = new_range(c[type])iftype==0: a_rgb = [colorsys.hls_to_rgb( *((ri, c[1], c[2]) ) ) for ri in r] type_name ="hue"iftype==1: a_rgb = [colorsys.hls_to_rgb( *((c[0], ri, c[2]) ) ) for ri in r] type_name ="lightness"iftype==2: a_rgb = [colorsys.hls_to_rgb( *((c[0], c[1], ri) ) ) for ri in r] type_name ="saturation" a_hex = [rgb_to_hex(i) for i in a_rgb] ax[type].imshow([a_rgb], interpolation='none')# Minor ticks ax[type].set_xticks(np.arange(-.5, len(a_rgb), 1), minor=True)# Gridlines based on minor ticks ax[type].grid(which='minor', color='w', linestyle='-', linewidth=2)# Remove minor ticks ax[type].tick_params(which='minor', bottom=False, left=False) ax[type].set_xticks(np.arange(len(a_rgb))) ax[type].set_xticklabels(a_hex, rotation=30); ax[type].set_yticks([]) ax[type].text(1.02, 0.5, type_name, transform=ax[type].transAxes, horizontalalignment='left', verticalalignment='center', fontweight="bold") mask = np.abs(r-c[type])<1.0e-8 idx = np.argwhere(mask) ax[type].plot(idx[0], [0], marker='o', markersize=20, markerfacecolor='None', markeredgecolor="white")return a_hexdef pallete(color_name): fig, ax = plt.subplots(3, 1, figsize=(10,6)) c = get_hls(color_name) hex_codes = plot_panel(c, 0, ax)print("hue array:\n", hex_codes) hex_codes = plot_panel(c, 1, ax)print("lightness array:\n", hex_codes) hex_codes = plot_panel(c, 2, ax)print("saturation array:\n", hex_codes) ax[0].set_title("color = "+ color_name)return fig