using SkiaSharp;
namespace Sandbox;
public partial class Bitmap
{
private SKPaint _pen;
SKPaint GetPen()
{
if ( _pen is not null ) return _pen;
// defaults
_pen = new SKPaint();
_pen.IsAntialias = true;
return _pen;
}
///
/// Sets the pen for drawing with a solid color and stroke style.
///
public void SetAntialias( bool on )
{
var pen = GetPen();
pen.IsAntialias = on;
}
///
/// Sets the pen to use a specific blend mode.
///
/// The blend mode to apply.
public void SetBlendMode( BlendMode blendMode )
{
var pen = GetPen();
// pen.BlendMode = blendMode;
// TODO
}
///
/// Sets the pen for drawing with a solid color and stroke style.
///
/// The color of the pen.
/// The width of the pen in pixels.
public void SetPen( Color color, float width )
{
var pen = GetPen();
pen.Color = color.ToSk();
pen.ColorF = color.ToSkF();
pen.StrokeWidth = width;
pen.Style = SKPaintStyle.Stroke;
pen.Shader = default;
}
///
/// Sets the pen for drawing dashed or dotted lines.
///
/// The color of the pen.
/// The width of the pen in pixels.
/// An array defining the dash pattern (e.g., [10, 5] for 10px dash, 5px gap).
public void SetDashedPen( Color color, float width, float[] dashPattern )
{
var pen = GetPen();
pen.Color = color.ToSk();
pen.ColorF = color.ToSkF();
pen.StrokeWidth = width;
pen.Style = SKPaintStyle.Stroke;
pen.PathEffect = SKPathEffect.CreateDash( dashPattern, 0 );
pen.Shader = default;
}
///
/// Sets the pen for drawing filled shapes with a solid color.
///
/// The color to fill the shapes with.
public void SetFill( Color color )
{
var pen = GetPen();
pen.Color = color.ToSk();
pen.ColorF = color.ToSkF();
pen.Style = SKPaintStyle.Fill;
pen.StrokeWidth = default;
pen.Shader = default;
}
static readonly float[] gradientPoints = Enumerable.Range( 0, 64 ).Select( x => x / 64.0f ).ToArray();
///
/// Sets the pen for drawing with a linear gradient.
///
/// the gradient's start point.
/// the gradient's end point.
/// The color of the gradient.
public void SetLinearGradient( Vector2 start, Vector2 end, Gradient gradient )
{
var pen = GetPen();
pen.StrokeWidth = default;
pen.Shader = SKShader.CreateLinearGradient(
new SKPoint( start.x, start.y ),
new SKPoint( end.x, end.y ),
gradientPoints.Select( x => gradient.Evaluate( x ).ToSkF() ).ToArray(),
SKColorSpace.CreateSrgbLinear(),
gradientPoints,
SKShaderTileMode.Clamp
);
pen.Style = SKPaintStyle.Fill; // Gradients typically fill shapes
}
///
/// Sets the pen for drawing with a radial gradient.
///
/// The gradient's center.
/// The radius of the gradient.
/// The color of the gradient.
public void SetRadialGradient( Vector2 center, float radius, Gradient gradient )
{
var pen = GetPen();
pen.StrokeWidth = default;
pen.Shader = SKShader.CreateRadialGradient(
new SKPoint( center.x, center.y ),
radius,
gradientPoints.Select( x => gradient.Evaluate( x ).ToSkF() ).ToArray(),
SKColorSpace.CreateSrgbLinear(),
gradientPoints,
SKShaderTileMode.Clamp
);
pen.IsAntialias = true;
pen.Style = SKPaintStyle.Fill; // Gradients typically fill shapes
}
}