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

Commit 5047cfc

Browse files
committed
Minor bugfixes, improved documentation
1 parent 953884d commit 5047cfc

20 files changed

+175
-79
lines changed

ColorSharp/src/ColorSpaces/ColorStrategy.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,45 @@ namespace ColorSpaces
4040
[Flags]
4141
public enum ColorStrategy
4242
{
43+
/// <summary>If the color can't be represented in the new color space, an exception will be thrown.</summary>
4344
NoForce = 0,
45+
46+
/// <summary>
47+
/// "Truncates" component values in the new color space if they're lower than the new color space's lower boundaries.
48+
/// </summary>
4449
ForceLowTruncate = 2,
50+
51+
/// <summary>
52+
/// "Stretches" component values in the new color space if they're lower than the new color space's lower boundaries.
53+
/// </summary>
4554
ForceLowStretch = 4,
55+
56+
/// <summary>
57+
/// "Truncates" component values in the new color space if they're lower than the new color space's upper boundaries.
58+
/// </summary>
4659
ForceHighTruncate = 8,
60+
61+
/// <summary>
62+
/// "Stretches" component values in the new color space if they're lower than the new color space's upper boundaries.
63+
/// </summary>
4764
ForceHighStretch = 16,
65+
66+
/// <summary>Combination of ForceLowStretch and ForceLowTruncate</summary>
4867
ForceLow = ForceLowStretch | ForceLowTruncate,
68+
69+
/// <summary>Combination of ForceHighStretch and ForceHighTruncate</summary>
4970
ForceHigh = ForceHighStretch | ForceHighTruncate,
71+
72+
/// <summary>Combination of ForceLowTruncate and ForceHighTruncate</summary>
5073
ForceTruncate = ForceLowTruncate | ForceHighTruncate,
74+
75+
/// <summary>Combination of ForceLowStretch and ForceHighStretch</summary>
5176
ForceStretch = ForceLowStretch | ForceHighStretch,
77+
78+
/// <summary>Combination of ForceLow and ForceHigh</summary>
5279
Force = ForceLow | ForceHigh,
5380

81+
/// <summary>NoForce</summary>
5482
Default = NoForce
5583
}
5684
}

ColorSharp/src/LightSpectrums/AInterpolatedLightSpectrum.cs

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

2929

3030
using Litipk.ColorSharp.ColorSpaces;
31-
using Litipk.ColorSharp.MatchingFunctions;
3231
using Litipk.ColorSharp.InternalUtils;
3332

3433

@@ -41,10 +40,16 @@ namespace LightSpectrums
4140
*/
4241
public abstract class AInterpolatedLightSpectrum : ALightSpectrum, IRealInterpolatedFunctionWithFiniteSupport
4342
{
43+
/**
44+
* <inheritdoc />
45+
*/
4446
protected AInterpolatedLightSpectrum(AConvertibleColor dataSource=null) : base(dataSource) { }
4547

48+
/**
49+
* <summary>Gives us the number of data points used to construct the spectrum object.</summary>
50+
*/
4651
public abstract int GetNumberOfDataPoints();
4752
}
4853
}
4954
}
50-
55+

