考虑类似:
def blend(color, alpha, base=[255,255,255]):
'''
color should be a 3-element iterable, elements in [0,255]
alpha should be a float in [0,1]
base should be a 3-element iterable, elements in [0,255] (defaults to white)
'''
out = [int(round((alpha * color[i]) + ((1 - alpha) * base[i]))) for i in range(3)]
return out
print blend([255,0,0], 0) # [255, 255, 255] (White)
print blend([255,0,0], 0.25) # [255, 191, 191]
print blend([255,0,0], 0.5) # [255, 128, 128]
print blend([255,0,0], 0.75) # [255, 64, 64]
print blend([255,0,0], 1) # [255,0,0] (Red)
# Or RGB hex values instead of lists:
def to_hex(color):
return ''.join(["%02x" % e for e in color])
print to_hex(blend([255,0,0], 0)) # ffffff (White)
print to_hex(blend([255,0,0], 0.25)) # ffbfbf
print to_hex(blend([255,0,0], 0.5)) # ff8080
print to_hex(blend([255,0,0], 0.75)) # ff4040
print to_hex(blend([255,0,0], 1)) # ff0000 (Red)
关于此函数如何与gradients you listed[1] 一起使用,color 是渐变条右侧的颜色,而alpha 是您在渐变条上的位置(0.0 最左侧,1.0 远对)
[1] 这仅适用于 2 色渐变 - 您的“颜色”和“白色”(或任何您指定的 base)(即来自该图像的渐变,如 Blues、Greens、 Grays等)
您将无法使用此函数生成像YlGnBu 这样的渐变。