在Python中返回渐变字符串

2024-10-03 11:13:36 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我要做的是。。。你知道吗

假设我使用了“彩虹”效果,它将使用字体标记生成字符串并给它一个“彩虹”效果。但是彩虹渐变效果会延伸到文本的长度,并且颜色不会重复。你知道吗

我的意思是:

字符串“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个字母相匹配?现在让我们假设文本更长。我是说,再长一点。你知道吗

字符串“rainboooow”需要适应其中的所有渐变,同时以“#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中执行此操作?我一直在想办法,但我就是不能。。。我们将不胜感激。你知道吗


Tags: 字符串代码标记文本颜色字母字体color
1条回答
网友
1楼 · 发布于 2024-10-03 11:13:36

有趣的问题!我认为一个简单的方法是使用hue, saturation and luminance (or lightness) (HSL) color model。利用这种颜色模型,通过改变彩虹的色调,保持彩虹的饱和度和亮度不变,可以得到彩虹的所有颜色。你知道吗

为了在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]

注意:如果您不想将颜色环绕,请将get_rainbow_colors中的(length - 1)更改为length,即第一种颜色始终为红色,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()

相关问题 更多 >