using System;
using Prism;
using Prism.Glass;
namespace Konis.Testbed
{
///
/// The pixels of the color picker's and gradient editor's art, as straight-alpha RGBA rows from the top down.
///
public static class PickerArt
{
private static readonly Color Dark = Color.FromArgb(255, 30, 30, 34);
private static readonly Color Light = Color.FromArgb(255, 255, 255, 255);
private static readonly Color Clear = Color.FromArgb(0, 0, 0, 0);
// Runs from the bottom of the image to its top, as the strips are drawn from low to high.
private static byte[] Column(byte[] strip, int length, int width)
{
var returnMe = new byte[Rgba.ByteCount(width, length)];
for (int row = 0; row < length; row++)
{
int from = (length - 1 - row) * 4;
for (int column = 0; column < width; column++)
{
Array.Copy(strip, from, returnMe, (row * width + column) * 4, 4);
}
}
return returnMe;
}
///
/// Returns the strip that runs from black at the bottom to white at the top.
///
public static byte[] ValueStrip(int width, int height)
{
var strip = new byte[Rgba.ByteCount(height, 1)];
ColorStrips.FillValue(strip, height, hue: 0, saturation: 0);
return Column(strip, height, width);
}
///
/// Returns the strip that runs from transparent at the bottom to opaque white at the top.
///
public static byte[] AlphaStrip(int width, int height)
{
var strip = new byte[Rgba.ByteCount(height, 1)];
ColorStrips.FillAlpha(strip, height, Light);
return Column(strip, height, width);
}
///
/// Returns the hue and saturation disc at full value.
///
public static byte[] Disc(int diameter)
{
var returnMe = new byte[Rgba.ByteCount(diameter, diameter)];
HueDisc.Fill(returnMe, diameter, value: 1);
return returnMe;
}
///
/// Returns a checkerboard of light and dark gray cells, for a transparent color to show against.
///
public static byte[] Backdrop(int width, int height, int cell)
{
var returnMe = new byte[Rgba.ByteCount(width, height)];
Checkerboard.Fill(
returnMe, width, height, cell, Color.FromArgb(255, 200, 200, 200), Color.FromArgb(255, 130, 130, 130));
return returnMe;
}
private static bool InTriangle(int x, int y, int width, int height, bool selected)
{
if (x < 0 || x >= width || y < 0 || y >= height)
{
return false;
}
double half = width / 2.0;
double reach = selected ? Math.Min(1, (y + 1) / (height / 2.0)) * half : (y + 1) * half / height;
return Math.Abs(x + 0.5 - half) <= reach;
}
///
/// Returns a white arrow pointing up, outlined in dark. The selected one is bulkier, since both are tinted.
///
public static byte[] Marker(int width, int height, bool selected)
{
var returnMe = new byte[Rgba.ByteCount(width, height)];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color color = Clear;
if (InTriangle(x, y, width, height, selected))
{
bool edge = !InTriangle(x - 1, y, width, height, selected)
|| !InTriangle(x + 1, y, width, height, selected)
|| !InTriangle(x, y - 1, width, height, selected)
|| !InTriangle(x, y + 1, width, height, selected);
color = edge ? Dark : Light;
}
Rgba.Write(returnMe, y * width + x, color);
}
}
return returnMe;
}
///
/// Returns a white ring between two dark ones, with the middle left open.
///
public static byte[] Ring(int diameter)
{
var returnMe = new byte[Rgba.ByteCount(diameter, diameter)];
double radius = diameter / 2.0;
for (int y = 0; y < diameter; y++)
{
for (int x = 0; x < diameter; x++)
{
double dx = x + 0.5 - radius;
double dy = y + 0.5 - radius;
double distance = Math.Sqrt(dx * dx + dy * dy);
Color color = Clear;
if (distance <= radius && distance > radius - 1)
{
color = Dark;
}
else if (distance <= radius - 1 && distance > radius - 2)
{
color = Light;
}
else if (distance <= radius - 2 && distance > radius - 3)
{
color = Dark;
}
Rgba.Write(returnMe, y * diameter + x, color);
}
}
return returnMe;
}
///
/// Returns a white disc inside a dark outline.
///
public static byte[] Dot(int diameter)
{
var returnMe = new byte[Rgba.ByteCount(diameter, diameter)];
double radius = diameter / 2.0;
for (int y = 0; y < diameter; y++)
{
for (int x = 0; x < diameter; x++)
{
double dx = x + 0.5 - radius;
double dy = y + 0.5 - radius;
double distance = Math.Sqrt(dx * dx + dy * dy);
Color color = Clear;
if (distance <= radius - 1)
{
color = Light;
}
else if (distance <= radius)
{
color = Dark;
}
Rgba.Write(returnMe, y * diameter + x, color);
}
}
return returnMe;
}
///
/// Returns a one-pixel-wide line of white between two dark ones, which stretches across a strip.
///
public static byte[] Line()
{
var returnMe = new byte[Rgba.ByteCount(1, 3)];
Rgba.Write(returnMe, 0, Dark);
Rgba.Write(returnMe, 1, Light);
Rgba.Write(returnMe, 2, Dark);
return returnMe;
}
}
}