|
| 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 Hsl (hue, saturation, lightness) color. |
| 11 | +/// </summary> |
| 12 | +public readonly struct Hsl : IColorProfile<Hsl, Rgb> |
| 13 | +{ |
| 14 | + private static readonly Vector3 Min = Vector3.Zero; |
| 15 | + private static readonly Vector3 Max = new(360, 1, 1); |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="Hsl"/> struct. |
| 19 | + /// </summary> |
| 20 | + /// <param name="h">The h hue component.</param> |
| 21 | + /// <param name="s">The s saturation component.</param> |
| 22 | + /// <param name="l">The l value (lightness) component.</param> |
| 23 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 24 | + public Hsl(float h, float s, float l) |
| 25 | + : this(new Vector3(h, s, l)) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Initializes a new instance of the <see cref="Hsl"/> struct. |
| 31 | + /// </summary> |
| 32 | + /// <param name="vector">The vector representing the h, s, l components.</param> |
| 33 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 34 | + public Hsl(Vector3 vector) |
| 35 | + { |
| 36 | + vector = Vector3.Clamp(vector, Min, Max); |
| 37 | + this.H = vector.X; |
| 38 | + this.S = vector.Y; |
| 39 | + this.L = vector.Z; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Gets the hue component. |
| 44 | + /// <remarks>A value ranging between 0 and 360.</remarks> |
| 45 | + /// </summary> |
| 46 | + public readonly float H { get; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets the saturation component. |
| 50 | + /// <remarks>A value ranging between 0 and 1.</remarks> |
| 51 | + /// </summary> |
| 52 | + public readonly float S { get; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets the lightness component. |
| 56 | + /// <remarks>A value ranging between 0 and 1.</remarks> |
| 57 | + /// </summary> |
| 58 | + public readonly float L { get; } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Compares two <see cref="Hsl"/> objects for equality. |
| 62 | + /// </summary> |
| 63 | + /// <param name="left"> |
| 64 | + /// The <see cref="Hsl"/> on the left side of the operand. |
| 65 | + /// </param> |
| 66 | + /// <param name="right">The <see cref="Hsl"/> on the right side of the operand.</param> |
| 67 | + /// <returns> |
| 68 | + /// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false. |
| 69 | + /// </returns> |
| 70 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 71 | + public static bool operator ==(Hsl left, Hsl right) => left.Equals(right); |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Compares two <see cref="Hsl"/> objects for inequality. |
| 75 | + /// </summary> |
| 76 | + /// <param name="left">The <see cref="Hsl"/> on the left side of the operand.</param> |
| 77 | + /// <param name="right">The <see cref="Hsl"/> on the right side of the operand.</param> |
| 78 | + /// <returns> |
| 79 | + /// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false. |
| 80 | + /// </returns> |
| 81 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 82 | + public static bool operator !=(Hsl left, Hsl right) => !left.Equals(right); |
| 83 | + |
| 84 | + /// <inheritdoc/> |
| 85 | + public static Hsl FromProfileConnectingSpace(ColorConversionOptions options, in Rgb source) |
| 86 | + { |
| 87 | + float r = source.R; |
| 88 | + float g = source.G; |
| 89 | + float b = source.B; |
| 90 | + |
| 91 | + float max = MathF.Max(r, MathF.Max(g, b)); |
| 92 | + float min = MathF.Min(r, MathF.Min(g, b)); |
| 93 | + float chroma = max - min; |
| 94 | + float h = 0F; |
| 95 | + float s = 0F; |
| 96 | + float l = (max + min) / 2F; |
| 97 | + |
| 98 | + if (MathF.Abs(chroma) < Constants.Epsilon) |
| 99 | + { |
| 100 | + return new Hsl(0F, s, l); |
| 101 | + } |
| 102 | + |
| 103 | + if (MathF.Abs(r - max) < Constants.Epsilon) |
| 104 | + { |
| 105 | + h = (g - b) / chroma; |
| 106 | + } |
| 107 | + else if (MathF.Abs(g - max) < Constants.Epsilon) |
| 108 | + { |
| 109 | + h = 2F + ((b - r) / chroma); |
| 110 | + } |
| 111 | + else if (MathF.Abs(b - max) < Constants.Epsilon) |
| 112 | + { |
| 113 | + h = 4F + ((r - g) / chroma); |
| 114 | + } |
| 115 | + |
| 116 | + h *= 60F; |
| 117 | + if (h < 0F) |
| 118 | + { |
| 119 | + h += 360F; |
| 120 | + } |
| 121 | + |
| 122 | + if (l <= .5F) |
| 123 | + { |
| 124 | + s = chroma / (max + min); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + s = chroma / (2F - max - min); |
| 129 | + } |
| 130 | + |
| 131 | + return new Hsl(h, s, l); |
| 132 | + } |
| 133 | + |
| 134 | + /// <inheritdoc/> |
| 135 | + public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<Rgb> source, Span<Hsl> destination) |
| 136 | + { |
| 137 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 138 | + for (int i = 0; i < source.Length; i++) |
| 139 | + { |
| 140 | + Rgb rgb = source[i]; |
| 141 | + destination[i] = FromProfileConnectingSpace(options, in rgb); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// <inheritdoc/> |
| 146 | + public Rgb ToProfileConnectingSpace(ColorConversionOptions options) |
| 147 | + { |
| 148 | + float rangedH = this.H / 360F; |
| 149 | + float r = 0; |
| 150 | + float g = 0; |
| 151 | + float b = 0; |
| 152 | + float s = this.S; |
| 153 | + float l = this.L; |
| 154 | + |
| 155 | + if (MathF.Abs(l) > Constants.Epsilon) |
| 156 | + { |
| 157 | + if (MathF.Abs(s) < Constants.Epsilon) |
| 158 | + { |
| 159 | + r = g = b = l; |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + float temp2 = (l < .5F) ? l * (1F + s) : l + s - (l * s); |
| 164 | + float temp1 = (2F * l) - temp2; |
| 165 | + |
| 166 | + r = GetColorComponent(temp1, temp2, rangedH + 0.3333333F); |
| 167 | + g = GetColorComponent(temp1, temp2, rangedH); |
| 168 | + b = GetColorComponent(temp1, temp2, rangedH - 0.3333333F); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return new Rgb(r, g, b); |
| 173 | + } |
| 174 | + |
| 175 | + /// <inheritdoc/> |
| 176 | + public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<Hsl> source, Span<Rgb> destination) |
| 177 | + { |
| 178 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 179 | + for (int i = 0; i < source.Length; i++) |
| 180 | + { |
| 181 | + Hsl hsl = source[i]; |
| 182 | + destination[i] = hsl.ToProfileConnectingSpace(options); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /// <inheritdoc/> |
| 187 | + public static ChromaticAdaptionWhitePointSource GetChromaticAdaptionWhitePointSource() |
| 188 | + => ChromaticAdaptionWhitePointSource.RgbWorkingSpace; |
| 189 | + |
| 190 | + /// <inheritdoc/> |
| 191 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 192 | + public override int GetHashCode() => HashCode.Combine(this.H, this.S, this.L); |
| 193 | + |
| 194 | + /// <inheritdoc/> |
| 195 | + public override string ToString() => FormattableString.Invariant($"Hsl({this.H:#0.##}, {this.S:#0.##}, {this.L:#0.##})"); |
| 196 | + |
| 197 | + /// <inheritdoc/> |
| 198 | + public override bool Equals(object? obj) => obj is Hsl other && this.Equals(other); |
| 199 | + |
| 200 | + /// <inheritdoc/> |
| 201 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 202 | + public bool Equals(Hsl other) |
| 203 | + => this.H.Equals(other.H) |
| 204 | + && this.S.Equals(other.S) |
| 205 | + && this.L.Equals(other.L); |
| 206 | + |
| 207 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 208 | + private static float GetColorComponent(float first, float second, float third) |
| 209 | + { |
| 210 | + third = MoveIntoRange(third); |
| 211 | + if (third < 0.1666667F) |
| 212 | + { |
| 213 | + return first + ((second - first) * 6F * third); |
| 214 | + } |
| 215 | + |
| 216 | + if (third < .5F) |
| 217 | + { |
| 218 | + return second; |
| 219 | + } |
| 220 | + |
| 221 | + if (third < 0.6666667F) |
| 222 | + { |
| 223 | + return first + ((second - first) * (0.6666667F - third) * 6F); |
| 224 | + } |
| 225 | + |
| 226 | + return first; |
| 227 | + } |
| 228 | + |
| 229 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 230 | + private static float MoveIntoRange(float value) |
| 231 | + { |
| 232 | + if (value < 0F) |
| 233 | + { |
| 234 | + value++; |
| 235 | + } |
| 236 | + else if (value > 1F) |
| 237 | + { |
| 238 | + value--; |
| 239 | + } |
| 240 | + |
| 241 | + return value; |
| 242 | + } |
| 243 | +} |
0 commit comments