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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Types used in the library to describe Layers properties
use oklab::{oklab_to_srgb, srgb_to_oklab, Oklab, RGB};
use serde::Serialize;
use skia_safe::Color4f;
use skia_safe::Vector;

pub use skia_safe::{Image, Matrix, Picture, M44, V3};

#[derive(Clone, Copy, Serialize, Debug)]
#[repr(C)]
pub struct Color {
    pub l: f32,
    pub a: f32,
    pub b: f32,
    pub alpha: f32,
}
impl Color {
    pub fn c4f(&self) -> skia::Color4f {
        Color4f::from(*self)
    }
}
#[derive(Clone, Copy, Serialize, Debug)]
#[repr(C)]
pub struct Point {
    pub x: f32,
    pub y: f32,
}

impl Point {
    pub fn new(x: f32, y: f32) -> Self {
        Point { x, y }
    }
}
#[derive(Clone, Copy, Serialize, Debug)]
#[repr(C)]
pub struct Size {
    pub width: taffy::style::Dimension,
    pub height: taffy::style::Dimension,
}
impl From<skia_safe::Size> for Size {
    fn from(size: skia_safe::Size) -> Self {
        Size {
            width: taffy::style::Dimension::Length(size.width),
            height: taffy::style::Dimension::Length(size.height),
        }
    }
}
impl Size {
    pub fn points(width: f32, height: f32) -> Self {
        Size {
            width: taffy::style::Dimension::Length(width),
            height: taffy::style::Dimension::Length(height),
        }
    }
    pub fn percent(width: f32, height: f32) -> Self {
        Size {
            width: taffy::style::Dimension::Percent(width),
            height: taffy::style::Dimension::Percent(height),
        }
    }
    pub fn auto() -> Self {
        Size {
            width: taffy::style::Dimension::Auto,
            height: taffy::style::Dimension::Auto,
        }
    }
}

impl Default for Size {
    fn default() -> Self {
        Size {
            width: taffy::style::Dimension::Auto,
            height: taffy::style::Dimension::Auto,
        }
    }
}
use core::ops::Sub;
impl Sub for &Point {
    type Output = Point;

