using System;
using RGB.NET.Core;
using RGB.NET.Presets.Textures.Sampler;
namespace RGB.NET.Presets.Textures;
///
///
/// Represents a texture of byte-data pixels.
///
public sealed class BytePixelTexture : PixelTexture
{
#region Properties & Fields
private readonly byte[] _data;
///
protected override ReadOnlySpan Data => _data;
///
/// Gets the color format the data is in.
///
public ColorFormat ColorFormat { get; }
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
/// A is used.
///
/// The width of the texture.
/// The height of the texture.
/// The pixel-data of the texture.
/// The color format the data is in. (default: RGB)
public BytePixelTexture(int with, int height, byte[] data, ColorFormat colorFormat = ColorFormat.RGB)
: this(with, height, data, new AverageByteSampler(), colorFormat)
{ }
///
/// Initializes a new instance of the class.
///
/// The width of the texture.
/// The height of the texture.
/// The pixel-data of the texture.
/// The sampler used to get the color of a region.
/// The color format the data is in. (default: RGB)
public BytePixelTexture(int with, int height, byte[] data, ISampler sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, 3, sampler)
{
this._data = data;
this.ColorFormat = colorFormat;
if (Data.Length != ((with * height) * 3)) throw new ArgumentException($"Data-Length {Data.Length} differs from the specified size {with}x{height} * 3 bytes ({with * height * 3}).");
}
#endregion
#region Methods
///
protected override Color GetColor(in ReadOnlySpan pixel)
{
return ColorFormat switch
{
ColorFormat.RGB => new Color(pixel[0], pixel[1], pixel[2]),
ColorFormat.BGR => new Color(pixel[2], pixel[1], pixel[0]),
_ => throw new ArgumentOutOfRangeException()
};
}
#endregion
}