在Python中返回渐变字符串

3

所以这是我想要做的事情...

假设我使用“彩虹”效果,它将使用字体标签生成字符串并赋予“彩虹”效果。但彩虹渐变效果会延伸到文本的长度,不会重复颜色。

这就是我的意思:

字符串“Rainbow”将产生以下代码:

<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>

看到彩虹的7个颜色如何适应文本的7个字母?现在假设文本更长。我的意思是,非常长。
当字符串“Rainboooooooooooooooooow”需要容纳所有渐变色时,它必须以“#ff0000”标记开头,并以“#8b00ff”标记结束,而且不重复任何相同的颜色。这意味着我需要在它们之间“生成”字体颜色。因此,该字符串将会得到类似以下的结果:
<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>

我该怎么用Python实现这个功能?我一直在尝试理解,但是无法做到......希望能得到帮助。

1个回答

1

有趣的问题!我认为实现这个的简单方法是使用 HSL(色相、饱和度和亮度)颜色模型。使用这种颜色模型,我们可以通过改变色相并保持饱和度和亮度不变来获得彩虹的所有颜色。

为了在Python中实现这一点,我们可以利用 colour 模块。使用它,我编写了一个函数,该函数生成彩虹颜色列表,然后编写了第二个函数,将这些 Color 对象转换为它们的十六进制表示:

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]

注意:如果您不想将颜色环绕,即第一个颜色始终为红色,并且使用length-1最后一个颜色也是红色,但只使用length最后一个颜色将是蓝色/紫色。请在get_rainbow_colors中更改(length-1)length。然后,最后我创建了一个函数,它将单词转换为HTML文本,其中每个字符都有不同的颜色。
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

这个函数对单词"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>

使用matplotlib的例子

enter image description here

完整代码

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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接