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
21bitflags! {
22    #[repr(C)]
23    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
24    pub struct CGGradientDrawingOptions: u32 {
25        const CGGradientDrawsBeforeStartLocation = (1 << 0);
26        const CGGradientDrawsAfterEndLocation = (1 << 1);
27    }
28}
29
30foreign_type! {
31    #[doc(hidden)]
32    pub unsafe type CGGradient {
33        type CType = crate::sys::CGGradient;
34        fn drop = |p| CFRelease(p as *mut _);
35        fn clone = |p| CFRetain(p as *const _) as *mut _;
36    }
37}
38
39impl CGGradient {
40    pub fn create_with_color_components(
41        color_space: &CGColorSpace,
42        components: &[CGFloat],
43        locations: &[CGFloat],
44        count: usize,
45    ) -> CGGradient {
46        unsafe {
47            let result = CGGradientCreateWithColorComponents(
48                color_space.as_ptr(),
49                components.as_ptr(),
50                locations.as_ptr(),
51                count,
52            );
53            assert!(!result.is_null());
54            Self::from_ptr(result)
55        }
56    }
57
58    pub fn create_with_colors(
59        color_space: &CGColorSpace,
60        colors: &CFArray<CGColor>,
61        locations: &[CGFloat],
62    ) -> CGGradient {
63        unsafe {
64            let result = CGGradientCreateWithColors(
65                color_space.as_ptr(),
66                colors.as_concrete_TypeRef(),
67                locations.as_ptr(),
68            );
69            assert!(!result.is_null());
70            Self::from_ptr(result)
71        }
72    }
73}
74
75#[cfg_attr(feature = "link", link(name = "CoreGraphics", kind = "framework"))]
76extern "C" {
77    fn CGGradientCreateWithColorComponents(
78        color_space: crate::sys::CGColorSpaceRef,
79        components: *const CGFloat,
80        locations: *const CGFloat,
81        count: usize,
82    ) -> crate::sys::CGGradientRef;
83    fn CGGradientCreateWithColors(
84        color_space: crate::sys::CGColorSpaceRef,
85        colors: CFArrayRef,
86        locations: *const CGFloat,
87    ) -> crate::sys::CGGradientRef;
88}