|
| 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 the CIE L*C*h°, cylindrical form of the CIE L*a*b* 1976 color. |
| 11 | +/// <see href="https://en.wikipedia.org/wiki/Lab_color_space#Cylindrical_representation:_CIELCh_or_CIEHLC"/> |
| 12 | +/// </summary> |
| 13 | +public readonly struct CieLch : IColorProfile<CieLch, CieLab> |
| 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 | + private static readonly Vector3 Min = new(0, -200, 0); |
| 22 | + private static readonly Vector3 Max = new(100, 200, 360); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes a new instance of the <see cref="CieLch"/> struct. |
| 26 | + /// </summary> |
| 27 | + /// <param name="l">The lightness dimension.</param> |
| 28 | + /// <param name="c">The chroma, relative saturation.</param> |
| 29 | + /// <param name="h">The hue in degrees.</param> |
| 30 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 31 | + public CieLch(float l, float c, float h) |
| 32 | + : this(new Vector3(l, c, h)) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Initializes a new instance of the <see cref="CieLch"/> struct. |
| 38 | + /// </summary> |
| 39 | + /// <param name="vector">The vector representing the l, c, h components.</param> |
| 40 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 41 | + public CieLch(Vector3 vector) |
| 42 | + { |
| 43 | + vector = Vector3.Clamp(vector, Min, Max); |
| 44 | + this.L = vector.X; |
| 45 | + this.C = vector.Y; |
| 46 | + this.H = vector.Z; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets the lightness dimension. |
| 51 | + /// <remarks>A value ranging between 0 (black), 100 (diffuse white) or higher (specular white).</remarks> |
| 52 | + /// </summary> |
| 53 | + public readonly float L { get; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets the a chroma component. |
| 57 | + /// <remarks>A value ranging from 0 to 200.</remarks> |
| 58 | + /// </summary> |
| 59 | + public readonly float C { get; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets the h° hue component in degrees. |
| 63 | + /// <remarks>A value ranging from 0 to 360.</remarks> |
| 64 | + /// </summary> |
| 65 | + public readonly float H { get; } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Compares two <see cref="CieLch"/> objects for equality. |
| 69 | + /// </summary> |
| 70 | + /// <param name="left">The <see cref="CieLch"/> on the left side of the operand.</param> |
| 71 | + /// <param name="right">The <see cref="CieLch"/> on the right side of the operand.</param> |
| 72 | + /// <returns> |
| 73 | + /// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false. |
| 74 | + /// </returns> |
| 75 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 76 | + public static bool operator ==(CieLch left, CieLch right) => left.Equals(right); |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Compares two <see cref="CieLch"/> objects for inequality |
| 80 | + /// </summary> |
| 81 | + /// <param name="left">The <see cref="CieLch"/> on the left side of the operand.</param> |
| 82 | + /// <param name="right">The <see cref="CieLch"/> on the right side of the operand.</param> |
| 83 | + /// <returns> |
| 84 | + /// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false. |
| 85 | + /// </returns> |
| 86 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 87 | + public static bool operator !=(CieLch left, CieLch right) => !left.Equals(right); |
| 88 | + |
| 89 | + /// <inheritdoc/> |
| 90 | + public override int GetHashCode() |
| 91 | + => HashCode.Combine(this.L, this.C, this.H); |
| 92 | + |
| 93 | + /// <inheritdoc/> |
| 94 | + public override string ToString() => FormattableString.Invariant($"CieLch({this.L:#0.##}, {this.C:#0.##}, {this.H:#0.##})"); |
| 95 | + |
| 96 | + /// <inheritdoc/> |
| 97 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 98 | + public override bool Equals(object? obj) => obj is CieLch other && this.Equals(other); |
| 99 | + |
| 100 | + /// <inheritdoc/> |
| 101 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 102 | + public bool Equals(CieLch other) |
| 103 | + => this.L.Equals(other.L) |
| 104 | + && this.C.Equals(other.C) |
| 105 | + && this.H.Equals(other.H); |
| 106 | + |
| 107 | + /// <inheritdoc/> |
| 108 | + public static CieLch FromProfileConnectingSpace(ColorConversionOptions options, in CieLab source) |
| 109 | + { |
| 110 | + // Conversion algorithm described here: |
| 111 | + // https://en.wikipedia.org/wiki/Lab_color_space#Cylindrical_representation:_CIELCh_or_CIEHLC |
| 112 | + float l = source.L, a = source.A, b = source.B; |
| 113 | + float c = MathF.Sqrt((a * a) + (b * b)); |
| 114 | + float hRadians = MathF.Atan2(b, a); |
| 115 | + float hDegrees = GeometryUtilities.RadianToDegree(hRadians); |
| 116 | + |
| 117 | + // Wrap the angle round at 360. |
| 118 | + hDegrees %= 360; |
| 119 | + |
| 120 | + // Make sure it's not negative. |
| 121 | + while (hDegrees < 0) |
| 122 | + { |
| 123 | + hDegrees += 360; |
| 124 | + } |
| 125 | + |
| 126 | + return new CieLch(l, c, hDegrees); |
| 127 | + } |
| 128 | + |
| 129 | + /// <inheritdoc/> |
| 130 | + public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<CieLab> source, Span<CieLch> destination) |
| 131 | + { |
| 132 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 133 | + |
| 134 | + for (int i = 0; i < source.Length; i++) |
| 135 | + { |
| 136 | + CieLab lab = source[i]; |
| 137 | + destination[i] = FromProfileConnectingSpace(options, in lab); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /// <inheritdoc/> |
| 142 | + public CieLab ToProfileConnectingSpace(ColorConversionOptions options) |
| 143 | + { |
| 144 | + // Conversion algorithm described here: |
| 145 | + // https://en.wikipedia.org/wiki/Lab_color_space#Cylindrical_representation:_CIELCh_or_CIEHLC |
| 146 | + float l = this.L, c = this.C, hDegrees = this.H; |
| 147 | + float hRadians = GeometryUtilities.DegreeToRadian(hDegrees); |
| 148 | + |
| 149 | + float a = c * MathF.Cos(hRadians); |
| 150 | + float b = c * MathF.Sin(hRadians); |
| 151 | + |
| 152 | + return new CieLab(l, a, b); |
| 153 | + } |
| 154 | + |
| 155 | + /// <inheritdoc/> |
| 156 | + public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<CieLch> source, Span<CieLab> destination) |
| 157 | + { |
| 158 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 159 | + |
| 160 | + for (int i = 0; i < source.Length; i++) |
| 161 | + { |
| 162 | + CieLch lch = source[i]; |
| 163 | + destination[i] = lch.ToProfileConnectingSpace(options); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments