summaryrefslogtreecommitdiffstats
path: root/font/src/darwin/cg_color.rs
blob: 79ea08634ee06108a7725fec309450978198a98e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType};
use core_graphics::color_space::{CGColorSpace, CGColorSpaceRef};
use core_graphics::base::CGFloat;
use std::mem;

#[repr(C)]
pub struct __CGColor;

pub type CGColorRef = *const __CGColor;

pub struct CGColor {
    obj: CGColorRef,
}

impl Drop for CGColor {
    fn drop(&mut self) {
        unsafe {
            CFRelease(self.as_CFTypeRef())
        }
    }
}

impl Clone for CGColor {
    fn clone(&self) -> CGColor {
        unsafe {
            TCFType::wrap_under_get_rule(self.as_concrete_TypeRef())
        }
    }
}

impl TCFType<CGColorRef> for CGColor {
    #[inline]
    fn as_concrete_TypeRef(&self) -> CGColorRef {
        self.obj
    }

    #[inline]
    unsafe fn wrap_under_get_rule(reference: CGColorRef) -> CGColor {
        let reference: CGColorRef = mem::transmute(CFRetain(mem::transmute(reference)));
        TCFType::wrap_under_create_rule(reference)
    }

    #[inline]
    fn as_CFTypeRef(&self) -> CFTypeRef {
        unsafe {
            mem::transmute(self.as_concrete_TypeRef())
        }
    }

    #[inline]
    unsafe fn wrap_under_create_rule(obj: CGColorRef) -> CGColor {
        CGColor {
            obj: obj,
        }
    }

    #[inline]
    fn type_id() -> CFTypeID {
        unsafe {
            CGColorGetTypeID()
        }
    }
}

impl CGColor {
    pub fn new(color_space: CGColorSpace, values: [CGFloat; 4]) -> CGColor {
        unsafe {
            let result = CGColorCreate(color_space.as_concrete_TypeRef(), values.as_ptr());
            TCFType::wrap_under_create_rule(result)
        }
    }
}

#[link(name = "ApplicationServices", kind = "framework")]
extern {
    fn CGColorCreate(space: CGColorSpaceRef, vals: *const CGFloat) -> CGColorRef;
    fn CGColorGetTypeID() -> CFTypeID;
}