forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrush.rs
262 lines (227 loc) · 7.42 KB
/
brush.rs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
use pyo3::prelude::*;
use pyo3_stub_gen::{derive::gen_stub_pyclass, derive::gen_stub_pymethods, impl_stub_type};
use crate::errors::PyColorParseError;
#[gen_stub_pyclass]
#[pyclass]
#[derive(FromPyObject)]
struct RgbaColor {
#[pyo3(get, set)]
red: u8,
#[pyo3(get, set)]
green: u8,
#[pyo3(get, set)]
blue: u8,
#[pyo3(get, set)]
alpha: u8,
}
#[gen_stub_pyclass]
#[pyclass]
#[derive(FromPyObject)]
struct RgbColor {
#[pyo3(get, set)]
red: u8,
#[pyo3(get, set)]
green: u8,
#[pyo3(get, set)]
blue: u8,
}
#[derive(FromPyObject)]
#[pyclass]
enum PyColorInput {
ColorStr(String),
// This variant must come before RgbColor
RgbaColor {
#[pyo3(item)]
red: u8,
#[pyo3(item)]
green: u8,
#[pyo3(item)]
blue: u8,
#[pyo3(item)]
alpha: u8,
},
RgbColor {
#[pyo3(item)]
red: u8,
#[pyo3(item)]
green: u8,
#[pyo3(item)]
blue: u8,
},
}
impl_stub_type!(PyColorInput = String | RgbaColor | RgbColor);
/// A Color object represents a color in the RGB color space with an alpha. Each color channel as well as the alpha is represented as an 8-bit
/// integer. The alpha channel is 0 for fully transparent and 255 for fully opaque.
///
/// Construct colors from either a CSS color string, or by specifying the red, green, blue, and (optional) alpha channels in a dict.
#[gen_stub_pyclass]
#[pyclass(name = "Color")]
#[derive(Clone)]
pub struct PyColor {
pub color: slint_interpreter::Color,
}
#[gen_stub_pymethods]
#[pymethods]
impl PyColor {
#[new]
#[pyo3(signature = (maybe_value=None))]
fn py_new(maybe_value: Option<PyColorInput>) -> PyResult<Self> {
let Some(value) = maybe_value else {
return Ok(Self { color: Default::default() });
};
match value {
PyColorInput::ColorStr(color_str) => color_str
.parse::<css_color_parser2::Color>()
.map(|c| Self {
color: slint_interpreter::Color::from_argb_u8(
(c.a * 255.) as u8,
c.r,
c.g,
c.b,
),
})
.map_err(|color_err| PyColorParseError(color_err).into()),
PyColorInput::RgbaColor { red, green, blue, alpha } => {
Ok(Self { color: slint_interpreter::Color::from_argb_u8(alpha, red, green, blue) })
}
PyColorInput::RgbColor { red, green, blue } => {
Ok(Self { color: slint_interpreter::Color::from_rgb_u8(red, green, blue) })
}
}
}
/// The red channel.
#[getter]
fn red(&self) -> u8 {
self.color.red()
}
/// The green channel.
#[getter]
fn green(&self) -> u8 {
self.color.green()
}
/// The blue channel.
#[getter]
fn blue(&self) -> u8 {
self.color.blue()
}
/// The alpha channel.
#[getter]
fn alpha(&self) -> u8 {
self.color.alpha()
}
/// Returns a new color that is brighter than this color by the given factor.
fn brighter(&self, factor: f32) -> Self {
Self { color: self.color.brighter(factor) }
}
/// Returns a new color that is darker than this color by the given factor.
fn darker(&self, factor: f32) -> Self {
Self { color: self.color.darker(factor) }
}
/// Returns a new version of this color with the opacity decreased by `factor`.
///
/// The transparency is obtained by multiplying the alpha channel by `(1 - factor)`.
fn transparentize(&self, factor: f32) -> Self {
Self { color: self.color.transparentize(factor) }
}
/// Returns a new color that is a mix of this color and `other`. The specified factor is
/// clamped to be between `0.0` and `1.0` and then applied to this color, while `1.0 - factor`
/// is applied to `other`.
fn mix(&self, other: &Self, factor: f32) -> Self {
Self { color: self.color.mix(&other.color, factor) }
}
/// Returns a new version of this color with the opacity set to `alpha`.
fn with_alpha(&self, alpha: f32) -> Self {
Self { color: self.color.with_alpha(alpha) }
}
fn __str__(&self) -> String {
self.color.to_string()
}
fn __eq__(&self, other: &Self) -> bool {
self.color == other.color
}
}
impl From<slint_interpreter::Color> for PyColor {
fn from(color: slint_interpreter::Color) -> Self {
Self { color }
}
}
#[derive(FromPyObject)]
#[pyclass]
enum PyBrushInput {
SolidColor(PyColor),
}
impl_stub_type!(PyBrushInput = PyColor);
/// A brush is a data structure that is used to describe how
/// a shape, such as a rectangle, path or even text, shall be filled.
/// A brush can also be applied to the outline of a shape, that means
/// the fill of the outline itself.
///
/// Brushes can only be constructed from solid colors. This is a restriction we anticipate to lift in the future,
/// to programmatically also declare gradients.
#[gen_stub_pyclass]
#[pyclass(name = "Brush")]
pub struct PyBrush {
pub brush: slint_interpreter::Brush,
}
#[gen_stub_pymethods]
#[pymethods]
impl PyBrush {
#[new]
#[pyo3(signature = (maybe_value=None))]
fn py_new(maybe_value: Option<PyBrushInput>) -> PyResult<Self> {
let Some(value) = maybe_value else {
return Ok(Self { brush: Default::default() });
};
match value {
PyBrushInput::SolidColor(pycol) => Ok(Self { brush: pycol.color.into() }),
}
}
/// The brush's color.
#[getter]
fn color(&self) -> PyColor {
self.brush.color().into()
}
/// Returns true if this brush contains a fully transparent color (alpha value is zero).
fn is_transparent(&self) -> bool {
self.brush.is_transparent()
}
/// Returns true if this brush is fully opaque.
fn is_opaque(&self) -> bool {
self.brush.is_opaque()
}
/// Returns a new version of this brush that has the brightness increased
/// by the specified factor. This is done by calling `Color.brighter` on
/// all the colors of this brush.
fn brighter(&self, factor: f32) -> Self {
Self { brush: self.brush.brighter(factor) }
}
/// Returns a new version of this brush that has the brightness decreased
/// by the specified factor. This is done by calling `Color.darker` on
/// all the color of this brush.
fn darker(&self, factor: f32) -> Self {
Self { brush: self.brush.darker(factor) }
}
/// Returns a new version of this brush with the opacity decreased by `factor`.
///
/// The transparency is obtained by multiplying the alpha channel by `(1 - factor)`.
///
/// See also `Color.transparentize`.
fn transparentize(&self, amount: f32) -> Self {
Self { brush: self.brush.transparentize(amount) }
}
/// Returns a new version of this brush with the related color's opacities
/// set to `alpha`.
fn with_alpha(&self, alpha: f32) -> Self {
Self { brush: self.brush.with_alpha(alpha) }
}
fn __eq__(&self, other: &Self) -> bool {
self.brush == other.brush
}
}
impl From<slint_interpreter::Brush> for PyBrush {
fn from(brush: slint_interpreter::Brush) -> Self {
Self { brush }
}
}