|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Numerics; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | + |
| 7 | +namespace SixLabors.ImageSharp.ColorProfiles; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Represents a CIE L*a*b* 1976 color. |
| 11 | +/// <see href="https://en.wikipedia.org/wiki/Lab_color_space"/> |
| 12 | +/// </summary> |
| 13 | +public readonly struct CieLab : IProfileConnectingSpace<CieLab, CieXyz> |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// D50 standard illuminant. |
| 17 | + /// Used when reference white is not specified explicitly. |
| 18 | + /// </summary> |
| 19 | + public static readonly CieXyz DefaultWhitePoint = Illuminants.D50; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes a new instance of the <see cref="CieLab"/> struct. |
| 23 | + /// </summary> |
| 24 | + /// <param name="l">The lightness dimension.</param> |
| 25 | + /// <param name="a">The a (green - magenta) component.</param> |
| 26 | + /// <param name="b">The b (blue - yellow) component.</param> |
| 27 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 28 | + public CieLab(float l, float a, float b) |
| 29 | + : this(new Vector3(l, a, b)) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Initializes a new instance of the <see cref="CieLab"/> struct. |
| 35 | + /// </summary> |
| 36 | + /// <param name="vector">The vector representing the l, a, b components.</param> |
| 37 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 38 | + public CieLab(Vector3 vector) |
| 39 | + : this() |
| 40 | + { |
| 41 | + // Not clamping as documentation about this space only indicates "usual" ranges |
| 42 | + this.L = vector.X; |
| 43 | + this.A = vector.Y; |
| 44 | + this.B = vector.Z; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets the lightness dimension. |
| 49 | + /// <remarks>A value usually ranging between 0 (black), 100 (diffuse white) or higher (specular white).</remarks> |
| 50 | + /// </summary> |
| 51 | + public readonly float L { get; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets the a color component. |
| 55 | + /// <remarks>A value usually ranging from -100 to 100. Negative is green, positive magenta.</remarks> |
| 56 | + /// </summary> |
| 57 | + public readonly float A { get; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Gets the b color component. |
| 61 | + /// <remarks>A value usually ranging from -100 to 100. Negative is blue, positive is yellow</remarks> |
| 62 | + /// </summary> |
| 63 | + public readonly float B { get; } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Compares two <see cref="CieLab"/> objects for equality. |
| 67 | + /// </summary> |
| 68 | + /// <param name="left">The <see cref="CieLab"/> on the left side of the operand.</param> |
| 69 | + /// <param name="right">The <see cref="CieLab"/> on the right side of the operand.</param> |
| 70 | + /// <returns> |
| 71 | + /// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false. |
| 72 | + /// </returns> |
| 73 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 74 | + public static bool operator ==(CieLab left, CieLab right) => left.Equals(right); |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Compares two <see cref="CieLab"/> objects for inequality |
| 78 | + /// </summary> |
| 79 | + /// <param name="left">The <see cref="CieLab"/> on the left side of the operand.</param> |
| 80 | + /// <param name="right">The <see cref="CieLab"/> on the right side of the operand.</param> |
| 81 | + /// <returns> |
| 82 | + /// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false. |
| 83 | + /// </returns> |
| 84 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 85 | + public static bool operator !=(CieLab left, CieLab right) => !left.Equals(right); |
| 86 | + |
| 87 | + /// <inheritdoc/> |
| 88 | + public override int GetHashCode() => HashCode.Combine(this.L, this.A, this.B); |
| 89 | + |
| 90 | + /// <inheritdoc/> |
| 91 | + public override string ToString() => FormattableString.Invariant($"CieLab({this.L:#0.##}, {this.A:#0.##}, {this.B:#0.##})"); |
| 92 | + |
| 93 | + /// <inheritdoc/> |
| 94 | + public override bool Equals(object? obj) => obj is CieLab other && this.Equals(other); |
| 95 | + |
| 96 | + /// <inheritdoc/> |
| 97 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 98 | + public bool Equals(CieLab other) => |
| 99 | + this.L.Equals(other.L) |
| 100 | + && this.A.Equals(other.A) |
| 101 | + && this.B.Equals(other.B); |
| 102 | + |
| 103 | + /// <inheritdoc/> |
| 104 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 105 | + public static CieLab FromProfileConnectingSpace(ColorConversionOptions options, in CieXyz source) |
| 106 | + { |
| 107 | + // Conversion algorithm described here: http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_Lab.html |
| 108 | + CieXyz whitePoint = options.TargetWhitePoint; |
| 109 | + float wx = whitePoint.X, wy = whitePoint.Y, wz = whitePoint.Z; |
| 110 | + |
| 111 | + float xr = source.X / wx, yr = source.Y / wy, zr = source.Z / wz; |
| 112 | + |
| 113 | + const float inv116 = 1 / 116F; |
| 114 | + |
| 115 | + float fx = xr > CieConstants.Epsilon ? MathF.Pow(xr, 0.3333333F) : ((CieConstants.Kappa * xr) + 16F) * inv116; |
| 116 | + float fy = yr > CieConstants.Epsilon ? MathF.Pow(yr, 0.3333333F) : ((CieConstants.Kappa * yr) + 16F) * inv116; |
| 117 | + float fz = zr > CieConstants.Epsilon ? MathF.Pow(zr, 0.3333333F) : ((CieConstants.Kappa * zr) + 16F) * inv116; |
| 118 | + |
| 119 | + float l = (116F * fy) - 16F; |
| 120 | + float a = 500F * (fx - fy); |
| 121 | + float b = 200F * (fy - fz); |
| 122 | + |
| 123 | + return new CieLab(l, a, b); |
| 124 | + } |
| 125 | + |
| 126 | + /// <inheritdoc/> |
| 127 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 128 | + public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<CieXyz> source, Span<CieLab> destination) |
| 129 | + { |
| 130 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 131 | + |
| 132 | + for (int i = 0; i < source.Length; i++) |
| 133 | + { |
| 134 | + CieXyz xyz = source[i]; |
| 135 | + destination[i] = FromProfileConnectingSpace(options, in xyz); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /// <inheritdoc/> |
| 140 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 141 | + public CieXyz ToProfileConnectingSpace(ColorConversionOptions options) |
| 142 | + { |
| 143 | + // Conversion algorithm described here: http://www.brucelindbloom.com/index.html?Eqn_Lab_to_XYZ.html |
| 144 | + float l = this.L, a = this.A, b = this.B; |
| 145 | + float fy = (l + 16) / 116F; |
| 146 | + float fx = (a / 500F) + fy; |
| 147 | + float fz = fy - (b / 200F); |
| 148 | + |
| 149 | + float fx3 = Numerics.Pow3(fx); |
| 150 | + float fz3 = Numerics.Pow3(fz); |
| 151 | + |
| 152 | + float xr = fx3 > CieConstants.Epsilon ? fx3 : ((116F * fx) - 16F) / CieConstants.Kappa; |
| 153 | + float yr = l > CieConstants.Kappa * CieConstants.Epsilon ? Numerics.Pow3((l + 16F) / 116F) : l / CieConstants.Kappa; |
| 154 | + float zr = fz3 > CieConstants.Epsilon ? fz3 : ((116F * fz) - 16F) / CieConstants.Kappa; |
| 155 | + |
| 156 | + CieXyz whitePoint = options.WhitePoint; |
| 157 | + Vector3 wxyz = new(whitePoint.X, whitePoint.Y, whitePoint.Z); |
| 158 | + |
| 159 | + // Avoids XYZ coordinates out range (restricted by 0 and XYZ reference white) |
| 160 | + Vector3 xyzr = Vector3.Clamp(new Vector3(xr, yr, zr), Vector3.Zero, Vector3.One); |
| 161 | + |
| 162 | + return new(xyzr * wxyz); |
| 163 | + } |
| 164 | + |
| 165 | + /// <inheritdoc/> |
| 166 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 167 | + public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<CieLab> source, Span<CieXyz> destination) |
| 168 | + { |
| 169 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 170 | + |
| 171 | + for (int i = 0; i < source.Length; i++) |
| 172 | + { |
| 173 | + CieLab lab = source[i]; |
| 174 | + destination[i] = lab.ToProfileConnectingSpace(options); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments