using System;
using System.Drawing;
using Blacklight.Core;
namespace Konis.Testbed
{
///
/// The sprite every particle draws, made in code so the testbed needs no art.
///
public static class SoftDisc
{
public const int Side = 64;
// The highlight sits off center so a particle's rotation can be seen.
private const double HighlightOffset = 0.45;
private const double HighlightRadius = 0.14;
private static double SmoothStep(double edge0, double edge1, double value)
{
double fraction = Math.Max(0.0, Math.Min(1.0, (value - edge0) / (edge1 - edge0)));
return fraction * fraction * (3.0 - 2.0 * fraction);
}
///
/// Returns white that fades from the middle to nothing at the edge, with a brighter spot toward the right.
///
public static DecodedImage Build()
{
var rgba = new byte[Side * Side * DecodedImage.BytesPerPixel];
double middle = (Side - 1) / 2.0;
for (int y = 0; y < Side; y++)
{
for (int x = 0; x < Side; x++)
{
double across = (x - middle) / middle;
double down = (y - middle) / middle;
double fromMiddle = Math.Sqrt(across * across + down * down);
double spotX = across - HighlightOffset;
double fromSpot = Math.Sqrt(spotX * spotX + down * down);
double alpha = 0.7 * (1.0 - SmoothStep(0.0, 1.0, fromMiddle));
alpha = Math.Max(alpha, 1.0 - SmoothStep(0.0, HighlightRadius, fromSpot));
int index = (y * Side + x) * DecodedImage.BytesPerPixel;
rgba[index + 0] = 255;
rgba[index + 1] = 255;
rgba[index + 2] = 255;
rgba[index + 3] = (byte)(alpha * 255.0 + 0.5);
}
}
return new DecodedImage(rgba, new Size(Side, Side));
}
}
}