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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use std::sync::Arc;

use ilmenite::{ImtHoriAlign, ImtTextWrap, ImtVertAlign};

use crate::atlas::{AtlasCacheCtrl, AtlasCoords};
use crate::image_view::BstImageView;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BinPosition {
    /// Position will be done from the window's dimensions
    Window,
    /// Position will be done from the parent's dimensions
    Parent,
    /// Position will be done from the parent's dimensions
    /// and other siblings the same type.
    Floating,
}

impl Default for BinPosition {
    fn default() -> Self {
        BinPosition::Window
    }
}

#[derive(Default, Clone)]
pub struct BinStyle {
    /// Determines the positioning type
    pub position: Option<BinPosition>,
    /// Overrides the z-index automatically calculated.
    pub z_index: Option<i16>,
    /// Offsets the z-index automatically calculated.
    pub add_z_index: Option<i16>,
    /// Hides the bin, with None set parent will decide
    /// the visiblity, setting this explictely will ignore
    /// the parents visiblity.
    pub hidden: Option<bool>,
    /// Set the opacity of the bin's content.
    pub opacity: Option<f32>,
    // Position from Edges
    pub pos_from_t: Option<f32>,
    pub pos_from_b: Option<f32>,
    pub pos_from_l: Option<f32>,
    pub pos_from_r: Option<f32>,
    pub pos_from_t_pct: Option<f32>,
    pub pos_from_b_pct: Option<f32>,
    pub pos_from_l_pct: Option<f32>,
    pub pos_from_r_pct: Option<f32>,
    pub pos_from_l_offset: Option<f32>,
    pub pos_from_t_offset: Option<f32>,
    pub pos_from_r_offset: Option<f32>,
    pub pos_from_b_offset: Option<f32>,
    // Size
    pub width: Option<f32>,
    pub width_pct: Option<f32>,
    /// Used in conjunction with `width_pct` to provide additional flexibility
    pub width_offset: Option<f32>,
    pub height: Option<f32>,
    pub height_pct: Option<f32>,
    /// Used in conjunction with `height_pct` to provide additional flexibility
    pub height_offset: Option<f32>,
    pub margin_t: Option<f32>,
    pub margin_b: Option<f32>,
    pub margin_l: Option<f32>,
    pub margin_r: Option<f32>,
    // Padding
    pub pad_t: Option<f32>,
    pub pad_b: Option<f32>,
    pub pad_l: Option<f32>,
    pub pad_r: Option<f32>,
    // Scrolling
    pub scroll_y: Option<f32>,
    pub scroll_x: Option<f32>,
    pub overflow_y: Option<bool>,
    pub overflow_x: Option<bool>,
    // Border
    pub border_size_t: Option<f32>,
    pub border_size_b: Option<f32>,
    pub border_size_l: Option<f32>,
    pub border_size_r: Option<f32>,
    pub border_color_t: Option<Color>,
    pub border_color_b: Option<Color>,
    pub border_color_l: Option<Color>,
    pub border_color_r: Option<Color>,
    pub border_radius_tl: Option<f32>,
    pub border_radius_tr: Option<f32>,
    pub border_radius_bl: Option<f32>,
    pub border_radius_br: Option<f32>,
    // Background
    pub back_color: Option<Color>,
    pub back_image: Option<String>,
    pub back_image_url: Option<String>,
    pub back_image_atlas: Option<AtlasCoords>,
    pub back_image_raw: Option<Arc<BstImageView>>,
    pub back_image_raw_coords: Option<AtlasCoords>,
    pub back_image_cache: Option<AtlasCacheCtrl>,
    pub back_srgb_yuv: Option<bool>,
    pub back_image_effect: Option<ImageEffect>,
    // Text
    pub text: String,
    pub text_color: Option<Color>,
    pub text_height: Option<f32>,
    pub text_secret: Option<bool>,
    pub line_spacing: Option<f32>,
    pub line_limit: Option<usize>,
    pub text_wrap: Option<ImtTextWrap>,
    pub text_vert_align: Option<ImtVertAlign>,
    pub text_hori_align: Option<ImtHoriAlign>,
    pub custom_verts: Vec<BinVert>,
}

impl BinStyle {
    pub fn is_floating_compatible(&self) -> Result<(), String> {
        if self.position != Some(BinPosition::Floating) {
            Err(String::from("'position' must be 'BinPosition::Floating'."))
        } else if self.pos_from_t.is_some() {
            Err(String::from(
                "'pos_from_t' is not allowed or not implemented.",
            ))
        } else if self.pos_from_b.is_some() {
            Err(String::from(
                "'pos_from_b' is not allowed or not implemented.",
            ))
        } else if self.pos_from_l.is_some() {
            Err(String::from(
                "'pos_from_l' is not allowed or not implemented.",
            ))
        } else if self.pos_from_r.is_some() {
            Err(String::from(
                "'pos_from_r' is not allowed or not implemented.",
            ))
        } else if self.pos_from_t_pct.is_some() {
            Err(String::from(
                "'pos_from_t_pct' is not allowed or not implemented.",
            ))
        } else if self.pos_from_b_pct.is_some() {
            Err(String::from(
                "'pos_from_b_pct' is not allowed or not implemented.",
            ))
        } else if self.pos_from_l_pct.is_some() {
            Err(String::from(
                "'pos_from_l_pct' is not allowed or not implemented.",
            ))
        } else if self.pos_from_r_pct.is_some() {
            Err(String::from(
                "'pos_from_r_pct' is not allowed or not implemented.",
            ))
        } else if self.pos_from_l_offset.is_some() {
            Err(String::from(
                "'pos_from_l_offset' is not allowed or not implemented.",
            ))
        } else if self.pos_from_t_offset.is_some() {
            Err(String::from(
                "'pos_from_t_offset' is not allowed or not implemented.",
            ))
        } else if self.pos_from_r_offset.is_some() {
            Err(String::from(
                "'pos_from_r_offset' is not allowed or not implemented.",
            ))
        } else if self.pos_from_b_offset.is_some() {
            Err(String::from(
                "'pos_from_b_offset' is not allowed or not implemented.",
            ))
        } else if self.width.is_none() && self.width_pct.is_none() {
            Err(String::from("'width' or 'width_pct' must be set."))
        } else if self.height.is_none() && self.height_pct.is_none() {
            Err(String::from("'height' or 'height_pct' must be set."))
        } else {
            Ok(())
        }
    }
}

#[derive(Clone, Debug)]
pub enum ImageEffect {
    BackColorAdd,
    BackColorBehind,
    BackColorSubtract,
    BackColorMultiply,
    BackColorDivide,
    GlyphWithColor,
    Invert,
}

impl ImageEffect {
    pub fn vert_type(&self) -> i32 {
        match *self {
            ImageEffect::BackColorAdd => 102,
            ImageEffect::BackColorBehind => 103,
            ImageEffect::BackColorSubtract => 104,
            ImageEffect::BackColorMultiply => 105,
            ImageEffect::BackColorDivide => 106,
            ImageEffect::Invert => 107,
            ImageEffect::GlyphWithColor => 108,
        }
    }
}

#[derive(Default, Clone, Debug, PartialEq)]
pub struct BinVert {
    pub position: (f32, f32, i16),
    pub color: Color,
}

