Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 1986cfb

Browse files
committed
Added BlackBodySpectrum
1 parent e964336 commit 1986cfb

File tree

5 files changed

+160
-4
lines changed

5 files changed

+160
-4
lines changed

ColorSharp/ColorSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="src\Illuminants\CIE_C.cs" />
8686
<Compile Include="src\Illuminants\CIE_D50.cs" />
8787
<Compile Include="src\Illuminants\CIE_D55.cs" />
88+
<Compile Include="src\LightSpectrums\BlackBodySpectrum.cs" />
8889
</ItemGroup>
8990
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
9091
<ItemGroup>

ColorSharp/src/LightSpectrums/ALightSpectrum.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected ALightSpectrum(AConvertibleColor dataSource=null) : base(dataSource) {
7878
* If the method returns -1.0 , then we suppose we have an "analytic" spectrum, so we don't have samples.
7979
* </summary>
8080
*/
81-
public abstract double GetNextAmplitudeSample (double waveLength);
81+
//public abstract double GetNextAmplitudeSample (double waveLength);
8282

8383
#endregion
8484

@@ -98,27 +98,49 @@ public CIEXYZ ToCIEXYZ (SpectrumStrategy strategy=SpectrumStrategy.Default)
9898
{
9999
AMatchingFunction[] MFs;
100100

101+
int step;
101102
if (strategy == SpectrumStrategy.Nm1Deg2) {
103+
step = 1;
102104
MFs = new AMatchingFunction[] {
103105
CIE1931XYZ1Nm2DegX.Instance, CIE1931XYZ1Nm2DegY.Instance, CIE1931XYZ1Nm2DegZ.Instance
104106
};
105107
} else if (strategy == SpectrumStrategy.Nm1Deg10) {
108+
step = 1;
106109
MFs = new AMatchingFunction[] {
107110
CIE1964XYZ1Nm10DegX.Instance, CIE1964XYZ1Nm10DegY.Instance, CIE1964XYZ1Nm10DegZ.Instance
108111
};
109112
} else if (strategy == SpectrumStrategy.Nm5Deg10) {
113+
step = 5;
110114
MFs = new AMatchingFunction[] {
111115
CIE1964XYZ5Nm10DegX.Instance, CIE1964XYZ5Nm10DegY.Instance, CIE1964XYZ5Nm10DegZ.Instance
112116
};
113117
} else { // if (strategy == SpectrumStrategy.Nm5Deg2) {
118+
step = 5;
114119
MFs = new AMatchingFunction[] {
115120
CIE1931XYZ5Nm2DegX.Instance, CIE1931XYZ5Nm2DegY.Instance, CIE1931XYZ5Nm2DegZ.Instance
116121
};
117122
}
118123

119-
return new CIEXYZ (
120-
MFs [0].DoConvolution (this), MFs [1].DoConvolution (this), MFs [2].DoConvolution (this), DataSource ?? this
121-
);
124+
double X = 0.0, Y = 0.0, Z = 0.0;
125+
int n = MFs [0].GetNumberOfDataPoints ();
126+
double minV = MFs [0].GetSupportMinValue ();
127+
128+
for (int i = 0; i < n; i++) {
129+
if (i * step + minV < GetSupportMinValue ()) {
130+
continue;
131+
}
132+
if (i * step + minV > GetSupportMaxValue ()) {
133+
break;
134+
}
135+
136+
double v = EvaluateAt (i * step + minV);
137+
138+
X += MFs [0].EvaluateAt (i * step + minV) * v * step;
139+
Y += MFs [1].EvaluateAt (i * step + minV) * v * step;
140+
Z += MFs [2].EvaluateAt (i * step + minV) * v * step;
141+
}
142+
143+
return new CIEXYZ (X, Y, Z);
122144
}
123145

124146
#endregion
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* The MIT License (MIT)
3+
* Copyright (c) 2014 Andrés Correa Casablanca
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
/*
25+
* Contributors:
26+
* - Andrés Correa Casablanca <castarco@gmail.com , castarco@litipk.com>
27+
*/
28+
29+
30+
using System;
31+
32+
33+
namespace Litipk.ColorSharp
34+
{
35+
namespace LightSpectrums
36+
{
37+
public sealed class BlackBodySpectrum : ALightSpectrum
38+
{
39+
#region properties
40+
41+
public readonly double CCT;
42+
43+
public readonly double MinWaveLength;
44+
public readonly double MaxWaveLength;
45+
46+
#endregion
47+
48+
#region constructors
49+
50+
public BlackBodySpectrum (double cct) : this (cct, 380.0, 780.0)
51+
{
52+
// Nothing to do here
53+
}
54+
55+
public BlackBodySpectrum (double cct, double minWaveLength, double maxWaveLength)
56+
{
57+
if (minWaveLength <= 0.0) {
58+
throw new ArgumentOutOfRangeException ("minWaveLength", "minWaveLength must be greater than 0");
59+
}
60+
61+
if (maxWaveLength <= minWaveLength) {
62+
throw new ArgumentException("maxWaveLength must be greater than minWaveLength", "maxWaveLength");
63+
}
64+
65+
if (cct <= 1) {
66+
throw new ArgumentOutOfRangeException ("cct", "cct must be greater than 1");
67+
}
68+
69+
CCT = cct;
70+
MinWaveLength = minWaveLength;
71+
MaxWaveLength = maxWaveLength;
72+
}
73+
74+
#endregion
75+
76+
#region inherited methods
77+
78+
public override double EvaluateAt (double waveLength)
79+
{
80+
// 2*h*c² = 2 * 299792458 * 1.98644568e-25 = 1.1910428661813628e-16
81+
double a = (
82+
1.1910428661813628e-16 / (
83+
Math.Pow(waveLength, 5) * (
84+
Math.Exp(1.98644568e-25 / (waveLength * CCT * 1.3806488e-23)) - 1.0
85+
)
86+
)
87+
);
88+
89+
double b = (
90+
(3.7417749e-16 * Math.Pow(waveLength, -5.0) / Math.PI) * Math.Pow(
91+
(Math.Exp(1.4388e-2 / (waveLength * CCT)) - 1), -1
92+
)
93+
);
94+
95+
Console.WriteLine (a + " <--> " + b);
96+
97+
return a;
98+
}
99+
100+
public override double GetMaxValueOnSupport ()
101+
{
102+
double result = Double.NegativeInfinity;
103+
104+
for (int i = 0; i + MinWaveLength < MaxWaveLength; i++) {
105+
result = Math.Max (EvaluateAt (MinWaveLength + i), result);
106+
}
107+
108+
return result;
109+
}
110+
111+
public override double GetSupportMinValue ()
112+
{
113+
return MinWaveLength;
114+
}
115+
116+
public override double GetSupportMaxValue ()
117+
{
118+
return MaxWaveLength;
119+
}
120+
121+
public override bool IsInsideColorSpace (bool highPrecision = false)
122+
{
123+
return true;
124+
}
125+
126+
#endregion
127+
}
128+
}
129+
}

ColorSharp/src/LightSpectrums/RegularLightSpectrum.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public override int GetNumberOfDataPoints()
157157
/**
158158
* <inheritdoc />
159159
*/
160+
/*
160161
public override double GetNextAmplitudeSample (double waveLength)
161162
{
162163
if (waveLength < MinWaveLength && waveLength >= MaxWaveLength) {
@@ -165,6 +166,7 @@ public override double GetNextAmplitudeSample (double waveLength)
165166
166167
return MinWaveLength + ((uint)Math.Floor ((waveLength - MinWaveLength) / NmPerStep) + 1) * NmPerStep;
167168
}
169+
*/
168170

169171
#endregion
170172

ColorSharp/src/LightSpectrums/TabularLightSpectrum.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public override double EvaluateAt(double waveLength)
131131
/**
132132
* <inheritdoc />
133133
*/
134+
/*
134135
public override double GetNextAmplitudeSample (double waveLength)
135136
{
136137
if (waveLength < RawAmplitudes[0].Key || waveLength >= RawAmplitudes[RawAmplitudes.Count-1].Key) {
@@ -147,6 +148,7 @@ public override double GetNextAmplitudeSample (double waveLength)
147148
148149
return RawAmplitudes [index + 1].Key;
149150
}
151+
*/
150152

151153
#endregion
152154

0 commit comments

Comments
 (0)