Spaces:
Sleeping
Sleeping
| import colorsys | |
| def hex_to_rgb(hex_color): | |
| """Convert hex color to RGB tuple.""" | |
| hex_color = hex_color.lstrip('#') | |
| return tuple(int(hex_color[i:i+2], 16) / 255.0 for i in (0, 2, 4)) | |
| def rgb_to_hex(rgb_color): | |
| """Convert RGB tuple to hex color.""" | |
| return "#{:02x}{:02x}{:02x}".format(int(rgb_color[0] * 255), int(rgb_color[1] * 255), int(rgb_color[2] * 255)) | |
| def interpolate_color(val, minval, maxval, start_hex, end_hex): | |
| """Interpolate color between two hex colors given a value between minval and maxval.""" | |
| start_color = hex_to_rgb(start_hex) | |
| end_color = hex_to_rgb(end_hex) | |
| # Normalize value to range 0-1 | |
| f = float(val - minval) / (maxval - minval) | |
| # Convert start and end colors to HSV | |
| start_color_hsv = colorsys.rgb_to_hsv(*start_color) | |
| end_color_hsv = colorsys.rgb_to_hsv(*end_color) | |
| # Interpolate HSV values | |
| interpolated_hsv = tuple(start + f * (end - start) for start, end in zip(start_color_hsv, end_color_hsv)) | |
| # Convert interpolated HSV back to RGB | |
| interpolated_rgb = colorsys.hsv_to_rgb(*interpolated_hsv) | |
| # Convert RGB values to hex | |
| return rgb_to_hex(interpolated_rgb) | |
| def reduce_hsv_color_v(hex_color, factor): | |
| """Reduce the value (brightness) of a color in HSV color space.""" | |
| rgb_color = hex_to_rgb(hex_color) | |
| hsv_color = colorsys.rgb_to_hsv(*rgb_color) | |
| reduced_hsv_color = (hsv_color[0], hsv_color[1] * factor, hsv_color[2]) | |
| reduced_rgb_color = colorsys.hsv_to_rgb(*reduced_hsv_color) | |
| return rgb_to_hex(reduced_rgb_color) | |