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

Commit 514373e

Browse files
committed
Added CCT computation
1 parent 7115650 commit 514373e

File tree

6 files changed

+114
-5
lines changed

6 files changed

+114
-5
lines changed

ColorSharp/src/ColorSpaces/AConvertibleColor.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929

3030
using System;
31-
using System.Collections.Generic;
31+
32+
using Litipk.ColorSharp.LightSpectrums;
3233

3334

3435
namespace Litipk.ColorSharp
@@ -124,6 +125,18 @@ AConvertibleColor InnerConvertTo (Type t, ColorStrategy strategy = ColorStrategy
124125
*/
125126
public abstract bool IsInsideColorSpace (bool highPrecision = false);
126127

128+
/**
129+
* <summary>Gives us the Correlater Color Temperature associated to the spectrum or color.</summary>
130+
*/
131+
public virtual double GetCCT ()
132+
{
133+
if (DataSource is BlackBodySpectrum) {
134+
return DataSource.GetCCT ();
135+
}
136+
137+
return ToCIEUCS ().GetCCT ();
138+
}
139+
127140
/**
128141
* <summary>Converts the color sample to a CIE's 1931 XYZ color sample.</summary>
129142
*/

ColorSharp/src/ColorSpaces/CIEUCS.cs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929

3030
using System;
31+
using System.Collections.Generic;
32+
33+
using Litipk.ColorSharp.LightSpectrums;
3134

3235

3336
namespace Litipk.ColorSharp
@@ -74,6 +77,8 @@ public sealed class CIEUCS : AConvertibleColor
7477
*/
7578
public double Y { get { return V; } }
7679

80+
static List<CIEUCS> TemperatureChromaticities = null;
81+
7782
#endregion
7883

7984

@@ -146,10 +151,10 @@ public CIEUCS (double U, double V, double u, double v, double W, AConvertibleCol
146151
throw new ArgumentException ("Invalid color point");
147152
}
148153

149-
if (Math.Abs (U / (U + V + W) - u) > 8*double.Epsilon) {
154+
if (Math.Abs (U / (U + V + W) - u) > 1e-15) {
150155
throw new ArgumentException ("Inconsistent data");
151156
}
152-
if (Math.Abs (V / (U + V + W) - v) > 8*double.Epsilon) {
157+
if (Math.Abs (V / (U + V + W) - v) > 1e-15) {
153158
throw new ArgumentException ("Inconsistent data");
154159
}
155160

@@ -177,6 +182,66 @@ public override bool IsInsideColorSpace(bool highPrecision = false)
177182
return ToCIExyY ().IsInsideColorSpace (highPrecision);
178183
}
179184

185+
public override double GetCCT ()
186+
{
187+
if (DataSource is BlackBodySpectrum) {
188+
return (DataSource as BlackBodySpectrum).CCT;
189+
}
190+
191+
if (TemperatureChromaticities == null) {
192+
// Oversized to improve alignment (needs 302).
193+
TemperatureChromaticities = new List<CIEUCS> (512);
194+
195+
// From 1000º K to 20000º K
196+
for (double t = 1000.0; t < 20001.0; t *= 1.01) {
197+
TemperatureChromaticities.Add (
198+
new BlackBodySpectrum (t).ToCIEXYZ (SpectrumStrategy.Nm1Deg2).ToCIEUCS ()
199+
);
200+
}
201+
}
202+
203+
int bestI = 0;
204+
double minDuv = double.PositiveInfinity;
205+
206+
// First gross grained search
207+
// TODO: This is a naive search, must be improved!
208+
for (int i = 0; i < TemperatureChromaticities.Count; i++) {
209+
double tmpDuv = Math.Sqrt (
210+
Math.Pow (u - TemperatureChromaticities [i].u, 2) +
211+
Math.Pow (v - TemperatureChromaticities [i].v, 2)
212+
);
213+
214+
if (minDuv > tmpDuv) {
215+
minDuv = tmpDuv;
216+
bestI = i;
217+
}
218+
}
219+
double bestTmp = TemperatureChromaticities [bestI].GetCCT ();
220+
221+
// Preparing the following fine grained search
222+
double tMin = TemperatureChromaticities [
223+
(bestI > 0) ? bestI - 1 : bestI
224+
].GetCCT ();
225+
double tMax = TemperatureChromaticities [
226+
(bestI < TemperatureChromaticities.Count - 1) ? bestI + 1 : bestI
227+
].GetCCT ();
228+
double tDiff = (tMax - tMin) / 100.0;
229+
230+
// Second fine grained search
231+
for (double t = tMin; t < tMax; t += tDiff) {
232+
var tmpUV = new BlackBodySpectrum (t).ToCIEXYZ (SpectrumStrategy.Nm1Deg2).ToCIEUCS ();
233+
234+
double tmpDuv = Math.Sqrt (Math.Pow (u - tmpUV.u, 2) + Math.Pow (v - tmpUV.v, 2));
235+
236+
if (minDuv > tmpDuv) {
237+
minDuv = tmpDuv;
238+
bestTmp = t;
239+
}
240+
}
241+
242+
return bestTmp;
243+
}
244+
180245
/**
181246
* <inheritdoc />
182247
*/

ColorSharp/src/ColorSpaces/CIExyY.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ public override CIExyY ToCIExyY (ColorStrategy strategy = ColorStrategy.Default)
190190
return this;
191191
}
192192

193+
/**
194+
* <inheritdoc />
195+
*/
196+
public override CIEUCS ToCIEUCS (ColorStrategy strategy = ColorStrategy.Default)
197+
{
198+
if (DataSource is CIEUCS) {
199+
return DataSource as CIEUCS;
200+
}
201+
202+
// Warning, it's not a good idea to do it with other types, we do with CIEXYZ bacause
203+
// we have a more precise conversion and because a call-loop it's not possible.
204+
if (DataSource is CIEXYZ) {
205+
return (DataSource as CIEXYZ).ToCIEUCS ();
206+
}
207+
208+
double div = (12 * y - 2 * x + 3);
209+
double yy = Y / y;
210+
211+
return new CIEUCS (
212+
DataSource ?? this,
213+
4 * x / div, 6 * y / div, 0.5 * (-x * yy + 3 * Y + yy * (1.0 - x - y))
214+
);
215+
}
216+
193217
#endregion
194218

195219

ColorSharp/src/LightSpectrums/ALightSpectrum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public CIEXYZ ToCIEXYZ (SpectrumStrategy strategy=SpectrumStrategy.Default)
132132
Z += MFs [2].EvaluateAt (i * step + minV) * v * step;
133133
}
134134

135-
return new CIEXYZ (X, Y, Z);
135+
return new CIEXYZ (X, Y, Z, DataSource ?? this);
136136
}
137137

138138
#endregion

ColorSharp/src/LightSpectrums/BlackBodySpectrum.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ public override bool IsInsideColorSpace (bool highPrecision = false)
113113
return true;
114114
}
115115

116+
/**
117+
* <inheritdoc />
118+
*/
119+
public override double GetCCT ()
120+
{
121+
return CCT;
122+
}
123+
116124
#endregion
117125
}
118126
}

ColorSharp/src/LightSpectrums/RegularLightSpectrum.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
using System.Collections.ObjectModel;
3434

3535
using Litipk.ColorSharp.ColorSpaces;
36-
using Litipk.ColorSharp.InternalUtils;
3736

3837

3938
namespace Litipk.ColorSharp

0 commit comments

Comments
 (0)