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

Commit 896d529

Browse files
committed
Heavy commit:
* Added 1 nanometer-step CIE's XYZ matching functions * Added test to the spectrum -> CIE's XYZ color space conversion
1 parent 87eb81c commit 896d529

File tree

10 files changed

+1560
-18
lines changed

10 files changed

+1560
-18
lines changed

ColorSharp/ColorSharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<Compile Include="src\InternalUtils\Structs.cs" />
6868
<Compile Include="src\ColorSpaces\ColorConversionStrategy.cs" />
6969
<Compile Include="src\Illuminants\D65.cs" />
70+
<Compile Include="src\MatchingFunctions\CIE1931XYZ1NmMatchingFunctionX.cs" />
71+
<Compile Include="src\MatchingFunctions\CIE1931XYZ1NmMatchingFunctionY.cs" />
72+
<Compile Include="src\MatchingFunctions\CIE1931XYZ51NmMatchingFunctionZ.cs" />
7073
</ItemGroup>
7174
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
7275
<ItemGroup>

ColorSharp/src/ColorSpaces/CIExyY.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ public sealed class CIExyY : AConvertibleColor
4747

4848
#region static properties
4949
// Light wavelengths coordinates in the CIE's 1931 xyY color sapce.
50-
static xyYPoint[] Sharkfin;
50+
static xyYPoint[] Sharkfin1Nm;
51+
static xyYPoint[] Sharkfin5Nm;
5152

5253
// Sharkfin transformation with performance purposes.
53-
static xyYPoint[] SortedSharkfin;
54+
static xyYPoint[] SortedSharkfin1Nm;
55+
static xyYPoint[] SortedSharkfin5Nm;
5456
#endregion
5557

5658
#region readonly properties
@@ -80,10 +82,28 @@ public sealed class CIExyY : AConvertibleColor
8082
*/
8183
static CIExyY()
8284
{
83-
var mfX = CIE1931XYZ5NmMatchingFunctionX.Instance.Amplitudes;
84-
var mfY = CIE1931XYZ5NmMatchingFunctionY.Instance.Amplitudes;
85-
var mfZ = CIE1931XYZ5NmMatchingFunctionZ.Instance.Amplitudes;
85+
computeSharkfin (
86+
CIE1931XYZ5NmMatchingFunctionX.Instance.Amplitudes,
87+
CIE1931XYZ5NmMatchingFunctionY.Instance.Amplitudes,
88+
CIE1931XYZ5NmMatchingFunctionZ.Instance.Amplitudes,
89+
ref Sharkfin5Nm,
90+
ref SortedSharkfin5Nm
91+
);
92+
93+
computeSharkfin (
94+
CIE1931XYZ1NmMatchingFunctionX.Instance.Amplitudes,
95+
CIE1931XYZ1NmMatchingFunctionY.Instance.Amplitudes,
96+
CIE1931XYZ1NmMatchingFunctionZ.Instance.Amplitudes,
97+
ref Sharkfin1Nm,
98+
ref SortedSharkfin1Nm
99+
);
100+
}
86101

102+
/**
103+
* Computes the 'sharkin' points.
104+
*/
105+
static void computeSharkfin(double[] mfX, double[] mfY, double[] mfZ, ref xyYPoint[] Sharkfin, ref xyYPoint[] SortedSharkfin)
106+
{
87107
var n = mfX.Length;
88108

89109
Sharkfin = new xyYPoint[n];
@@ -126,9 +146,9 @@ public override bool IsInsideColorSpace()
126146
if (y > 1.0 - x || y < (x - 0.25) * 0.5 || y < 0.4 - x * 4 || y >= 0.85)
127147
return false;
128148

129-
xyYPoint[] points = new xyYPoint[SortedSharkfin.Length + 1];
130-
Array.Copy (SortedSharkfin, 0, points, 0, SortedSharkfin.Length);
131-
points[SortedSharkfin.Length] = new xyYPoint{x=x, y=y};
149+
xyYPoint[] points = new xyYPoint[SortedSharkfin5Nm.Length + 1];
150+
Array.Copy (SortedSharkfin5Nm, 0, points, 0, SortedSharkfin5Nm.Length);
151+
points[SortedSharkfin5Nm.Length] = new xyYPoint{x=x, y=y};
132152

133153
xyYPoint[] convexHull = findConvexHull (points);
134154

@@ -159,7 +179,7 @@ public override CIExyY ToCIExyY (ConversionStrategy strategy = ConversionStrateg
159179
/**
160180
* <inheritdoc />
161181
*/
162-
public override SRGB ToSRGB (ConversionStrategy strategy = ConversionStrategy.Default)
182+
public override SRGB ToSRGB (ConversionStrategy strategy = ConversionStrategy.ForceLowTruncate|ConversionStrategy.ForceHighStretch)
163183
{
164184
return ToCIEXYZ ().ToSRGB (strategy);
165185
}

ColorSharp/src/ColorSpaces/ColorConversionStrategy.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ namespace ColorSpaces
4040
[Flags]
4141
public enum ConversionStrategy
4242
{
43-
4443
WaveLength5NmStep = 0,
4544
WaveLength1NmStep = 1,
4645

ColorSharp/src/LightSpectrums/ALightSpectrum.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,21 @@ protected ALightSpectrum(AConvertibleColor dataSource=null) : base(dataSource) {
9898
*/
9999
public override CIEXYZ ToCIEXYZ (ConversionStrategy strategy=ConversionStrategy.Default)
100100
{
101-
// TODO : Check ConversionStrategy
102-
103-
AMatchingFunction[] MFs = {
104-
CIE1931XYZ5NmMatchingFunctionX.Instance,
105-
CIE1931XYZ5NmMatchingFunctionY.Instance,
106-
CIE1931XYZ5NmMatchingFunctionZ.Instance
107-
};
101+
AMatchingFunction[] MFs;
102+
103+
if ((strategy & ConversionStrategy.WaveLength1NmStep) != 0) {
104+
MFs = new AMatchingFunction[] {
105+
CIE1931XYZ1NmMatchingFunctionX.Instance,
106+
CIE1931XYZ1NmMatchingFunctionY.Instance,
107+
CIE1931XYZ1NmMatchingFunctionZ.Instance
108+
};
109+
} else { // By default we choose 5 Nm
110+
MFs = new AMatchingFunction[] {
111+
CIE1931XYZ5NmMatchingFunctionX.Instance,
112+
CIE1931XYZ5NmMatchingFunctionY.Instance,
113+
CIE1931XYZ5NmMatchingFunctionZ.Instance
114+
};
115+
}
108116

109117
return new CIEXYZ (
110118
MFs [0].DoConvolution (this), MFs [1].DoConvolution (this), MFs [2].DoConvolution (this), DataSource ?? this
@@ -122,7 +130,7 @@ public override CIExyY ToCIExyY (ConversionStrategy strategy = ConversionStrateg
122130
/**
123131
* <inheritdoc />
124132
*/
125-
public override SRGB ToSRGB(ConversionStrategy strategy = ConversionStrategy.Default)
133+
public override SRGB ToSRGB(ConversionStrategy strategy = ConversionStrategy.ForceLowTruncate|ConversionStrategy.ForceHighStretch)
126134
{
127135
return ToCIEXYZ ().ToSRGB (strategy);
128136
}

0 commit comments

Comments
 (0)