mar*_*eau 17
您可以编写自己的函数,将值转换为0 ... 100 => 0 ... 120度,然后将该值用作HSV(或HLS)颜色空间中颜色的H(或角度).然后可以将其转换为RGB颜色以用于显示目的.当在这个颜色空间中计算线性解释的颜色时,它们通常看起来更好:这是HSV颜色空间的样子:
更新:
好消息,我惊喜地发现Python在其内置colorsys
模块中具有色彩空间转换程序(它们实际上意味着"包括电池").有趣的是它创建了一个能够完成我所描述的相当简单的功能,如下图所示:
from colorsys import hsv_to_rgb
def pseudocolor(val, minval, maxval):
""" Convert val in range minval..maxval to the range 0..120 degrees which
correspond to the colors Red and Green in the HSV colorspace.
"""
h = (float(val-minval) / (maxval-minval)) * 120
# Convert hsv color (h,1,1) to its rgb equivalent.
# Note: hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
r, g, b = hsv_to_rgb(h/360, 1., 1.)
return r, g, b
if __name__ == '__main__':
steps = 10
print('val R G B')
for val in range(0, 100+steps, steps):
print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
val, *pseudocolor(val, 0, 100)))
Run Code Online (Sandbox Code Playgroud)
输出:
val R G B
0 -> (1.000, 0.000, 0.000)
10 -> (1.000, 0.200, 0.000)
20 -> (1.000, 0.400, 0.000)
30 -> (1.000, 0.600, 0.000)
40 -> (1.000, 0.800, 0.000)
50 -> (1.000, 1.000, 0.000)
60 -> (0.800, 1.000, 0.000)
70 -> (0.600, 1.000, 0.000)
80 -> (0.400, 1.000, 0.000)
90 -> (0.200, 1.000, 0.000)
100 -> (0.000, 1.000, 0.000)
Run Code Online (Sandbox Code Playgroud)
这是一个显示其输出结果的示例:
我想你可能会发现颜色比我的其他答案更好.
归纳:
可以将此功能修改为更通用,因为它可以使用除了当前硬编码到其中的红色和绿色之外的颜色.
以下是如何做到这一点:
def pseudocolor(val, minval, maxval, start_hue, stop_hue):
""" Convert val in range minval..maxval to the range start_hue..stop_hue
degrees in the HSV colorspace.
"""
h = (float(val-minval) / (maxval-minval)) * (stop_hue-start_hue) + start_hue
# Convert hsv color (h,1,1) to its rgb equivalent.
# Note: hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
r, g, b = hsv_to_rgb(h/360, 1., 1.)
return r, g, b
if __name__ == '__main__':
# angles of common colors in hsv colorspace
RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA = range(0, 360, 60)
steps = 10
print('val R G B')
for val in range(0, 100+steps, steps):
print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
val, *pseudocolor(val, 0, 100, YELLOW, BLUE)))
Run Code Online (Sandbox Code Playgroud)
结果:
虽然不如在HLS或HSV颜色空间中插值H那么漂亮,但实现方法要简单得多,就是编写一个函数,将单个值映射成三个分量,对应于RGB中完全red (1,0,0)
和完全绿色之间的线性插值颜色.(0,1,0)
色彩空间.
这就是我的意思:
def pseudocolor(val, minval, maxval):
""" Convert value in the range minval...maxval to a color between red
and green.
"""
f = float(val-minval) / (maxval-minval)
r, g, b = 1-f, f, 0.
return r, g, b
if __name__ == '__main__':
steps = 10
print('val R G B')
for val in xrange(0, 100+steps, steps):
print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
val, *pseudocolor(val, 0, 100)))
Run Code Online (Sandbox Code Playgroud)
输出:
val R G B
0 -> (1.000, 0.000, 0.000)
10 -> (0.900, 0.100, 0.000)
20 -> (0.800, 0.200, 0.000)
30 -> (0.700, 0.300, 0.000)
40 -> (0.600, 0.400, 0.000)
50 -> (0.500, 0.500, 0.000)
60 -> (0.400, 0.600, 0.000)
70 -> (0.300, 0.700, 0.000)
80 -> (0.200, 0.800, 0.000)
90 -> (0.100, 0.900, 0.000)
100 -> (0.000, 1.000, 0.000)
Run Code Online (Sandbox Code Playgroud)
您可以根据需要变换浮点r,g,b分量,例如0到255范围内的整数.
这是一个显示其输出结果的示例:
如果你想从绿色变为红色,只需反转函数中r和g的计算.无需太多额外工作,您可以概括该概念以允许任何两种给定颜色之间的线性插值.
这是如何做到的:
def pseudocolor(val, minval, maxval, startcolor, stopcolor):
""" Convert value in the range minval...maxval to a color in the range
startcolor to stopcolor. The colors passed and the the one returned are
composed of a sequence of N component values.
"""
f = float(val-minval) / (maxval-minval)
return tuple(f*(b-a)+a for (a, b) in zip(startcolor, stopcolor))
if __name__ == '__main__':
YELLOW, BLUE = (1, 1, 0), (0, 0, 1)
steps = 10
print('val R G B')
for val in range(0, 100+steps, steps):
print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
val, *pseudocolor(val, 0, 100, YELLOW, BLUE)))
Run Code Online (Sandbox Code Playgroud)
使用提供的颜色输出样本:
您可以直接访问matplolib 的内置颜色映射,这正是 pcolor 用于确定其颜色映射的内容。每个映射接受范围 [0, 1] 中的浮点数,并返回范围 [0, 1] 中的浮点数的 4 元素元组以及组件 (R, G, B, A)。以下是使用标准jet
颜色图返回 RGBA 元组的函数示例:
from matplotlib import cm
def pseudocolor(val, minval, maxmal):
# Scale val to be in the range [0, 1]
val = (val - minval) / (maxval - minval)
# Return RGBA tuple from jet colormap
return cm.jet(val)
pseudocolor(20, 0, 100)
# Returns: (0.0, 0.3, 1.0, 1.0)
pseudocolor(80, 0, 100)
# Returns: (1.0, 0.4074, 0.0, 1.0)
Run Code Online (Sandbox Code Playgroud)
这将映射到下图所示的颜色范围。
一个关于这种方法方便快捷的特点是,你可以方便地切换到任何一个matplotlib的colormaps通过改变cm.jet
到cm.rainbow
,cm.nipy_spectral
,cm.Accent
,等。
归档时间: |
|
查看次数: |
6204 次 |
最近记录: |