#[derive(Clone, Debug, PartialEq, Default)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Color {
    pub fn as_array(&self) -> [f32; 4] {
        [self.r, self.g, self.b, self.a]
    }

    fn ffh(mut c1: u8, mut c2: u8) -> f32 {
        if (97..=102).contains(&c1) {
            c1 -= 87;
        } else if (65..=70).contains(&c1) {
            c1 -= 65;
        } else if (48..=57).contains(&c1) {
            c1 = c1.checked_sub(48).unwrap();
        } else {
            c1 = 0;
        }

        if (97..=102).contains(&c2) {
            c2 -= 87;
        } else if (65..=70).contains(&c2) {
            c2 -= 65;
        } else if (48..=57).contains(&c2) {
            c2 = c2.checked_sub(48).unwrap();
        } else {
            c2 = 0;
        }
        ((c1 * 16) + c2) as f32 / 255.0
    }

    pub fn clamp(&mut self) {
        if self.r > 1.0 {
            self.r = 1.0;
        } else if self.r < 0.0 {
            self.r = 0.0;
        }
        if self.g > 1.0 {
            self.g = 1.0;
        } else if self.g < 0.0 {
            self.g = 0.0;
        }
        if self.b > 1.0 {
            self.b = 1.0;
        } else if self.b < 0.0 {
            self.b = 0.0;
        }
        if self.a > 1.0 {
            self.a = 1.0;
        } else if self.a < 0.0 {
            self.a = 0.0;
        }
    }

    pub fn to_linear(&mut self) {
        self.r = f32::powf((self.r + 0.055) / 1.055, 2.4);
        self.g = f32::powf((self.g + 0.055) / 1.055, 2.4);
        self.b = f32::powf((self.b + 0.055) / 1.055, 2.4);
        self.a = f32::powf((self.a + 0.055) / 1.055, 2.4);
    }

    pub fn to_nonlinear(&mut self) {
        self.r = (self.r.powf(1.0 / 2.4) * 1.055) - 0.055;
        self.g = (self.g.powf(1.0 / 2.4) * 1.055) - 0.055;
        self.b = (self.b.powf(1.0 / 2.4) * 1.055) - 0.055;
        self.a = (self.a.powf(1.0 / 2.4) * 1.055) - 0.055;
    }

    pub fn srgb_hex(code: &str) -> Self {
        let mut color = Self::from_hex(code);
        color.to_linear();
        color
    }

    pub fn from_hex(code: &str) -> Self {
        let mut iter = code.bytes();
        let mut red = 0.0;
        let mut green = 0.0;
        let mut blue = 0.0;
        let mut alpha = 1.0;

        red = match iter.next() {
            Some(c1) => {
                match iter.next() {
                    Some(c2) => Self::ffh(c1, c2),
                    None => {
                        return Color {
                            r: red,
                            g: green,
                            b: blue,
                            a: alpha,
                        }
                    },
                }
            },
            None => {
                return Color {
                    r: red,
                    g: green,
                    b: blue,
                    a: alpha,
                }
            },
        };
        green = match iter.next() {
            Some(c1) => {
                match iter.next() {
                    Some(c2) => Self::ffh(c1, c2),
                    None => {
                        return Color {
                            r: red,
                            g: green,
                            b: blue,
                            a: alpha,
                        }
                    },
                }
            },
            None => {
                return Color {
                    r: red,
                    g: green,
                    b: blue,
                    a: alpha,
                }
            },
        };
        blue = match iter.next() {
            Some(c1) => {
                match iter.next() {
                    Some(c2) => Self::ffh(c1, c2),
                    None => {
                        return Color {
                            r: red,
                            g: green,
                            b: blue,
                            a: alpha,
                        }
                    },
                }
            },
            None => {
                return Color {
                    r: red,
                    g: green,
                    b: blue,
                    a: alpha,
                }
            },
        };
        alpha = match iter.next() {
            Some(c1) => {
                match iter.next() {
                    Some(c2) => Self::ffh(c1, c2),
                    None => {
                        return Color {
                            r: red,
                            g: green,
                            b: blue,
                            a: alpha,
                        }
                    },
                }
            },
            None => {
                return Color {
                    r: red,
                    g: green,
                    b: blue,
                    a: alpha,
                }
            },
        };

        Color {
            r: red,
            g: green,
            b: blue,
            a: alpha,
        }
    }
}