Helpers/TemperatureExtensions.cs
using Sandbox;
using System;
namespace RotSoft.Lighting;
/// <summary>Temperature to color conversions.</summary>
public static class TemperatureExtensions
{
/// <summary>
/// Converts this temperature to an sRGB color, clamped to <see cref="Temperature.MinTemperature"/>..<see cref="Temperature.MaxTemperature"/>.
/// </summary>
public static Color ToColor( this Temperature temperature )
{
float kelvin = Math.Clamp( temperature.Kelvin, Temperature.MinTemperature, Temperature.MaxTemperature );
float temp = kelvin / 100f;
float r, g, b;
if ( temp <= 66f )
r = 1f;
else
r = Math.Clamp( 1.29293618606f * MathF.Pow( temp - 60f, -0.1332047592f ), 0f, 1f );
if ( temp <= 66f )
g = Math.Clamp( 0.390081578769f * MathF.Log( temp ) - 0.63184144378f, 0f, 1f );
else
g = Math.Clamp( 1.12989086369f * MathF.Pow( temp - 60f, -0.0755148492f ), 0f, 1f );
if ( temp >= 66f )
b = 1f;
else if ( temp <= 19f )
b = 0f;
else
b = Math.Clamp( 0.54320678911f * MathF.Log( temp - 10f ) - 1.19625408914f, 0f, 1f );
return new Color( r, g, b );
}
}