diff --git a/cidre/src/ns/app/color.rs b/cidre/src/ns/app/color.rs index 3b3bb9c9..cf236f81 100644 --- a/cidre/src/ns/app/color.rs +++ b/cidre/src/ns/app/color.rs @@ -1,8 +1,60 @@ -use crate::{arc, define_obj_type, ns, objc}; +use crate::{arc, cg, define_obj_type, ns, objc}; -define_obj_type!(pub Color(ns::Id), NS_COLOR); +define_obj_type!( + #[doc(alias = "NSColor")] + pub Color(ns::Id), NS_COLOR +); -impl Color {} +impl Color { + #[objc::cls_msg_send(colorWithWhite:alpha:)] + pub fn with_white_alpha_ar(white: cg::Float, alpha: cg::Float) -> arc::Rar; + + #[objc::cls_rar_retain] + pub fn with_white_alpha(white: cg::Float, alpha: cg::Float) -> arc::R; + + #[objc::cls_msg_send(colorWithRed:green:blue:alpha:)] + pub fn with_rgba_ar(r: cg::Float, g: cg::Float, b: cg::Float, a: cg::Float) -> arc::Rar; + + #[objc::cls_rar_retain] + pub fn with_rgba(r: cg::Float, g: cg::Float, b: cg::Float, a: cg::Float) -> arc::R; + + #[objc::cls_msg_send(colorWithHue:saturation:brightness:alpha:)] + pub fn with_hsba_ar(h: cg::Float, s: cg::Float, b: cg::Float, a: cg::Float) -> arc::Rar; + + #[objc::cls_rar_retain] + pub fn with_hsba(h: cg::Float, s: cg::Float, b: cg::Float, a: cg::Float) -> arc::R; + + #[objc::msg_send(whiteComponent)] + pub unsafe fn white_throws(&self) -> cg::Float; + + pub fn white<'ar>(&self) -> Result { + ns::try_catch(|| unsafe { self.white_throws() }) + } + + #[objc::msg_send(alphaComponent)] + pub fn alpha(&self) -> cg::Float; + + #[objc::msg_send(redComponent)] + pub unsafe fn red_throws(&self) -> cg::Float; + + #[objc::msg_send(greenComponent)] + pub unsafe fn green_throws(&self) -> cg::Float; + + #[objc::msg_send(blueComponent)] + pub unsafe fn blue_throws(&self) -> cg::Float; + + pub fn red<'ar>(&self) -> Result { + ns::try_catch(|| unsafe { self.red_throws() }) + } + + pub fn green<'ar>(&self) -> Result { + ns::try_catch(|| unsafe { self.green_throws() }) + } + + pub fn blue<'ar>(&self) -> Result { + ns::try_catch(|| unsafe { self.blue_throws() }) + } +} unsafe impl Send for Color {} @@ -10,3 +62,18 @@ unsafe impl Send for Color {} extern "C" { static NS_COLOR: &'static objc::Class; } + +#[cfg(test)] +mod tests { + use crate::ns; + + #[test] + fn basics() { + let black = ns::Color::with_white_alpha(0.0, 1.0); + assert_eq!(black.white().unwrap(), 0.0); + + assert_eq!(black.alpha(), 1.0); + + black.red().expect_err("should be err"); + } +}