import colorsys
def interpolate(fraction, start_color, end_color):
s_r, s_g, s_b = start_color
e_r, e_g, e_b = end_color
new_r = s_r + fraction * (e_r - s_r)
new_g = s_g + fraction * (e_g - s_g)
new_b = s_b + fraction * (e_b - s_b)
return int(new_r), int(new_g), int(new_b)
def rainbow(text):
num_of_colors = sum(1 for char in text if char != ' ' and char != '\n')
gradient_colors = []
for i in range(num_of_colors):
hue = i / num_of_colors
rgb_color = tuple(round(i * 255) for i in colorsys.hsv_to_rgb(hue, 1, 1))
gradient_colors.append(rgb_color)
colored_text = ""
color_index = 0
for char in text:
if char == ' ':
colored_text += " "
continue
if char == '\n':
colored_text += "\n"
continue
hex_color = "#{:02x}{:02x}{:02x}".format(*gradient_colors[color_index])
colored_text += f"%{hex_color}%{char}%%"
color_index += 1
return colored_text
def hex_to_rgb(hex_color):
hex_color = hex_color.lstrip('#')
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
def generate_gradient(text, start_hex, end_hex):
start_color = hex_to_rgb(start_hex)
end_color = hex_to_rgb(end_hex)
num_of_colors = sum(1 for char in text if char != ' ' and char != '\n')
gradient_colors = [interpolate(i / (num_of_colors - 1), start_color, end_color) for i in range(num_of_colors)]
colored_text = ""
color_index = 0
for char in text:
if char == ' ' or char == '\n':
colored_text += char
continue
hex_color = "#{:02x}{:02x}{:02x}".format(*gradient_colors[color_index])
colored_text += f"%{hex_color}%{char}%%"
color_index += 1
return colored_text
input_text = """text example!"""
# also works with multiline strings too :3
start_hex = "#FF0000"
end_hex = "#FFFFFF"
print('\n---\nGRADIENT TEXT:')
print(generate_gradient(input_text, start_hex, end_hex))
print('\n---\nRAINBOW TEXT:')
print(rainbow(input_text))