|
| 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 an CIE xyY 1931 color |
| 11 | +/// <see href="https://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space"/> |
| 12 | +/// </summary> |
| 13 | +public readonly struct CieXyy : IColorProfile<CieXyy, CieXyz> |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of the <see cref="CieXyy"/> struct. |
| 17 | + /// </summary> |
| 18 | + /// <param name="x">The x chroma component.</param> |
| 19 | + /// <param name="y">The y chroma component.</param> |
| 20 | + /// <param name="yl">The y luminance component.</param> |
| 21 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 22 | + public CieXyy(float x, float y, float yl) |
| 23 | + { |
| 24 | + // Not clamping as documentation about this space only indicates "usual" ranges |
| 25 | + this.X = x; |
| 26 | + this.Y = y; |
| 27 | + this.Yl = yl; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes a new instance of the <see cref="CieXyy"/> struct. |
| 32 | + /// </summary> |
| 33 | + /// <param name="vector">The vector representing the x, y, Y components.</param> |
| 34 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 35 | + public CieXyy(Vector3 vector) |
| 36 | + : this() |
| 37 | + { |
| 38 | + // Not clamping as documentation about this space only indicates "usual" ranges |
| 39 | + this.X = vector.X; |
| 40 | + this.Y = vector.Y; |
| 41 | + this.Yl = vector.Z; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the X chrominance component. |
| 46 | + /// <remarks>A value usually ranging between 0 and 1.</remarks> |
| 47 | + /// </summary> |
| 48 | + public readonly float X { get; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets the Y chrominance component. |
| 52 | + /// <remarks>A value usually ranging between 0 and 1.</remarks> |
| 53 | + /// </summary> |
| 54 | + public readonly float Y { get; } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets the Y luminance component. |
| 58 | + /// <remarks>A value usually ranging between 0 and 1.</remarks> |
| 59 | + /// </summary> |
| 60 | + public readonly float Yl { get; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Compares two <see cref="CieXyy"/> objects for equality. |
| 64 | + /// </summary> |
| 65 | + /// <param name="left">The <see cref="CieXyy"/> on the left side of the operand.</param> |
| 66 | + /// <param name="right">The <see cref="CieXyy"/> 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(InliningOptions.ShortMethod)] |
| 71 | + public static bool operator ==(CieXyy left, CieXyy right) => left.Equals(right); |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Compares two <see cref="CieXyy"/> objects for inequality. |
| 75 | + /// </summary> |
| 76 | + /// <param name="left">The <see cref="CieXyy"/> on the left side of the operand.</param> |
| 77 | + /// <param name="right">The <see cref="CieXyy"/> 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(InliningOptions.ShortMethod)] |
| 82 | + public static bool operator !=(CieXyy left, CieXyy right) => !left.Equals(right); |
| 83 | + |
| 84 | + /// <inheritdoc/> |
| 85 | + public static CieXyy FromProfileConnectingSpace(ColorConversionOptions options, in CieXyz source) |
| 86 | + { |
| 87 | + float x = source.X / (source.X + source.Y + source.Z); |
| 88 | + float y = source.Y / (source.X + source.Y + source.Z); |
| 89 | + |
| 90 | + if (float.IsNaN(x) || float.IsNaN(y)) |
| 91 | + { |
| 92 | + return new CieXyy(0, 0, source.Y); |
| 93 | + } |
| 94 | + |
| 95 | + return new CieXyy(x, y, source.Y); |
| 96 | + } |
| 97 | + |
| 98 | + /// <inheritdoc/> |
| 99 | + public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<CieXyz> source, Span<CieXyy> destination) |
| 100 | + { |
| 101 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 102 | + for (int i = 0; i < source.Length; i++) |
| 103 | + { |
| 104 | + CieXyz xyz = source[i]; |
| 105 | + destination[i] = FromProfileConnectingSpace(options, in xyz); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// <inheritdoc/> |
| 110 | + public CieXyz ToProfileConnectingSpace(ColorConversionOptions options) |
| 111 | + { |
| 112 | + if (MathF.Abs(this.Y) < Constants.Epsilon) |
| 113 | + { |
| 114 | + return new CieXyz(0, 0, this.Yl); |
| 115 | + } |
| 116 | + |
| 117 | + float x = (this.X * this.Yl) / this.Y; |
| 118 | + float y = this.Yl; |
| 119 | + float z = ((1 - this.X - this.Y) * y) / this.Y; |
| 120 | + |
| 121 | + return new CieXyz(x, y, z); |
| 122 | + } |
| 123 | + |
| 124 | + /// <inheritdoc/> |
| 125 | + public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<CieXyy> source, Span<CieXyz> destination) |
| 126 | + { |
| 127 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 128 | + for (int i = 0; i < source.Length; i++) |
| 129 | + { |
| 130 | + CieXyy xyz = source[i]; |
| 131 | + destination[i] = xyz.ToProfileConnectingSpace(options); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// <inheritdoc/> |
| 136 | + public static ChromaticAdaptionWhitePointSource GetChromaticAdaptionWhitePointSource() |
| 137 | + => ChromaticAdaptionWhitePointSource.WhitePoint; |
| 138 | + |
| 139 | + /// <inheritdoc/> |
| 140 | + public override int GetHashCode() |
| 141 | + => HashCode.Combine(this.X, this.Y, this.Yl); |
| 142 | + |
| 143 | + /// <inheritdoc/> |
| 144 | + public override string ToString() |
| 145 | + => FormattableString.Invariant($"CieXyy({this.X:#0.##}, {this.Y:#0.##}, {this.Yl:#0.##})"); |
| 146 | + |
| 147 | + /// <inheritdoc/> |
| 148 | + public override bool Equals(object? obj) => obj is CieXyy other && this.Equals(other); |
| 149 | + |
| 150 | + /// <inheritdoc/> |
| 151 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 152 | + public bool Equals(CieXyy other) |
| 153 | + => this.X.Equals(other.X) |
| 154 | + && this.Y.Equals(other.Y) |
| 155 | + && this.Yl.Equals(other.Yl); |
| 156 | +} |
0 commit comments