如何在Python终端中打印RGBA颜色

问题描述 投票:0回答:1

我尝试使用 ANSI 转义序列在 Python 终端中打印彩色文本。我开始以任何 RGB 颜色打印彩色文本。但我也想改变文本的透明度。有什么办法可以做到这一点吗?

我尝试使用这个:

print("\033[38;2;255;0;0;0.5mRed")

但是它不起作用并且没有改变透明度。 RGB 代码末尾的

0.5
应该充当 Alpha。

python rgba
1个回答
0
投票

要在 Python 终端中打印 RGBA 颜色,您可以使用 ANSI 转义码来更改文本颜色。

def print_rgba_color(text, r, g, b, a):
# ANSI escape code to set text color
color_code = f"\033[38;2;{r};{g};{b};{a}m"

# ANSI escape code to reset text color
reset_code = "\033[0m"

print(f"{color_code}{text}{reset_code}")

红色的 RGBA 颜色值示例 (255, 0, 0, 255)

print_rgba_color("你好,RGBA 颜色!", 255, 0, 0, 255)

© www.soinside.com 2019 - 2024. All rights reserved.