// Paint styles, gradients, blend modes, and image filters for laplaceliu/tycho_2d.
///|
#derive(Eq, Debug)
pub(all) enum PaintStyle {
Fill
Stroke
StrokeAndFill
}
///|
#derive(Eq, Debug)
pub(all) enum StrokeCap {
Butt
Round
Square
}
///|
#derive(Eq, Debug)
pub(all) enum StrokeJoin {
Miter
Round
Bevel
}
///|
#derive(Eq, Debug)
pub(all) enum TileMode {
Clamp
Repeat
Mirror
Decal
}
///|
#derive(Eq, Debug)
pub(all) enum BlendMode2D {
SrcOver
Src
Dst
DstOver
SrcIn
DstIn
SrcOut
DstOut
SrcAtop
DstAtop
Xor
Plus
Multiply
Screen
Darken
Lighten
Clear
}
///|
pub fn BlendMode2D::to_rhi(self : BlendMode2D) -> @rhi.BlendMode {
match self {
SrcOver => @rhi.BlendMode::Premultiplied
Src => @rhi.BlendMode::Opaque
Dst => @rhi.BlendMode::Opaque
DstOver => @rhi.BlendMode::Premultiplied
SrcIn => @rhi.BlendMode::SrcIn
DstIn => @rhi.BlendMode::DstIn
SrcOut => @rhi.BlendMode::Premultiplied
DstOut => @rhi.BlendMode::Premultiplied
SrcAtop => @rhi.BlendMode::Premultiplied
DstAtop => @rhi.BlendMode::Premultiplied
Xor => @rhi.BlendMode::Premultiplied
Plus => @rhi.BlendMode::Additive
Multiply => @rhi.BlendMode::Multiply
Screen => @rhi.BlendMode::Additive
Darken => @rhi.BlendMode::Opaque
Lighten => @rhi.BlendMode::Opaque
Clear => @rhi.BlendMode::Clear
}
}
///|
// Explicit blend equations for modes the preset `BlendMode` values do not
// cover. `None` means the preset is exact.
pub fn BlendMode2D::to_blend_state(self : BlendMode2D) -> @rhi.BlendState? {
let one = @rhi.BlendFactor::One
let zero = @rhi.BlendFactor::Zero
let sa = @rhi.BlendFactor::SrcAlpha
let osa = @rhi.BlendFactor::OneMinusSrcAlpha
let da = @rhi.BlendFactor::DstAlpha
let oda = @rhi.BlendFactor::OneMinusDstAlpha
match self {
SrcOver => None
Src => None
Dst => Some(@rhi.BlendState::new(zero, one, zero, one))
DstOver => Some(@rhi.BlendState::new(oda, one, oda, one))
SrcIn => None
DstIn => None
SrcOut => Some(@rhi.BlendState::new(oda, zero, oda, zero))
DstOut => Some(@rhi.BlendState::new(zero, osa, zero, osa))
SrcAtop => Some(@rhi.BlendState::new(da, osa, da, osa))
DstAtop => Some(@rhi.BlendState::new(oda, sa, oda, sa))
Xor => Some(@rhi.BlendState::new(oda, osa, oda, osa))
Plus => Some(@rhi.BlendState::new(one, one, one, one))
Multiply => None
Screen => Some(
@rhi.BlendState::new(
@rhi.BlendFactor::OneMinusDstColor,
one,
one,
osa,
),
)
Darken => Some(
@rhi.BlendState::new(one, one, one, osa, color_op=@rhi.BlendOp::Min),
)
Lighten => Some(
@rhi.BlendState::new(one, one, one, osa, color_op=@rhi.BlendOp::Max),
)
Clear => None
}
}
///|
#derive(Eq, Debug)
pub(all) struct GradientStop {
offset : Float
color : Color
}
///|
pub fn GradientStop::new(offset : Float, color : Color) -> GradientStop {
{ offset: @tmath.clamp_f(offset, (0.0 : Float), (1.0 : Float)), color }
}
///|
// Monotonic id source for Image2D handles. Ids are only used as renderer cache
// keys, so allocating them internally removes a whole class of user-side
// collisions (two components both picking `id = 1`).
let g_next_image_id : Ref[Int] = Ref(1)
///|
fn alloc_image_id() -> Int {
let id = g_next_image_id.val
g_next_image_id.val = id + 1
id
}
// Image2D handle wraps a GPU texture (or queued pixels uploaded via Renderer2D).
pub(all) struct Image2D {
priv id : Int
width : Int
height : Int
pixels : Bytes
texture : @rhi.Texture?
// Pixel encoding hints. `pixels` is straight-alpha RGBA8 by default;
// `premultiplied` marks data that is already premultiplied. `srgb` selects
// an sRGB texture format when the image is promoted out of the atlas.
srgb : Bool
premultiplied : Bool
}
///|
/// Renderer-side cache key. Unique per handle, allocated on construction.
pub fn Image2D::id(self : Image2D) -> Int {
self.id
}
///|
fn is_srgb_texture_format(f : @rhi.TextureFormat) -> Bool {
match f {
@rhi.TextureFormat::RGBA8Srgb => true
@rhi.TextureFormat::BGRA8Srgb => true
_ => false
}
}
///|
pub fn Image2D::from_bytes(
width : Int,
height : Int,
pixels : Bytes,
srgb? : Bool = false,
premultiplied? : Bool = false,
) -> Image2D {
{
id: alloc_image_id(),
width,
height,
pixels,
texture: None,
srgb,
premultiplied,
}
}
///|
pub fn Image2D::from_texture(texture : @rhi.Texture) -> Image2D {
{
id: alloc_image_id(),
width: texture.width(),
height: texture.height(),
pixels: b"",
texture: Some(texture),
srgb: is_srgb_texture_format(texture.format()),
premultiplied: true,
}
}
///|
pub(all) enum Shader2D {
Solid(Color)
LinearGradient(Point, Point, Array[GradientStop], TileMode)
RadialGradient(Point, Float, Array[GradientStop], TileMode)
SweepGradient(Point, Float, Float, Array[GradientStop], TileMode)
ImagePattern(Image2D, Matrix3, @rhi.Filter, TileMode, TileMode)
}
///|
pub(all) enum ImageFilter {
Blur(Float, Float, TileMode)
DropShadow(Float, Float, Float, Float, Color)
ColorMatrix(FixedArray[Float])
}
///|
// Paint-level color filter, independent of ImageFilter. `Matrix` is a 4x5
// row-major color matrix; `Blend` is reserved for a future shader path.
pub(all) enum ColorFilter {
Matrix(FixedArray[Float])
Blend(Color, BlendMode2D)
}
///|
pub(all) enum MaskFilter {
Blur(Float, Float)
}
///|
pub(all) struct Paint {
color : Color
style : PaintStyle
stroke_width : Float
stroke_cap : StrokeCap
stroke_join : StrokeJoin
stroke_miter : Float
blend_mode : BlendMode2D
shader : Shader2D?
image_filter : ImageFilter?
color_filter : ColorFilter?
mask_filter : MaskFilter?
anti_alias : Bool
}
///|
pub fn Paint::new(
color? : Color = Color::white(),
style? : PaintStyle = PaintStyle::Fill,
stroke_width? : Float = (1.0 : Float),
stroke_cap? : StrokeCap = StrokeCap::Butt,
stroke_join? : StrokeJoin = StrokeJoin::Miter,
stroke_miter? : Float = (4.0 : Float),
blend_mode? : BlendMode2D = BlendMode2D::SrcOver,
shader? : Shader2D,
image_filter? : ImageFilter,
color_filter? : ColorFilter,
mask_filter? : MaskFilter,
anti_alias? : Bool = true,
) -> Paint {
{
color,
style,
stroke_width,
stroke_cap,
stroke_join,
stroke_miter,
blend_mode,
shader,
image_filter,
color_filter,
mask_filter,
anti_alias,
}
}
///|
// A drop shadow described by named fields instead of four anonymous floats.
// `blur` is the gaussian sigma, `spread` grows (or shrinks, when negative) the
// shadow shape before blurring.
pub(all) struct BoxShadow {
dx : Float
dy : Float
blur : Float
spread : Float
color : Color
}
///|
pub fn BoxShadow::new(
color : Color,
dx? : Float = (0.0 : Float),
dy? : Float = (4.0 : Float),
blur? : Float = (8.0 : Float),
spread? : Float = (0.0 : Float),
) -> BoxShadow {
{ dx, dy, blur, spread, color }
}
///|
pub fn Paint::fill(color : Color) -> Paint {
Paint::new(color~)
}
///|
pub fn Paint::stroke(
color : Color,
width : Float,
cap? : StrokeCap = StrokeCap::Butt,
join? : StrokeJoin = StrokeJoin::Miter,
) -> Paint {
Paint::new(
color~,
style=PaintStyle::Stroke,
stroke_width=width,
stroke_cap=cap,
stroke_join=join,
)
}