use core_graphics::geometry::{CGPoint, CGRect, CGSize};
use core_graphics::sys::CGContextRef;
use crate::types::Color;
extern "C" {
pub(crate) fn CGContextSaveGState(c: CGContextRef);
pub(crate) fn CGContextRestoreGState(c: CGContextRef);
pub(crate) fn CGContextClipToRect(c: CGContextRef, rect: CGRect);
pub(crate) fn CGContextSetRGBFillColor(
c: CGContextRef,
red: core_graphics::base::CGFloat,
green: core_graphics::base::CGFloat,
blue: core_graphics::base::CGFloat,
alpha: core_graphics::base::CGFloat,
);
pub(crate) fn CGContextFillRect(c: CGContextRef, rect: CGRect);
}
pub(crate) fn rect(x: f64, y: f64, w: f64, h: f64) -> CGRect {
CGRect::new(&CGPoint::new(x, y), &CGSize::new(w, h))
}
pub(crate) fn color_to_cg(c: Color) -> (f64, f64, f64, f64) {
(
c.r as f64 / 255.0,
c.g as f64 / 255.0,
c.b as f64 / 255.0,
c.a as f64 / 255.0,
)
}
pub(crate) unsafe fn fill_rect(ctx: CGContextRef, x: f64, y: f64, w: f64, h: f64, c: Color) {
let (r, g, b, a) = color_to_cg(c);
CGContextSetRGBFillColor(ctx, r, g, b, a);
CGContextFillRect(ctx, rect(x, y, w, h));
}
pub(crate) unsafe fn fill_rect_alpha(
ctx: CGContextRef,
x: f64,
y: f64,
w: f64,
h: f64,
c: Color,
alpha: f64,
) {
if w <= 0.0 || h <= 0.0 {
return;
}
let (r, g, b, _) = color_to_cg(c);
CGContextSetRGBFillColor(ctx, r, g, b, alpha);
CGContextFillRect(ctx, rect(x, y, w, h));
}