    fn sub(self, rhs: Self) -> Self::Output {
        Point {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Point3d {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}
#[derive(Clone, Copy, Default, Serialize, Debug)]
#[repr(C)]
pub struct Rectangle {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl From<skia_safe::Rect> for Rectangle {
    fn from(rect: skia_safe::Rect) -> Self {
        Rectangle {
            x: rect.left,
            y: rect.top,
            width: rect.width(),
            height: rect.height(),
        }
    }
}

#[derive(Clone, Serialize, Debug)]
pub struct GradientLinear {
    pub colors: Vec<Color>,
    pub points: Vec<f32>,
}
#[derive(Clone, Serialize, Debug)]
pub struct GradientRadial {
    pub center: Point,
    pub radius: f32,
    pub colors: Vec<Color>,
    pub points: Vec<Point>,
}

#[allow(dead_code)]
#[derive(Clone, Debug, Serialize)]
#[repr(C)]
pub enum PaintColor {
    Solid { color: Color },
    GradientLinear(Box<GradientLinear>),
    GradientRadial(Box<GradientRadial>),
}
impl Default for PaintColor {
    fn default() -> Self {
        PaintColor::Solid {
            color: Color::default(),
        }
    }
}
#[allow(dead_code)]
#[derive(Clone, Copy, Default, Serialize, Debug)]
#[repr(u32)]
pub enum BorderStyle {
    #[default]
    Solid,
    Dotted,
    Dashed,
}

#[derive(Clone, Copy, Debug, diff::Diff, Serialize)]
#[repr(C)]
pub struct BorderRadius {
    pub top_left: f32,
    pub top_right: f32,
    pub bottom_right: f32,
    pub bottom_left: f32,
}

#[allow(clippy::from_over_into)]
impl Into<[Vector; 4]> for BorderRadius {
    fn into(self) -> [Vector; 4] {
        [
            skia_safe::Point::new(self.top_left, self.top_left),
            skia_safe::Point::new(self.top_right, self.top_right),
            skia_safe::Point::new(self.bottom_left, self.bottom_left),
            skia_safe::Point::new(self.bottom_right, self.bottom_right),
        ]
    }
}

impl BorderRadius {
    pub fn new_single(r: f32) -> Self {
        BorderRadius {
            top_left: r,
            top_right: r,
            bottom_left: r,
            bottom_right: r,
        }
    }
    #[allow(dead_code)]
    fn set(mut self, radius: f32) -> Self {
        self.top_left = radius;
        self.top_right = radius;
        self.bottom_right = radius;
        self.bottom_left = radius;
        self
    }
}
impl Default for BorderRadius {
    fn default() -> Self {
        BorderRadius {
            top_left: 0.0,
            top_right: 0.0,
            bottom_left: 0.0,
            bottom_right: 0.0,
        }
    }
}
impl Default for Color {
    fn default() -> Self {
        Color {
            l: 0.0,
            a: 0.0,
            b: 0.0,
            alpha: 0.0,
        }
    }
}

impl Color {
    // Put in the public domain by Björn Ottosson 2020
    pub fn new_rgba(r: f32, g: f32, b: f32, alpha: f32) -> Self {
        let Oklab { l, a, b } = srgb_to_oklab(RGB {
            r: (r * 255.0) as u8,
            g: (g * 255.0) as u8,
            b: (b * 255.0) as u8,
        });

        Color { l, a, b, alpha }
    }

    pub fn new_rgba255(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self::new_rgba(
            r as f32 / 255.0,
            g as f32 / 255.0,
            b as f32 / 255.0,
            a as f32 / 255.0,
        )
    }

    pub fn new_hex(hex: &str) -> Self {
        let hex = hex.trim_start_matches('#');
        let r = u8::from_str_radix(&hex[0..2], 16).unwrap();
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap();
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap();
        let a = if hex.len() == 8 {
            u8::from_str_radix(&hex[6..8], 16).unwrap()
        } else {
            255
        };
        Self::new_rgba255(r, g, b, a)
    }
}

impl Default for Point {
    fn default() -> Self {
        Point { x: 0.0, y: 0.0 }
    }
}

#[derive(Clone, Copy, Default, Serialize, Debug)]
#[repr(u32)]
pub enum BlendMode {
    #[default]
    Normal,
    BackgroundBlur,
}

impl PartialEq for BlendMode {
    fn eq(&self, other: &Self) -> bool {
        core::mem::discriminant(self) == core::mem::discriminant(other)
    }
}

// skia conversions

impl From<Color> for Color4f {
    fn from(color: Color) -> Self {
        let Color { l, a, b, alpha } = color;
        let rgb = oklab_to_srgb(Oklab { l, a, b });

        Self {
            r: (rgb.r as f32 / 255.0),
            g: (rgb.g as f32 / 255.0),
            b: (rgb.b as f32 / 255.0),
            a: alpha,
        }
    }
}

impl From<Point> for skia_safe::Point {
    fn from(point: Point) -> Self {
        skia_safe::Point {
            x: point.x,
            y: point.y,
        }
    }
}
impl From<Point> for (f32, f32) {
    fn from(point: Point) -> (f32, f32) {
        (point.x, point.y)
    }
}

impl From<Point3d> for skia_safe::Point3 {
    fn from(point: Point3d) -> Self {
        skia_safe::Point3 {
            x: point.x,
            y: point.y,
            z: point.z,
        }
    }
}

impl From<(u32, u32)> for Point {
    fn from(point: (u32, u32)) -> Self {
        Point {
            x: point.0 as f32,
            y: point.1 as f32,
        }
    }
}

impl From<(usize, usize)> for Point {
    fn from(point: (usize, usize)) -> Self {
        Point {
            x: point.0 as f32,
            y: point.1 as f32,
        }
    }
}
impl From<(f32, f32)> for Point {
    fn from(point: (f32, f32)) -> Self {
        Point {
            x: point.0,
            y: point.1,
        }
    }
}

impl From<Color> for PaintColor {
    fn from(color: Color) -> Self {
        PaintColor::Solid { color }
    }
}

impl From<f32> for BorderRadius {
    fn from(radius: f32) -> Self {
        BorderRadius::new_single(radius)
    }
}