1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 //! Rust helpers to interact with Gecko's StyleComplexColor.
6 
7 use gecko::values::{convert_nscolor_to_rgba, convert_rgba_to_nscolor};
8 use gecko_bindings::structs::{nscolor, StyleComplexColor};
9 use values::computed::Color as ComputedColor;
10 use values::generics::pointing::CaretColor;
11 
12 impl From<nscolor> for StyleComplexColor {
from(other: nscolor) -> Self13     fn from(other: nscolor) -> Self {
14         StyleComplexColor {
15             mColor: other,
16             mForegroundRatio: 0,
17             mIsAuto: false,
18         }
19     }
20 }
21 
22 impl StyleComplexColor {
23     /// Create a `StyleComplexColor` value that represents `currentColor`.
current_color() -> Self24     pub fn current_color() -> Self {
25         StyleComplexColor {
26             mColor: 0,
27             mForegroundRatio: 255,
28             mIsAuto: false,
29         }
30     }
31 
32     /// Create a `StyleComplexColor` value that represents `auto`.
auto() -> Self33     pub fn auto() -> Self {
34         StyleComplexColor {
35             mColor: 0,
36             mForegroundRatio: 255,
37             mIsAuto: true,
38         }
39     }
40 }
41 
42 impl From<ComputedColor> for StyleComplexColor {
from(other: ComputedColor) -> Self43     fn from(other: ComputedColor) -> Self {
44         StyleComplexColor {
45             mColor: convert_rgba_to_nscolor(&other.color).into(),
46             mForegroundRatio: other.foreground_ratio,
47             mIsAuto: false,
48         }
49     }
50 }
51 
52 impl From<StyleComplexColor> for ComputedColor {
from(other: StyleComplexColor) -> Self53     fn from(other: StyleComplexColor) -> Self {
54         debug_assert!(!other.mIsAuto);
55         ComputedColor {
56             color: convert_nscolor_to_rgba(other.mColor),
57             foreground_ratio: other.mForegroundRatio,
58         }
59     }
60 }
61 
62 impl<Color> From<CaretColor<Color>> for StyleComplexColor
63 where
64     Color: Into<StyleComplexColor>,
65 {
from(other: CaretColor<Color>) -> Self66     fn from(other: CaretColor<Color>) -> Self {
67         match other {
68             CaretColor::Color(color) => color.into(),
69             CaretColor::Auto => StyleComplexColor::auto(),
70         }
71     }
72 }
73 
74 impl<Color> From<StyleComplexColor> for CaretColor<Color>
75 where
76     StyleComplexColor: Into<Color>,
77 {
from(other: StyleComplexColor) -> Self78     fn from(other: StyleComplexColor) -> Self {
79         if !other.mIsAuto {
80             CaretColor::Color(other.into())
81         } else {
82             CaretColor::Auto
83         }
84     }
85 }
86