use crate::nvg::color::Color;
use crate::nvg::context::NvgContext;
use crate::sys;
pub trait FillStyle {
fn apply_fill(&self, ctx: &NvgContext);
fn apply_stroke(&self, ctx: &NvgContext);
}
impl FillStyle for Color {
#[inline]
fn apply_fill(&self, ctx: &NvgContext) {
ctx.fill_color(*self);
}
#[inline]
fn apply_stroke(&self, ctx: &NvgContext) {
ctx.stroke_color(*self);
}
}
#[derive(Clone, Copy)]
pub struct Gradient {
pub(crate) raw: sys::NVGpaint,
}
impl Gradient {
pub fn linear(
ctx: &NvgContext,
sx: f32,
sy: f32,
ex: f32,
ey: f32,
inner: Color,
outer: Color,
) -> Self {
let raw = unsafe {
sys::nvgLinearGradient(
ctx.raw(),
sx,
sy,
ex,
ey,
inner.into_raw(),
outer.into_raw(),
)
};
Self { raw }
}
pub fn radial(
ctx: &NvgContext,
cx: f32,
cy: f32,
inner_radius: f32,
outer_radius: f32,
inner: Color,
outer: Color,
) -> Self {
let raw = unsafe {
sys::nvgRadialGradient(
ctx.raw(),
cx,
cy,
inner_radius,
outer_radius,
inner.into_raw(),
outer.into_raw(),
)
};
Self { raw }
}
pub fn box_(
ctx: &NvgContext,
x: f32,
y: f32,
w: f32,
h: f32,
radius: f32,
feather: f32,
inner: Color,
outer: Color,
) -> Self {
let raw = unsafe {
sys::nvgBoxGradient(
ctx.raw(),
x,
y,
w,
h,
radius,
feather,
inner.into_raw(),
outer.into_raw(),
)
};
Self { raw }
}
}
impl FillStyle for Gradient {
#[inline]
fn apply_fill(&self, ctx: &NvgContext) {
unsafe { sys::nvgFillPaint(ctx.raw(), self.raw) };
}
#[inline]
fn apply_stroke(&self, ctx: &NvgContext) {
unsafe { sys::nvgStrokePaint(ctx.raw(), self.raw) };
}
}
#[derive(Clone, Copy)]
pub struct ImagePattern {
pub(crate) raw: sys::NVGpaint,
}
impl ImagePattern {
pub fn new(
ctx: &NvgContext,
ox: f32,
oy: f32,
ex: f32,
ey: f32,
angle: f32,
image: i32,
alpha: f32,
) -> Self {
let raw = unsafe { sys::nvgImagePattern(ctx.raw(), ox, oy, ex, ey, angle, image, alpha) };
Self { raw }
}
}
impl FillStyle for ImagePattern {
#[inline]
fn apply_fill(&self, ctx: &NvgContext) {
unsafe { sys::nvgFillPaint(ctx.raw(), self.raw) };
}
#[inline]
fn apply_stroke(&self, ctx: &NvgContext) {
unsafe { sys::nvgStrokePaint(ctx.raw(), self.raw) };
}
}