Skip to content

Commit

Permalink
ns color api
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Dec 23, 2023
1 parent 5d800d5 commit 7dddf89
Showing 1 changed file with 70 additions and 3 deletions.
73 changes: 70 additions & 3 deletions cidre/src/ns/app/color.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,79 @@
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<Self>;

#[objc::cls_rar_retain]
pub fn with_white_alpha(white: cg::Float, alpha: cg::Float) -> arc::R<Self>;

#[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<Self>;

#[objc::cls_rar_retain]
pub fn with_rgba(r: cg::Float, g: cg::Float, b: cg::Float, a: cg::Float) -> arc::R<Self>;

#[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<Self>;

#[objc::cls_rar_retain]
pub fn with_hsba(h: cg::Float, s: cg::Float, b: cg::Float, a: cg::Float) -> arc::R<Self>;

#[objc::msg_send(whiteComponent)]
pub unsafe fn white_throws(&self) -> cg::Float;

pub fn white<'ar>(&self) -> Result<cg::Float, &'ar ns::Exception> {
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<cg::Float, &'ar ns::Exception> {
ns::try_catch(|| unsafe { self.red_throws() })
}

pub fn green<'ar>(&self) -> Result<cg::Float, &'ar ns::Exception> {
ns::try_catch(|| unsafe { self.green_throws() })
}

pub fn blue<'ar>(&self) -> Result<cg::Float, &'ar ns::Exception> {
ns::try_catch(|| unsafe { self.blue_throws() })
}
}

unsafe impl Send for Color {}

#[link(name = "app", kind = "static")]
extern "C" {
static NS_COLOR: &'static objc::Class<Color>;
}

#[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");
}
}

0 comments on commit 7dddf89

Please # to comment.