计算一系列值的RGB值以创建热图

ald*_*ado 23 python rgb colors heatmap linear-interpolation

我正在尝试用python创建一个热图.为此,我必须为可能值范围内的每个值分配RGB值.我想把颜色从蓝色(最小值)变为绿色到红色(最大值).

下面的图片示例解释了我如何考虑颜色组成:我们的范围从1(纯蓝色)到3(纯红色),2之间的颜色类似于绿色.

颜色成分RGB范围(1-3)

我读到了线性插值并编写了一个函数(或多或少)处理最小值和最大值之间的某个值的计算并返回RGB元组.它使用ifelif条件(这不会让我完全开心):

def convert_to_rgb(minimum, maximum, value):
    minimum, maximum = float(minimum), float(maximum)    
    halfmax = (minimum + maximum) / 2
    if minimum <= value <= halfmax:
        r = 0
        g = int( 255./(halfmax - minimum) * (value - minimum))
        b = int( 255. + -255./(halfmax - minimum)  * (value - minimum))
        return (r,g,b)    
    elif halfmax < value <= maximum:
        r = int( 255./(maximum - halfmax) * (value - halfmax))
        g = int( 255. + -255./(maximum - halfmax)  * (value - halfmax))
        b = 0
        return (r,g,b)
Run Code Online (Sandbox Code Playgroud)

但是我想知道是否可以在使用if条件的情况下为每个颜色值编写函数.有人有想法吗?非常感谢!

Joh*_*024 34

def rgb(minimum, maximum, value):
    minimum, maximum = float(minimum), float(maximum)
    ratio = 2 * (value-minimum) / (maximum - minimum)
    b = int(max(0, 255*(1 - ratio)))
    r = int(max(0, 255*(ratio - 1)))
    g = 255 - b - r
    return r, g, b
Run Code Online (Sandbox Code Playgroud)

  • `halfmax` 应该计算为 `(minimum - maximum) / 2` 并且 `value/halfmax` 应该是 `(value - minimum)/halfmax`,否则它只能在 `minimum` 为 1 并且 `maximum` 时正常工作3.见:http://codereview.stackexchange.com/a/64720/7641 (2认同)

mar*_*eau 15

这是另一种方法,虽然不尽可能短,但更为通用,因为它没有针对您的特定颜色进行硬编码.这意味着它还可以用于在任意颜色的可变大小调色板上线性插值指定范围的值.

另请注意,颜色可能已在其他颜色空间中进行插值,从而产生的结果可能比其他颜色空间更令人满意.这是从我提交给题为Rangeocol to pseudocolor的相关问题的两个单独答案中获得的不同结果中说明的.

import sys
EPSILON = sys.float_info.epsilon  # Smallest possible difference.

def convert_to_rgb(minval, maxval, val, colors):
    # "colors" is a series of RGB colors delineating a series of
    # adjacent linear color gradients between each pair.
    # Determine where the given value falls proportionality within
    # the range from minval->maxval and scale that fractional value
    # by the total number in the "colors" pallette.
    i_f = float(val-minval) / float(maxval-minval) * (len(colors)-1)
    # Determine the lower index of the pair of color indices this
    # value corresponds and its fractional distance between the lower
    # and the upper colors.
    i, f = int(i_f // 1), i_f % 1  # Split into whole & fractional parts.
    # Does it fall exactly on one of the color points?
    if f < EPSILON:
        return colors[i]
    else:  # Otherwise return a color within the range between them.
        (r1, g1, b1), (r2, g2, b2) = colors[i], colors[i+1]
        return int(r1 + f*(r2-r1)), int(g1 + f*(g2-g1)), int(b1 + f*(b2-b1))

if __name__ == '__main__':
    minval, maxval = 1, 3
    steps = 10
    delta = float(maxval-minval) / steps
    colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)]  # [BLUE, GREEN, RED]
    print('  Val       R    G    B')
    for i in range(steps+1):
        val = minval + i*delta
        r, g, b = convert_to_rgb(minval, maxval, val, colors)
        print('{:.3f} -> ({:3d}, {:3d}, {:3d})'.format(val, r, g, b))
Run Code Online (Sandbox Code Playgroud)

数字输出:

  Val       R    G    B
1.000 -> (  0,   0, 255)
1.200 -> (  0,  50, 204)
1.400 -> (  0, 101, 153)
1.600 -> (  0, 153, 101)
1.800 -> (  0, 204,  50)
2.000 -> (  0, 255,   0)
2.200 -> ( 51, 203,   0)
2.400 -> (102, 152,   0)
2.600 -> (153, 101,   0)
2.800 -> (203,  51,   0)
3.000 -> (255,   0,   0)
Run Code Online (Sandbox Code Playgroud)

这里的输出可视化为水平渐变:

在回答中用函数生成的水平梯度