So here's what i'm trying to do...
lets say I use a "rainbow" effect, which will produce the string using font tags and give it a "rainbow" effect. But the rainbow gradient effect would stretch itself to the length of the text, and no colors would repeat.
Here's what I mean:
the string 'Rainbow' would result in this code:
<font color="#ff0000">R</font><font color="#ff7f00">a</font><font color="#ffff00">i</font><font color="#00ff00">n</font><font color="#00ffff">b</font><font color="#0000ff">o</font><font color="#8b00ff">w</font>
see how all 7 colors of the rainbow fit all 7 letters of the text? Now lets say the text was longer. I mean, waaay longer.
The string 'Rainboooooooooooooooooow' would need to fit all the gradient in there while starting with the "#ff0000" tag and ending with the "#8b00ff" tag, without repeating any of the same colors. This means that I would need to somehow "generate" font colors in between. So this string would result in something along the lines of:
<font color="#ff0000">R</font><font color="#ff2000">a</font><font color="#ff4000">i</font><font color="#ff5f00">n</font><font color="#ff7f00">b</font><font color="#ff9f00">o</font><font color="#ffbf00">o</font><font color="#ffdf00">o</font><font color="#ffff00">o</font><font color="#bfff00">o</font><font color="#80ff00">o</font><font color="#40ff00">o</font><font color="#00ff00">o</font><font color="#00ff55">o</font><font color="#00ffaa">o</font><font color="#00ffff">o</font><font color="#00bfff">o</font><font color="#0080ff">o</font><font color="#0040ff">o</font><font color="#0000ff">o</font><font color="#2300ff">o</font><font color="#4600ff">o</font><font color="#6800ff">o</font><font color="#8b00ff">w</font>
How can I do this in python? I've been trying to figure it out but I just can't... help would be appreciated.
Fun question! I think an easy way to do this is to use the hue, saturation and luminance (or lightness) (HSL) color model. Using this color model we can get all the colors of the rainbow by varying the hue and keeping the saturation and luminance constant.
In order to implement this in Python we can make use of the colour module. Using this I made a function which makes a list of rainbow colors and then a second function which converts these Color objects to their hexadecimal representation:
def get_rainbow_colors(length):
""" Makes a list of rainbow Colors. """
return [
Color(hue=i/(length - 1), saturation=1, luminance=0.5)
for i in range(length)]
def convert_to_hex_colors(colors):
""" Convert a list of Color objects to hexadecimal colors. """
return [color.get_hex_l() for color in colors]
Note: change (length - 1) to length in get_rainbow_colors if you do not want to wrap the colors around, i.e. the first color is always red and with length - 1 the last one is also red, but with just length the last color will be blue/purpleish.
Then finally I made a function which converts a word into a HTML text where each character has a separate color.
def convert_to_rainbow_html_string(word):
""" Returns a HTML text where the colors of the characters make a rainbow. """
rainbow_colors = get_rainbow_colors(len(word))
rainbow_colors = convert_to_hex_colors(rainbow_colors)
html_str = ''
for color, character in zip(rainbow_colors, word):
html_str += '<font color="' + color + '">' + character + '</font>'
return html_str
This function gives the following output for the word rainbow: <font color="#ff0000">r</font><font color="#ffff00">a</font><font color="#00ff00">i</font><font color="#00ffff">n</font><font color="#0000ff">b</font><font color="#ff00ff">o</font><font color="#ff0000">w</font>

from colour import Color
import matplotlib.pyplot as plt
def get_rainbow_colors(length):
""" Makes a list of rainbow Colors. """
return [
Color(hue=i/(length - 1), saturation=1, luminance=0.5)
for i in range(length)]
def convert_to_hex_colors(colors):
""" Convert a list of Color objects to hexadecimal colors. """
return [color.get_hex_l() for color in colors]
def convert_to_rainbow_html_string(word):
""" Returns a HTML text where the colors of the characters make a rainbow. """
rainbow_colors = get_rainbow_colors(len(word))
rainbow_colors = convert_to_hex_colors(rainbow_colors)
html_str = ''
for color, character in zip(rainbow_colors, word):
html_str += '<font color="' + color + '">' + character + '</font>'
return html_str
print(convert_to_rainbow_html_string('rainbow'))
# Example with matplotlib
words = ["Text", "Longertext", "veryveryverylongtext"]
for y, word in enumerate(words):
rainbow_colors = get_rainbow_colors(len(word))
rainbow_colors = convert_to_hex_colors(rainbow_colors)
# Plot characters of a word in rainbow colors
for i, color in enumerate(rainbow_colors):
plt.text(i/len(rainbow_colors), 0.1 - y/10, word[i], color=color, size=18)
plt.xlim([-0.1, 1.1])
plt.ylim([-0.2, 0.2])
plt.axis('off')
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With