-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |