// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.ColorProfiles;
namespace SixLabors.ImageSharp.Tests.ColorProfiles;
///
/// Tests - conversions.
///
///
/// Test data generated mathematically
///
public class RgbAndYccKConversionTests
{
private static readonly ApproximateColorProfileComparer Comparer = new(.001F);
[Theory]
[InlineData(1, .5F, .5F, 0, 1, 1, 1)]
[InlineData(0, .5F, .5F, 1, 0, 0, 0)]
[InlineData(.5F, .5F, .5F, 0, .5F, .5F, .5F)]
public void Convert_YccK_To_Rgb(float y, float cb, float cr, float k, float r, float g, float b)
{
// Arrange
YccK input = new(y, cb, cr, k);
Rgb expected = new(r, g, b);
ColorProfileConverter converter = new();
Span inputSpan = new YccK[5];
inputSpan.Fill(input);
Span actualSpan = new Rgb[5];
// Act
Rgb actual = converter.Convert(input);
converter.Convert(inputSpan, actualSpan);
// Assert
Assert.Equal(expected, actual, Comparer);
for (int i = 0; i < actualSpan.Length; i++)
{
Assert.Equal(expected, actualSpan[i], Comparer);
}
}
[Theory]
[InlineData(1, 1, 1, 1, .5F, .5F, 0)]
[InlineData(0, 0, 0, 0, .5F, .5F, 1)]
[InlineData(.5F, .5F, .5F, 1, .5F, .5F, .5F)]
public void Convert_Rgb_To_YccK(float r, float g, float b, float y, float cb, float cr, float k)
{
// Multiple YccK representations can decode to the same RGB value.
// For example, (Y=1.0, Cb=0.5, Cr=0.5, K=0.5) and (Y=0.5, Cb=0.5, Cr=0.5, K=0.0) both yield RGB (0.5, 0.5, 0.5).
// This is expected because YccK is not a unique encoding — K modulates RGB after YCbCr decoding.
// Round-tripping RGB -> YccK -> RGB is stable, but YccK -> RGB -> YccK is not injective.
// Arrange
Rgb input = new(r, g, b);
YccK expected = new(y, cb, cr, k);
ColorProfileConverter converter = new();
Span inputSpan = new Rgb[5];
inputSpan.Fill(input);
Span actualSpan = new YccK[5];
// Act
YccK actual = converter.Convert(input);
converter.Convert(inputSpan, actualSpan);
// Assert
Assert.Equal(expected, actual, Comparer);
for (int i = 0; i < actualSpan.Length; i++)
{
Assert.Equal(expected, actualSpan[i], Comparer);
}
}
}