Skip to content

Commit cb18952

Browse files
iamcarbonJimBobSquarePants
authored andcommitted
Nits - Colorspaces (#880)
* Add missing periods * Only perform matrix inversion once in CieXyzToLinearRgbConverter * Eliminate unnessary ToVector3 call * Seal GammaWorkingSpace * Address stylecop violation * Add periods * Fix colorspace companding
1 parent 48bac7b commit cb18952

14 files changed

Lines changed: 53 additions & 49 deletions

src/ImageSharp/ColorSpaces/CieXyChromaticityCoordinates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace SixLabors.ImageSharp.ColorSpaces
99
{
1010
/// <summary>
11-
/// Represents the coordinates of CIEXY chromaticity space
11+
/// Represents the coordinates of CIEXY chromaticity space.
1212
/// </summary>
1313
public readonly struct CieXyChromaticityCoordinates : IEquatable<CieXyChromaticityCoordinates>
1414
{

src/ImageSharp/ColorSpaces/Companding/GammaCompanding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace SixLabors.ImageSharp.ColorSpaces.Companding
88
{
99
/// <summary>
10-
/// Implements gamma companding
10+
/// Implements gamma companding.
1111
/// </summary>
1212
/// <remarks>
1313
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>

src/ImageSharp/ColorSpaces/Companding/LCompanding.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace SixLabors.ImageSharp.ColorSpaces.Companding
99
{
1010
/// <summary>
11-
/// Implements L* companding
11+
/// Implements L* companding.
1212
/// </summary>
1313
/// <remarks>
1414
/// For more info see:
@@ -24,7 +24,7 @@ public static class LCompanding
2424
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
2525
[MethodImpl(InliningOptions.ShortMethod)]
2626
public static float Expand(float channel)
27-
=> channel <= 0.08 ? 100 * channel / CieConstants.Kappa : ImageMaths.Pow3((channel + 0.16F) / 1.16F);
27+
=> channel <= 0.08F ? (100F * channel) / CieConstants.Kappa : ImageMaths.Pow3((channel + 0.16F) / 1.16F);
2828

2929
/// <summary>
3030
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
@@ -33,6 +33,6 @@ public static float Expand(float channel)
3333
/// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
3434
[MethodImpl(InliningOptions.ShortMethod)]
3535
public static float Compress(float channel)
36-
=> channel <= CieConstants.Epsilon ? channel * CieConstants.Kappa / 100F : MathF.Pow(1.16F * channel, 0.3333333F) - 0.16F;
36+
=> channel <= CieConstants.Epsilon ? (channel * CieConstants.Kappa) / 100F : (1.16F * MathF.Pow(channel, 0.3333333F)) - 0.16F;
3737
}
3838
}

src/ImageSharp/ColorSpaces/Companding/Rec2020Companding.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,27 @@
77
namespace SixLabors.ImageSharp.ColorSpaces.Companding
88
{
99
/// <summary>
10-
/// Implements Rec. 2020 companding function (for 12-bits).
10+
/// Implements Rec. 2020 companding function.
1111
/// </summary>
1212
/// <remarks>
1313
/// <see href="http://en.wikipedia.org/wiki/Rec._2020"/>
14-
/// For 10-bits, companding is identical to <see cref="Rec709Companding"/>
1514
/// </remarks>
1615
public static class Rec2020Companding
1716
{
17+
private const float Alpha = 1.09929682680944F;
18+
private const float AlphaMinusOne = Alpha - 1F;
19+
private const float Beta = 0.018053968510807F;
20+
private const float InverseBeta = Beta * 4.5F;
21+
private const float Epsilon = 1 / 0.45F;
22+
1823
/// <summary>
1924
/// Expands a companded channel to its linear equivalent with respect to the energy.
2025
/// </summary>
2126
/// <param name="channel">The channel value.</param>
2227
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
2328
[MethodImpl(InliningOptions.ShortMethod)]
2429
public static float Expand(float channel)
25-
=> channel < 0.08145F ? channel / 4.5F : MathF.Pow((channel + 0.0993F) / 1.0993F, 2.222222F);
30+
=> channel < InverseBeta ? channel / 4.5F : MathF.Pow((channel + AlphaMinusOne) / Alpha, Epsilon);
2631

2732
/// <summary>
2833
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
@@ -31,6 +36,6 @@ public static float Expand(float channel)
3136
/// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
3237
[MethodImpl(InliningOptions.ShortMethod)]
3338
public static float Compress(float channel)
34-
=> channel < 0.0181F ? 4500F * channel : (1.0993F * channel) - 0.0993F;
39+
=> channel < Beta ? 4.5F * channel : (Alpha * MathF.Pow(channel, 0.45F)) - AlphaMinusOne;
3540
}
3641
}

src/ImageSharp/ColorSpaces/Companding/Rec709Companding.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ namespace SixLabors.ImageSharp.ColorSpaces.Companding
1414
/// </remarks>
1515
public static class Rec709Companding
1616
{
17+
private const float Epsilon = 1 / 0.45F;
18+
1719
/// <summary>
1820
/// Expands a companded channel to its linear equivalent with respect to the energy.
1921
/// </summary>
2022
/// <param name="channel">The channel value.</param>
2123
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
2224
[MethodImpl(InliningOptions.ShortMethod)]
2325
public static float Expand(float channel)
24-
=> channel < 0.081F ? channel / 4.5F : MathF.Pow((channel + 0.099F) / 1.099F, 2.222222F);
26+
=> channel < 0.081F ? channel / 4.5F : MathF.Pow((channel + 0.099F) / 1.099F, Epsilon);
2527

2628
/// <summary>
2729
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
@@ -30,6 +32,6 @@ public static float Expand(float channel)
3032
/// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
3133
[MethodImpl(InliningOptions.ShortMethod)]
3234
public static float Compress(float channel)
33-
=> channel < 0.018F ? 4500F * channel : (1.099F * channel) - 0.099F;
35+
=> channel < 0.018F ? 4.5F * channel : (1.099F * MathF.Pow(channel, 0.45F)) - 0.099F;
3436
}
3537
}

src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLch.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public partial class ColorSpaceConverter
2626
/// <returns>The <see cref="CieLch"/></returns>
2727
public CieLch ToCieLch(in CieLab color)
2828
{
29-
// Adaptation
3029
CieLab adapted = this.Adapt(color);
3130

32-
// Conversion
3331
return CieLabToCieLchConverter.Convert(adapted);
3432
}
3533

src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/CieXyzAndCieXyyConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
88
{
99
/// <summary>
10-
/// Color converter between CIE XYZ and CIE xyY
10+
/// Color converter between CIE XYZ and CIE xyY.
1111
/// <see href="http://www.brucelindbloom.com/"/> for formulas.
1212
/// </summary>
1313
internal sealed class CieXyzAndCieXyyConverter

src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/CieXyzToLinearRgbConverter.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,28 @@ public CieXyzToLinearRgbConverter()
2828
public CieXyzToLinearRgbConverter(RgbWorkingSpaceBase workingSpace)
2929
{
3030
this.TargetWorkingSpace = workingSpace;
31-
this.conversionMatrix = GetRgbToCieXyzMatrix(workingSpace);
31+
32+
// Gets the inverted Rgb -> Xyz matrix
33+
Matrix4x4.Invert(GetRgbToCieXyzMatrix(workingSpace), out Matrix4x4 inverted);
34+
35+
this.conversionMatrix = inverted;
3236
}
3337

3438
/// <summary>
35-
/// Gets the target working space
39+
/// Gets the target working space.
3640
/// </summary>
3741
public RgbWorkingSpaceBase TargetWorkingSpace { get; }
3842

3943
/// <summary>
4044
/// Performs the conversion from the <see cref="CieXyz"/> input to an instance of <see cref="LinearRgb"/> type.
4145
/// </summary>
4246
/// <param name="input">The input color instance.</param>
43-
/// <returns>The converted result</returns>
47+
/// <returns>The converted result.</returns>
4448
[MethodImpl(InliningOptions.ShortMethod)]
4549
public LinearRgb Convert(in CieXyz input)
4650
{
47-
Matrix4x4.Invert(this.conversionMatrix, out Matrix4x4 inverted);
48-
var vector = Vector3.Transform(input.ToVector3(), inverted);
51+
var vector = Vector3.Transform(input.ToVector3(), this.conversionMatrix);
52+
4953
return new LinearRgb(vector, this.TargetWorkingSpace);
5054
}
5155
}

src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/CmykAndRgbConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
99
{
1010
/// <summary>
11-
/// Color converter between <see cref="Cmyk"/> and <see cref="Rgb"/>
11+
/// Color converter between <see cref="Cmyk"/> and <see cref="Rgb"/>.
1212
/// </summary>
1313
internal sealed class CmykAndRgbConverter
1414
{
@@ -28,7 +28,7 @@ public Rgb Convert(in Cmyk input)
2828
/// Performs the conversion from the <see cref="Rgb"/> input to an instance of <see cref="Cmyk"/> type.
2929
/// </summary>
3030
/// <param name="input">The input color instance.</param>
31-
/// <returns>The converted result</returns>
31+
/// <returns>The converted result.</returns>
3232
[MethodImpl(InliningOptions.ShortMethod)]
3333
public Cmyk Convert(in Rgb input)
3434
{

src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/LinearRgbToRgbConverter.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@
66
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
77
{
88
/// <summary>
9-
/// Color converter between <see cref="LinearRgb"/> and <see cref="Rgb"/>
9+
/// Color converter between <see cref="LinearRgb"/> and <see cref="Rgb"/>.
1010
/// </summary>
1111
internal sealed class LinearRgbToRgbConverter
1212
{
1313
/// <summary>
1414
/// Performs the conversion from the <see cref="LinearRgb"/> input to an instance of <see cref="Rgb"/> type.
1515
/// </summary>
1616
/// <param name="input">The input color instance.</param>
17-
/// <returns>The converted result</returns>
17+
/// <returns>The converted result.</returns>
1818
[MethodImpl(InliningOptions.ShortMethod)]
1919
public Rgb Convert(in LinearRgb input)
2020
{
21-
var vector = input.ToVector3();
22-
vector.X = input.WorkingSpace.Compress(vector.X);
23-
vector.Y = input.WorkingSpace.Compress(vector.Y);
24-
vector.Z = input.WorkingSpace.Compress(vector.Z);
25-
26-
return new Rgb(vector, input.WorkingSpace);
21+
return new Rgb(
22+
r: input.WorkingSpace.Compress(input.R),
23+
g: input.WorkingSpace.Compress(input.G),
24+
b: input.WorkingSpace.Compress(input.B),
25+
workingSpace: input.WorkingSpace);
2726
}
2827
}
2928
}

0 commit comments

Comments
 (0)