python表⽰颜⾊的代码_Python的渐变⾊代码该怎么写?import seaborn as sns
import matplotlib.pyplot as plt
# RGB格式颜⾊转换为16进制颜⾊格式
def RGB_to_Hex(rgb):
RGB = rgb.split(',') # 将RGB格式划分开来
color = '#'
for i in RGB:
num = int(i)
# 将R、G、B分别转化为16进制拼接转换并⼤写 hex() 函数⽤于将10进制整数转换成16进制,以字符串形式表⽰
color += str(hex(num))[-2:].replace('x', '0').upper()
print(color)
return color
# RGB格式颜⾊转换为16进制颜⾊格式python新手代码错了应该怎么改
def RGB_list_to_Hex(RGB):
# RGB = rgb.split(',') # 将RGB格式划分开来
color = '#'
for i in RGB:
num = int(i)
# 将R、G、B分别转化为16进制拼接转换并⼤写 hex() 函数⽤于将10进制整数转换成16进制,以字符串形式表⽰
color += str(hex(num))[-2:].replace('x', '0').upper()
print(color)
return color
# 16进制颜⾊格式颜⾊转换为RGB格式
def Hex_to_RGB(hex):
r = int(hex[1:3], 16)
g = int(hex[3:5], 16)
b = int(hex[5:7], 16)
rgb = str(r) + ',' + str(g) + ',' + str(b)
print(rgb)
return rgb, [r, g, b]
# ⽣成渐变⾊
def gradient_color(color_list, color_sum=700):
color_center_count = len(color_list)
# if color_center_count == 2:
# color_center_count = 1
color_sub_count = int(color_sum / (color_center_count - 1))
color_index_start = 0
color_map = []
for color_index_end in range(1, color_center_count):
color_rgb_start = Hex_to_RGB(color_list[color_index_start])[1]
color_rgb_end = Hex_to_RGB(color_list[color_index_end])[1]
r_step = (color_rgb_end[0] - color_rgb_start[0]) / color_sub_count
g_step = (color_rgb_end[1] - color_rgb_start[1]) / color_sub_count
b_step = (color_rgb_end[2] - color_rgb_start[2]) / color_sub_count
# ⽣成中间渐变⾊
now_color = color_rgb_start
color_map.append(RGB_list_to_Hex(now_color))
for color_index in range(1, color_sub_count):
now_color = [now_color[0] + r_step, now_color[1] + g_step, now_color[2] + b_step]
color_map.append(RGB_list_to_Hex(now_color))
color_index_start = color_index_end
return color_map
if __name__ == '__main__':
# input_colors = ["#40FAFF", "#00EBEB", "#00EB00", "#FFC800", "#FC9600", "#FA0000", "#C800FA", "#FF64FF"] input_colors = ["#00e400", "#ffff00", "#ff7e00", "#ff0000", "#99004c", "#7e0023"]
colors = gradient_color(input_colors)
sns.palplot(colors)
print(len(colors))
plt.show()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。