Display temperature as a color with C#?

后端 未结 5 544
死守一世寂寞
死守一世寂寞 2020-12-15 08:01

Someone knows an algorithm that gets temperatue in Kelvin/Celsius and returns RGB?

Like in thermal cameras.

I found some links :

http://www.brucelind

5条回答
  •  长情又很酷
    2020-12-15 08:35

    The above function overestimate red color when temp > 10000 K. Colors turn to purple when temp>14000. I refitted the data with 7th order polynomials. The coefficients of should be:

    def temp_to_rgb(temp):
        t = temp/1000.
    
        # calculate red
        if t < 6.527:
            red = 1.0
        else:
            coeffs = [  4.93596077e+00,  -1.29917429e+00,
                        1.64810386e-01,  -1.16449912e-02,
                        4.86540872e-04,  -1.19453511e-05,
                        1.59255189e-07,  -8.89357601e-10]
            tt = min(t,40)
            red = poly(coeffs,tt)
        red = max(red,0)
        red = min(red,1)
    
        # calcuate green
        if t < 0.85:
            green = 0.0
        elif t < 6.6:
            coeffs = [ -4.95931720e-01,   1.08442658e+00,
                       -9.17444217e-01,   4.94501179e-01,
                       -1.48487675e-01,   2.49910386e-02,
                       -2.21528530e-03,   8.06118266e-05]
            green = poly(coeffs,t)
        else:
            coeffs = [  3.06119745e+00,  -6.76337896e-01,
                        8.28276286e-02,  -5.72828699e-03,
                        2.35931130e-04,  -5.73391101e-06,
                        7.58711054e-08,  -4.21266737e-10]
            tt = min(t,40)
            green = poly(coeffs,tt)
        green = max(green,0)
        green = min(green,1)
    
        # calculate blue
        if t < 1.9:
            blue = 0.0
        elif t < 6.6:
            coeffs = [  4.93997706e-01,  -8.59349314e-01,
                        5.45514949e-01,  -1.81694167e-01,
                        4.16704799e-02,  -6.01602324e-03,
                        4.80731598e-04,  -1.61366693e-05]
            blue = poly(coeffs,t)
        else:
            blue = 1.0
        blue = max(blue,0)
        blue = min(blue,1)
    
        return (red,green,blue)
    

    Here poly(coeffs,x) = coeffs[0] + coeffs[1]*x + coeffs[2]*x**2 + ...

    Sorry I am not familiar with C# but you can easily read the codes.

    The error is within only 0.5% for most cases and at most 1.2% for red in temp = 6600 K. High order polynomials are adopted here so red and green must keep constant for temp > 40000 K, otherwise strange things will happen.

提交回复
热议问题