using System;
namespace Prism.Glass
{
///
/// The one-pixel-wide strips of a color picker. Each fills a buffer of length pixels, so it serves as a row
/// or as a column. The first pixel is the start of the strip and the last is its end exactly.
///
public static class ColorStrips
{
private static double Along(int index, int length)
{
return index / (double)(length - 1);
}
private static void CheckLength(byte[] pixels, int length)
{
if (length < 2)
{
throw new ArgumentOutOfRangeException(nameof(length), length, "A strip needs at least 2 pixels.");
}
Rgba.CheckBuffer(pixels, length, 1);
}
///
/// Fills the strip that runs from black up to this hue and saturation at full value, opaque.
///
/// The buffer isn't length pixels.
///
/// The length is less than 2, or the saturation isn't 0 to 1.
///
public static void FillValue(byte[] pixels, int length, double hue, double saturation)
{
CheckLength(pixels, length);
for (int i = 0; i < length; i++)
{
Rgba.Write(pixels, i, new Hsv(hue, saturation, Along(i, length)).ToColor());
}
}
///
/// Fills the strip that runs from fully transparent up to fully opaque in this color. The color's own alpha is
/// ignored.
///
/// The buffer isn't length pixels.
/// The length is less than 2.
public static void FillAlpha(byte[] pixels, int length, Color color)
{
CheckLength(pixels, length);
for (int i = 0; i < length; i++)
{
Rgba.Write(pixels, i, Color.FromArgb((int)Math.Round(Along(i, length) * byte.MaxValue), color));
}
}
///
/// Fills the strip that runs along a gradient, with its own alpha. Each pixel is the gradient at its place, so
/// a hard edge lands on the pixel it falls on.
///
/// The gradient is null.
/// The buffer isn't length pixels.
/// The length is less than 2.
public static void FillGradient(byte[] pixels, int length, Gradient gradient)
{
if (gradient == null)
{
throw new ArgumentNullException(nameof(gradient));
}
CheckLength(pixels, length);
for (int i = 0; i < length; i++)
{
Rgba.Write(pixels, i, gradient.Evaluate(Along(i, length)));
}
}
}
}