ColorSharp/src/LightSpectrums/ALightSpectrum.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ protected ALightSpectrum(AConvertibleColor dataSource=null) : base(dataSource) {
4848

4949
#region abstract methods
5050

51-
#region IRealFunctionWithFiniteSupport methods
52-
5351
/**
5452
* <inheritdoc />
5553
*/
@@ -72,16 +70,6 @@ protected ALightSpectrum(AConvertibleColor dataSource=null) : base(dataSource) {
7270

7371
#endregion
7472

75-
/**
76-
* <summary>
77-
* Supposing the light spectrum we have is a discrete sample, this gives us the next data point.
78-
* If the method returns -1.0 , then we suppose we have an "analytic" spectrum, so we don't have samples.
79-
* </summary>
80-
*/
81-
//public abstract double GetNextAmplitudeSample (double waveLength);
82-
83-
#endregion
84-
8573

8674
#region AConvertibleColor methods
8775

@@ -94,6 +82,10 @@ public override CIEXYZ ToCIEXYZ (ColorStrategy strategy=ColorStrategy.Default)
9482
return ToCIEXYZ (SpectrumStrategy.Default);
9583
}
9684

85+
/**
86+
* <see cref="ToCIEXYZ(ColorStrategy)"/>
87+
* <param name="strategy">Strategy used to obtain the tristimulous values</param>
88+
*/
9789
public CIEXYZ ToCIEXYZ (SpectrumStrategy strategy=SpectrumStrategy.Default)
9890
{
9991
AMatchingFunction[] MFs;

ColorSharp/src/LightSpectrums/BlackBodySpectrum.cs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,76 +34,80 @@ namespace Litipk.ColorSharp
3434
{
3535
namespace LightSpectrums
3636
{
37+
/**
38+
* <summary>Class to represent black body spectrums at a given temperature.</summary>
39+
*/
3740
public sealed class BlackBodySpectrum : ALightSpectrum
3841
{
3942
#region properties
4043

44+
/**
45+
*
46+
*/
4147
public readonly double CCT;
4248

43-
public readonly double MinWaveLength;
44-
public readonly double MaxWaveLength;
45-
4649
#endregion
4750

4851
#region constructors
4952

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)
53+
/**
54+
* <param name="cct">Black body's temperature (in Kelvin degrees).</param>
55+
*/
56+
public BlackBodySpectrum (double cct)
5657
{
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-
6558
if (cct <= 1) {
6659
throw new ArgumentOutOfRangeException ("cct", "cct must be greater than 1");
6760
}
6861

6962
CCT = cct;
70-
MinWaveLength = minWaveLength;
71-
MaxWaveLength = maxWaveLength;
7263
}
7364

7465
#endregion
7566

7667
#region inherited methods
7768

69+
/**
70+
* <inheritdoc />
71+
*/
7872
public override double EvaluateAt (double waveLength)
7973
{
80-
// 2*h*c² = 2 * 299792458 * 1.98644568e-25 = 1.1910428661813628e-16
81-
return (1.1910428661813628e19 /*1.1910428661813628e-16*/ / (
82-
Math.Pow(waveLength/* * 1e-7*/, 5.0) * (Math.Exp(0.014387769576158687 / (waveLength * CCT)) - 1.0)
74+
// Conversion from nanometers to meters
75+
waveLength *= 1e-9;
76+
77+
// Applying Plank's Law : https://en.wikipedia.org/wiki/Planck's_law
78+
return (1.1910428661813628e-16 * Math.Pow(waveLength, -5.0) / (
79+
Math.Exp(0.014387769576158687 / (waveLength * CCT)) - 1.0
8380
));
8481
}
8582

83+
/**
84+
* <inheritdoc />
85+
*/
8686
public override double GetMaxValueOnSupport ()
8787
{
88-
double result = Double.NegativeInfinity;
89-
90-
for (int i = 0; i + MinWaveLength < MaxWaveLength; i++) {
91-
result = Math.Max (EvaluateAt (MinWaveLength + i), result);
92-
}
93-
94-
return result;
88+
// https://en.wikipedia.org/wiki/Wien's_displacement_law
89+
return EvaluateAt (2.8977721e6/CCT);
9590
}
9691

92+
/**
93+
* <inheritdoc />
94+
*/
9795
public override double GetSupportMinValue ()
9896
{
99-
return MinWaveLength;
97+
return 1e-6;
10098
}
10199

100+
/**
101+
* <inheritdoc />
102+
*/
102103
public override double GetSupportMaxValue ()
103104
{
104-
return MaxWaveLength;
105+
return double.PositiveInfinity;
105106
}
106107

108+
/**
109+
* <inheritdoc />
110+
*/
107111
public override bool IsInsideColorSpace (bool highPrecision = false)
108112
{
109113
return true;

ColorSharp/src/LightSpectrums/RegularLightSpectrum.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,38 @@ public sealed class RegularLightSpectrum : AInterpolatedLightSpectrum
4747
{
4848
#region properties
4949

50-
// Needed values to interpret the data points
50+
/**
51+
* <value>Nanometers between each data point.</value>
52+
*/
5153
public readonly double NmPerStep;
54+
55+
/**
56+
* <value>Minimum wavelength for which we have information on this spectrum.</value>
57+
*/
5258
public readonly double MinWaveLength;
59+
60+
/**
61+
* <value>Maximum wavelength for which we have information on this spectrum.</value>
62+
*/
5363
public readonly double MaxWaveLength;
5464

55-
// Data points
65+
/**
66+
* <value>Equidistant data points (usually relative values)</value>
67+
*/
5668
public readonly ReadOnlyCollection<double> Amplitudes;
5769
#endregion
5870

5971

6072
#region constructors
6173

62-
// Constructor
74+
/**
75+
* <param name="minWaveLength">Associated wavelength to the first value of 'amplitudes'.</param>
76+
* <param name="maxWaveLength">Associated wavelength to the last value of 'amplitudes'.</param>
77+
* <param name="amplitudes">Equidistant data points.</param>
78+
* <param name="dataSource">
79+
* Reference to a AConvertibleColor instance from which this object has been generated.
80+
* </param>
81+
*/
6382
public RegularLightSpectrum (double minWaveLength, double maxWaveLength, IList<double> amplitudes, AConvertibleColor dataSource=null) : base(dataSource)
6483
{
6584
MinWaveLength = minWaveLength;
@@ -68,7 +87,14 @@ public RegularLightSpectrum (double minWaveLength, double maxWaveLength, IList<d
6887
Amplitudes = new ReadOnlyCollection<double> (amplitudes);
6988
}
7089

71-
// Constructor
90+
/**
91+
* <param name="minWaveLength">Associated wavelength to the first value of 'amplitudes'.</param>
92+
* <param name="amplitudes">Equidistant data points.</param>
93+
* <param name="nmPerStep">Nanometers between each data point in 'amplitudes'.</param>
94+
* <param name="dataSource">
95+
* Reference to a AConvertibleColor instance from which this object has been generated.
96+
* </param>
97+
*/
7298
public RegularLightSpectrum (double minWaveLength, IList<double> amplitudes, double nmPerStep, AConvertibleColor dataSource=null) : base(dataSource)
7399
{
74100
NmPerStep = nmPerStep;
@@ -77,12 +103,19 @@ public RegularLightSpectrum (double minWaveLength, IList<double> amplitudes, dou
77103
Amplitudes = new ReadOnlyCollection<double> (amplitudes);
78104
}
79105

80-
// Constructor
81-
public RegularLightSpectrum (IList<double> amplitudes, double nmPerStep, double maxWavelength, AConvertibleColor dataSource=null) : base(dataSource)
106+
/**
107+
* <param name="amplitudes">Equidistant data points.</param>
108+
* <param name="nmPerStep">Nanometers between each data point in 'amplitudes'.</param>
109+
* <param name="maxWaveLength">Associated wavelength to the last value of 'amplitudes'.</param>
110+
* <param name="dataSource">
111+
* Reference to a AConvertibleColor instance from which this object has been generated.
112+
* </param>
113+
*/
114+
public RegularLightSpectrum (IList<double> amplitudes, double nmPerStep, double maxWaveLength, AConvertibleColor dataSource=null) : base(dataSource)
82115
{
83116
NmPerStep = nmPerStep;
84-
MaxWaveLength = maxWavelength;
85-
MinWaveLength = maxWavelength - nmPerStep * (amplitudes.Count - 1);
117+
MaxWaveLength = maxWaveLength;
118+
MinWaveLength = maxWaveLength - nmPerStep * (amplitudes.Count - 1);
86119
Amplitudes = new ReadOnlyCollection<double> (amplitudes);
87120
}
88121

ColorSharp/src/LightSpectrums/SpectrumStrategy.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,32 @@ namespace LightSpectrums
4040
[Flags]
4141
public enum SpectrumStrategy
4242
{
43+
/// <summary>Use the 1nm step matching functions to obtain the chromaticity components of the spectrum.</summary>
4344
WaveLength1NmStep = 0,
45+
46+
/// <summary>Use the 5nm step matching functions to obtain the chromaticity components of the spectrum.</summary>
4447
WaveLength5NmStep = 1,
4548

49+
/// <summary>Use the 2º matching functions to obtain the chromaticity components of the spectrum.</summary>
4650
TwoDegs = 2,
51+
52+
/// <summary>Use the 10º matching functions to obtain the chromaticity components of the spectrum.</summary>
4753
TenDegs = 4,
4854

55+
/// <summary>Combination of WaveLength1NmStep and TwoDegs</summary>
4956
Nm1Deg2 = WaveLength1NmStep | TwoDegs,
57+
58+
/// <summary>Combination of WaveLength5NmStep and TwoDegs</summary>
5059
Nm5Deg2 = WaveLength5NmStep | TwoDegs,
60+
61+
/// <summary>Combination of WaveLength1NmStep and TenDegs</summary>
5162
Nm1Deg10 = WaveLength1NmStep | TenDegs,
63+
64+
/// <summary>Combination of WaveLength5NmStep and TenDegs</summary>
5265
Nm5Deg10 = WaveLength5NmStep | TenDegs,
5366

54-
Default = WaveLength5NmStep | TwoDegs
67+
/// <summary>Nm5Deg2</summary>
68+
Default = Nm5Deg2
5569
}
5670
}
5771
}

ColorSharp/src/LightSpectrums/TabularLightSpectrum.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,6 @@ public override double EvaluateAt(double waveLength)
128128
return (1.0-alpha)*RawAmplitudes [index].Value + alpha*RawAmplitudes [index+1].Value;
129129
}
130130

131-
/**
132-
* <inheritdoc />
133-
*/
134-
/*
135-
public override double GetNextAmplitudeSample (double waveLength)
136-
{
137-
if (waveLength < RawAmplitudes[0].Key || waveLength >= RawAmplitudes[RawAmplitudes.Count-1].Key) {
138-
// TODO: Add extrapolation?
139-
throw new ArgumentOutOfRangeException();
140-
}
141-
142-
int index = RawAmplitudes.BinarySearch(
143-
new KeyValuePair<double, double>(waveLength, 0), new KeyValuePairComparer()
144-
);
145-
146-
if (index < 0)
147-
index = ~index;
148-
149-
return RawAmplitudes [index + 1].Key;
150-
}
151-
*/
152-
153131
#endregion
154132

155133

ColorSharp/src/MatchingFunctions/CIE1931XYZ1Nm2DegX.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ namespace MatchingFunctions
3636
*/
3737
sealed public class CIE1931XYZ1Nm2DegX : RegularMatchingFunction
3838
{
39+
/**
40+
* <summary>Component X of CIE's 1931 2º matching functions (1nm of precision)</summary>
41+
*/
3942
public static readonly CIE1931XYZ1Nm2DegX Instance = new CIE1931XYZ1Nm2DegX (
4043
360.0, new [] {
4144
0.000129900000,

ColorSharp/src/MatchingFunctions/CIE1931XYZ1Nm2DegY.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ namespace MatchingFunctions
3636
*/
3737
sealed public class CIE1931XYZ1Nm2DegY : RegularMatchingFunction
3838
{
39+
/**
40+
* <summary>Component Y of CIE's 1931 2º matching functions (1nm of precision)</summary>
41+
*/
3942
public static readonly CIE1931XYZ1Nm2DegY Instance = new CIE1931XYZ1Nm2DegY (
4043
360.0, new [] {
4144
0.000003917000,

ColorSharp/src/MatchingFunctions/CIE1931XYZ1Nm2DegZ.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ namespace MatchingFunctions
3636
*/
3737
sealed public class CIE1931XYZ1Nm2DegZ : RegularMatchingFunction
3838
{
39+
/**
40+
* <summary>Component Z of CIE's 1931 2º matching functions (1nm of precision)</summary>
41+
*/
3942
public static readonly CIE1931XYZ1Nm2DegZ Instance = new CIE1931XYZ1Nm2DegZ (
4043
360.0, new [] {
4144
0.000606100000,

0 commit comments

Comments
 (0)