有没有一种方便的方法可以使用指定的渐变将0,1范围内的浮点值转换为HTML十六进制颜色?
目前,我正在使用:
.myclass {fill:red, fill-opacity:FLOATINGVAL}但这是一个有点限制性的梯度。
发布于 2015-03-07 05:14:12
考虑一下这样的情况:
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 listed1而言,color是渐变条右侧的颜色,而alpha是您在渐变条上的位置(最左侧为0.0,最右侧为1.0 )
1这只适用于两种颜色的渐变--你的“颜色”和“白色”(或者任何你指定的base )(即来自图像的渐变,如Blues,Greens,Grays等)。
你不能用这个函数来生成像YlGnBu这样的渐变。
https://stackoverflow.com/questions/28907480
复制相似问题