core_graphics/
gradient.rs

1// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10#![allow(non_upper_case_globals)]
11
12use crate::base::CGFloat;
13use crate::color::CGColor;
14use crate::color_space::CGColorSpace;
15
16use bitflags::bitflags;
17use core_foundation::array::{CFArray, CFArrayRef};
18use core_foundation::base::{CFRelease, CFRetain, TCFType};
19use foreign_types::{foreign_type, ForeignType};
20
21use libc::size_t;
22
23bitflags! {
24    #[repr(C)]
25    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
26    pub struct CGGradientDrawingOptions: u32 {
27        const CGGradientDrawsBeforeStartLocation = (1 << 0);
28        const CGGradientDrawsAfterEndLocation = (1 << 1);
29    }
30}
31
32foreign_type! {
33    #[doc(hidden)]
34    pub unsafe type CGGradient {
35        type CType = crate::sys::CGGradient;
36        fn drop = |p| CFRelease(p as *mut _);
37        fn clone = |p| CFRetain(p as *const _) as *mut _;
38    }
39}
40
41impl CGGradient {
42    pub fn create_with_color_components(
43        color_space: &CGColorSpace,
44        components: &[CGFloat],
45        locations: &[CGFloat],
46        count: usize,
47    ) -> CGGradient {
48        unsafe {
49            let result = CGGradientCreateWithColorComponents(
50                color_space.as_ptr(),
51                components.as_ptr(),
52                locations.as_ptr(),
53                count,
54            );
55            assert!(!result.is_null());
56            Self::from_ptr(result)
57        }
58    }
59
60    pub fn create_with_colors(
61        color_space: &CGColorSpace,
62        colors: &CFArray<CGColor>,
63        locations: &[CGFloat],
64    ) -> CGGradient {
65        unsafe {
66            let result = CGGradientCreateWithColors(
67                color_space.as_ptr(),
68                colors.as_concrete_TypeRef(),
69                locations.as_ptr(),
70            );
71            assert!(!result.is_null());
72            Self::from_ptr(result)
73        }
74    }
75}
76
77#[cfg_attr(feature = "link", link(name = "CoreGraphics", kind = "framework"))]
78extern "C" {
79    fn CGGradientCreateWithColorComponents(
80        color_space: crate::sys::CGColorSpaceRef,
81        components: *const CGFloat,
82        locations: *const CGFloat,
83        count: size_t,
84    ) -> crate::sys::CGGradientRef;
85    fn CGGradientCreateWithColors(
86        color_space: crate::sys::CGColorSpaceRef,
87        colors: CFArrayRef,
88        locations: *const CGFloat,
89    ) -> crate::sys::CGGradientRef;
90}