ファイル

87 行
4.8 KiB
C#

using System.Numerics;
namespace SpectrumColorShift;
internal static class SpectralBasis
{
public const double First = 400.0;
public const double Interval = 5.0;
public const int Count = 61;
private static ReadOnlySpan<float> Red =>
[
0.192512361f, 0.16935988f, 0.146196031f, 0.123059145f, 0.100109343f, 0.0777127425f, 0.0564703934f, 0.0371695051f,
0.0207353365f, 0.00830780452f, 0.00137841186f, 7.5479875e-07f, 9.01286255e-07f, 1.06360691e-06f, 5.45645767e-07f, 3.57413785e-07f,
2.57572257e-07f, 2.02100568e-07f, 1.641348e-07f, 1.31969263e-07f, 1.0800669e-07f, 9.0420405e-08f, 7.66890265e-08f, 6.68081003e-08f,
5.99273434e-08f, 5.42758659e-08f, 4.43178079e-08f, 4.17441579e-08f, 4.91125451e-08f, 5.06339275e-08f, 4.67123455e-08f, 6.02243933e-08f,
3.32734569e-08f, 0.00218982151f, 0.0301830399f, 0.0943962603f, 0.194844971f, 0.323868219f, 0.468912693f, 0.615343668f,
0.748929449f, 0.858055159f, 0.935683229f, 0.980840619f, 0.99893757f, 0.999999877f, 0.999999636f, 0.999999509f,
0.999999413f, 0.999999252f, 0.999999011f, 0.999998673f, 0.999998148f, 0.999997337f, 0.99999599f, 0.999993519f,
0.99998865f, 0.999971924f, 0.99992753f, 0.999857504f, 0.99977574f,
];
private static ReadOnlySpan<float> Green =>
[
1.81116187e-05f, 4.76598665e-06f, 2.06583504e-06f, 1.14787887e-06f, 7.01859312e-07f, 4.9802927e-07f, 3.91446971e-07f, 2.9659085e-07f,
2.51710459e-07f, 1.52383012e-07f, 9.82642489e-08f, 0.0030716171f, 0.0200009702f, 0.0564915036f, 0.114178186f, 0.191829222f,
0.285986432f, 0.391619914f, 0.502778259f, 0.613202335f, 0.716844931f, 0.808340572f, 0.883459214f, 0.939549555f,
0.975987013f, 0.994593092f, 0.999999794f, 0.999999852f, 0.999999666f, 0.99999969f, 0.999999848f, 0.999548594f,
0.992259651f, 0.973278248f, 0.929308352f, 0.85554009f, 0.753853787f, 0.631069348f, 0.497157023f, 0.363430426f,
0.240949244f, 0.139096042f, 0.0643157669f, 0.0191591542f, 0.00106195131f, 4.56685713e-08f, 1.58152571e-07f, 2.22023037e-07f,
2.65629655e-07f, 3.38276201e-07f, 4.47483004e-07f, 6.00414965e-07f, 8.37904682e-07f, 1.20521489e-06f, 1.81520325e-06f, 2.94309258e-06f,
5.13254026e-06f, 1.23204315e-05f, 3.2743966e-05f, 6.68620547e-05f, 0.000107581877f,
];
private static ReadOnlySpan<float> Blue =>
[
0.807469527f, 0.830635354f, 0.853801904f, 0.876939707f, 0.899889955f, 0.92228676f, 0.943529215f, 0.962830198f,
0.979264412f, 0.991692043f, 0.99862149f, 0.996927628f, 0.979998128f, 0.943507433f, 0.885821269f, 0.808170421f,
0.71401331f, 0.608379884f, 0.497221577f, 0.386797533f, 0.283154961f, 0.191659337f, 0.11654071f, 0.0604503786f,
0.0240129266f, 0.00540685411f, 1.61548711e-07f, 1.06184152e-07f, 2.84808058e-07f, 2.59130901e-07f, 1.04914227e-07f, 0.000451346134f,
0.00774031585f, 0.0245319302f, 0.0405086082f, 0.0500636496f, 0.051301242f, 0.0450624333f, 0.0339302845f, 0.021225906f,
0.0101213068f, 0.00284879946f, 1.00408131e-06f, 2.26836029e-07f, 4.78697571e-07f, 7.77024557e-08f, 2.06003424e-07f, 2.68621395e-07f,
3.21384151e-07f, 4.09280481e-07f, 5.4140573e-07f, 7.26436417e-07f, 1.01373333e-06f, 1.45801888e-06f, 2.19525189e-06f, 3.5375357e-06f,
6.21747073e-06f, 1.57553175e-05f, 3.9726355e-05f, 7.56335619e-05f, 0.000116677997f,
];
private static ReadOnlySpan<float> D65 =>
[
82.7549f, 87.1204f, 91.486f, 92.4589f, 93.4318f, 90.057f, 86.6823f, 95.7736f,
104.865f, 110.936f, 117.008f, 117.41f, 117.812f, 116.336f, 114.861f, 115.392f,
115.923f, 112.367f, 108.811f, 109.082f, 109.354f, 108.578f, 107.802f, 106.296f,
104.79f, 106.239f, 107.689f, 106.047f, 104.405f, 104.225f, 104.046f, 102.023f,
100f, 98.1671f, 96.3342f, 96.0611f, 95.788f, 92.2368f, 88.6856f, 89.3459f,
90.0062f, 89.8026f, 89.5991f, 88.6489f, 87.6987f, 85.4936f, 83.2886f, 83.4939f,
83.6992f, 81.863f, 80.0268f, 80.1207f, 80.2146f, 81.2462f, 82.2778f, 80.281f,
78.2842f, 74.0027f, 69.7213f, 70.6652f, 71.6091f,
];
public static Vector3 At(double wavelength)
{
var (index, fraction) = Locate(wavelength);
return new Vector3(
Lerp(Red, index, fraction),
Lerp(Green, index, fraction),
Lerp(Blue, index, fraction));
}
public static double IlluminantAt(double wavelength)
{
var (index, fraction) = Locate(wavelength);
return Lerp(D65, index, fraction);
}
private static (int Index, float Fraction) Locate(double wavelength)
{
var position = Math.Clamp((wavelength - First) / Interval, 0.0, Count - 1);
var index = Math.Min((int)position, Count - 2);
return (index, (float)(position - index));
}
private static float Lerp(ReadOnlySpan<float> table, int index, float fraction)
=> table[index] + (table[index + 1] - table[index]) * fraction;
}