|
| 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 CMYK (cyan, magenta, yellow, keyline) color. |
| 11 | +/// </summary> |
| 12 | +public readonly struct Cmyk : IColorProfile<Cmyk, Rgb> |
| 13 | +{ |
| 14 | + private static readonly Vector4 Min = Vector4.Zero; |
| 15 | + private static readonly Vector4 Max = Vector4.One; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="Cmyk"/> struct. |
| 19 | + /// </summary> |
| 20 | + /// <param name="c">The cyan component.</param> |
| 21 | + /// <param name="m">The magenta component.</param> |
| 22 | + /// <param name="y">The yellow component.</param> |
| 23 | + /// <param name="k">The keyline black component.</param> |
| 24 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 25 | + public Cmyk(float c, float m, float y, float k) |
| 26 | + : this(new Vector4(c, m, y, k)) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes a new instance of the <see cref="Cmyk"/> struct. |
| 32 | + /// </summary> |
| 33 | + /// <param name="vector">The vector representing the c, m, y, k components.</param> |
| 34 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 35 | + public Cmyk(Vector4 vector) |
| 36 | + { |
| 37 | + vector = Numerics.Clamp(vector, Min, Max); |
| 38 | + this.C = vector.X; |
| 39 | + this.M = vector.Y; |
| 40 | + this.Y = vector.Z; |
| 41 | + this.K = vector.W; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the cyan color component. |
| 46 | + /// <remarks>A value ranging between 0 and 1.</remarks> |
| 47 | + /// </summary> |
| 48 | + public readonly float C { get; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets the magenta color component. |
| 52 | + /// <remarks>A value ranging between 0 and 1.</remarks> |
| 53 | + /// </summary> |
| 54 | + public readonly float M { get; } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets the yellow color component. |
| 58 | + /// <remarks>A value ranging between 0 and 1.</remarks> |
| 59 | + /// </summary> |
| 60 | + public readonly float Y { get; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets the keyline black color component. |
| 64 | + /// <remarks>A value ranging between 0 and 1.</remarks> |
| 65 | + /// </summary> |
| 66 | + public readonly float K { get; } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Compares two <see cref="Cmyk"/> objects for equality. |
| 70 | + /// </summary> |
| 71 | + /// <param name="left">The <see cref="Cmyk"/> on the left side of the operand.</param> |
| 72 | + /// <param name="right">The <see cref="Cmyk"/> on the right side of the operand.</param> |
| 73 | + /// <returns> |
| 74 | + /// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false. |
| 75 | + /// </returns> |
| 76 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 77 | + public static bool operator ==(Cmyk left, Cmyk right) => left.Equals(right); |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Compares two <see cref="Cmyk"/> objects for inequality. |
| 81 | + /// </summary> |
| 82 | + /// <param name="left">The <see cref="Cmyk"/> on the left side of the operand.</param> |
| 83 | + /// <param name="right">The <see cref="Cmyk"/> on the right side of the operand.</param> |
| 84 | + /// <returns> |
| 85 | + /// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false. |
| 86 | + /// </returns> |
| 87 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 88 | + public static bool operator !=(Cmyk left, Cmyk right) => !left.Equals(right); |
| 89 | + |
| 90 | + /// <inheritdoc/> |
| 91 | + public static Cmyk FromProfileConnectingSpace(ColorConversionOptions options, in Rgb source) |
| 92 | + { |
| 93 | + // To CMY |
| 94 | + Vector3 cmy = Vector3.One - source.ToScaledVector3(); |
| 95 | + |
| 96 | + // To CMYK |
| 97 | + Vector3 k = new(MathF.Min(cmy.X, MathF.Min(cmy.Y, cmy.Z))); |
| 98 | + |
| 99 | + if (MathF.Abs(k.X - 1F) < Constants.Epsilon) |
| 100 | + { |
| 101 | + return new Cmyk(0, 0, 0, 1F); |
| 102 | + } |
| 103 | + |
| 104 | + cmy = (cmy - k) / (Vector3.One - k); |
| 105 | + |
| 106 | + return new Cmyk(cmy.X, cmy.Y, cmy.Z, k.X); |
| 107 | + } |
| 108 | + |
| 109 | + /// <inheritdoc/> |
| 110 | + public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<Rgb> source, Span<Cmyk> destination) |
| 111 | + { |
| 112 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 113 | + |
| 114 | + // TODO: We can optimize this by using SIMD |
| 115 | + for (int i = 0; i < source.Length; i++) |
| 116 | + { |
| 117 | + Rgb rgb = source[i]; |
| 118 | + destination[i] = FromProfileConnectingSpace(options, in rgb); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /// <inheritdoc/> |
| 123 | + public Rgb ToProfileConnectingSpace(ColorConversionOptions options) |
| 124 | + { |
| 125 | + Vector3 rgb = (Vector3.One - new Vector3(this.C, this.M, this.Y)) * (Vector3.One - new Vector3(this.K)); |
| 126 | + return Rgb.FromScaledVector3(rgb); |
| 127 | + } |
| 128 | + |
| 129 | + /// <inheritdoc/> |
| 130 | + public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<Cmyk> source, Span<Rgb> destination) |
| 131 | + { |
| 132 | + // TODO: We can possibly optimize this by using SIMD |
| 133 | + for (int i = 0; i < source.Length; i++) |
| 134 | + { |
| 135 | + Cmyk cmyk = source[i]; |
| 136 | + destination[i] = cmyk.ToProfileConnectingSpace(options); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /// <inheritdoc/> |
| 141 | + public static ChromaticAdaptionWhitePointSource GetChromaticAdaptionWhitePointSource() |
| 142 | + => ChromaticAdaptionWhitePointSource.RgbWorkingSpace; |
| 143 | + |
| 144 | + /// <inheritdoc/> |
| 145 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 146 | + public override int GetHashCode() |
| 147 | + => HashCode.Combine(this.C, this.M, this.Y, this.K); |
| 148 | + |
| 149 | + /// <inheritdoc/> |
| 150 | + public override string ToString() |
| 151 | + => FormattableString.Invariant($"Cmyk({this.C:#0.##}, {this.M:#0.##}, {this.Y:#0.##}, {this.K:#0.##})"); |
| 152 | + |
| 153 | + /// <inheritdoc/> |
| 154 | + public override bool Equals(object? obj) |
| 155 | + => obj is Cmyk other && this.Equals(other); |
| 156 | + |
| 157 | + /// <inheritdoc/> |
| 158 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 159 | + public bool Equals(Cmyk other) |
| 160 | + => this.C.Equals(other.C) |
| 161 | + && this.M.Equals(other.M) |
| 162 | + && this.Y.Equals(other.Y) |
| 163 | + && this.K.Equals(other.K); |
| 164 | +} |
0 commit comments