oxiced 0.5.1

An iced themeing library
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use iced::{
    Theme, color,
    theme::{
        Palette,
        palette::{Background, Danger, Extended, Pair, Primary, Secondary, Success},
    },
};
use once_cell::sync::Lazy;
use serde::{Deserialize, de::DeserializeOwned};

use crate::{
    theme::theme::OXITHEME,
    utils::{
        color::{darken_color, lighten_color},
        file::get_theme_toml,
    },
};

pub fn get_all_themes() -> Vec<Theme> {
    let current = get_theme().clone();
    let all = [
        Theme::ALL,
        &[Theme::custom_with_fn(
            TOKYO_NIGHT_DARK_NAME.into(),
            TOKYO_NIGHT_DARK,
            |_: Palette| tokyo_generate(TOKYO_NIGHT_DARK),
        )],
    ]
    .concat();
    if all.contains(&current) {
        all
    } else {
        [
            Theme::ALL,
            &[current],
            &[Theme::custom_with_fn(
                TOKYO_NIGHT_DARK_NAME.into(),
                TOKYO_NIGHT_DARK,
                |_: Palette| tokyo_generate(TOKYO_NIGHT_DARK),
            )],
        ]
        .concat()
    }
}

const TOKYO_NIGHT_DARK_NAME: &str = "Tokyo Night Dark";

pub const TOKYO_NIGHT_DARK: Palette = Palette {
    background: color!(0x1a1b26), // Background (Night)
    text: color!(0xC0CAF5),       // Text
    primary: color!(0x1a1b26),    // Background (Night)
    success: color!(0x9ece6a),    // Green
    danger: color!(0xf7768e),     // Red
};

pub trait TomlTheme {
    fn name(&self) -> String;
}

//pub struct Base16 {
//    name: String,
//    base00: Color, //base
//    base01: Color, //mantle
//    base02: Color, //surface0
//    base03: Color, //surface1
//    base04: Color, //surface2
//    base05: Color, //text
//    base06: Color, //rosewater
//    base07: Color, //lavender
//    base08: Color, //red
//    base09: Color, //peach
//    base0a: Color, //yellow
//    base0b: Color, //green
//    base0c: Color, //teal
//    base0d: Color, //blue
//    base0e: Color, //mauve
//    base0f: Color, //flamingo
//}

#[derive(Deserialize)]
#[allow(dead_code)]
pub struct TomlBase16 {
    name: String,
    base00: String, // Default Background
    base01: String, // Lighter Background (Used for status bars, line number and folding marks)
    base02: String, // Selection Background
    base03: String, // Comments, Invisibles, Line Highlighting
    base04: String, // Dark Foreground (Used for status bars)
    base05: String, // Default Foreground, Caret, Delimiters, Operators
    base06: String, // Light Foreground (Not often used)
    base07: String, // Light Background (Not often used)
    base08: String, // Variables, XML Tags, Markup Link Text, Markup Lists, Diff Deleted
    base09: String, // Integers, Boolean, Constants, XML Attributes, Markup Link Url
    base0a: String, // Classes, Markup Bold, Search Text Background
    base0b: String, // Strings, Inherited Class, Markup Code, Diff Inserted
    base0c: String, // Support, Regular Expressions, Escape Characters, Markup Quotes
    base0d: String, // Functions, Methods, Attribute IDs, Headings
    base0e: String, // Keywords, Storage, Selector, Markup Italic, Diff Changed
    base0f: String, // Deprecated, Opening/Closing Embedded Language Tags, e.g. <?php ?>
}

pub struct ExtendedHex<'a> {
    pub background_weak_color: &'a String,
    pub background_base_color: &'a String,
    pub background_strong_color: &'a String,
    pub background_weak_text: &'a String,
    pub background_base_text: &'a String,
    pub background_strong_text: &'a String,

    pub primary_weak_color: &'a String,
    pub primary_base_color: &'a String,
    pub primary_strong_color: &'a String,
    pub primary_weak_text: &'a String,
    pub primary_base_text: &'a String,
    pub primary_strong_text: &'a String,

    pub secondary_weak_color: &'a String,
    pub secondary_base_color: &'a String,
    pub secondary_strong_color: &'a String,
    pub secondary_weak_text: &'a String,
    pub secondary_base_text: &'a String,
    pub secondary_strong_text: &'a String,

    pub success_weak_color: &'a String,
    pub success_base_color: &'a String,
    pub success_strong_color: &'a String,
    pub success_weak_text: &'a String,
    pub success_base_text: &'a String,
    pub success_strong_text: &'a String,

    pub danger_weak_color: &'a String,
    pub danger_base_color: &'a String,
    pub danger_strong_color: &'a String,
    pub danger_weak_text: &'a String,
    pub danger_base_text: &'a String,
    pub danger_strong_text: &'a String,
}

impl<'a> From<&'a TomlBase16> for ExtendedHex<'a> {
    fn from(value: &'a TomlBase16) -> Self {
        Self {
            background_weak_color: &value.base00,
            background_base_color: &value.base01,
            background_strong_color: &value.base01,

            background_weak_text: &value.base05,
            background_base_text: &value.base05,
            background_strong_text: &value.base05,

            primary_weak_color: &value.base02,
            primary_base_color: &value.base03,
            primary_strong_color: &value.base04,

            primary_weak_text: &value.base05,
            primary_base_text: &value.base05,
            primary_strong_text: &value.base05,

            secondary_weak_color: &value.base02,
            secondary_base_color: &value.base03,
            secondary_strong_color: &value.base04,

            secondary_weak_text: &value.base05,
            secondary_base_text: &value.base05,
            secondary_strong_text: &value.base05,

            success_weak_color: &value.base06,
            success_base_color: &value.base07,
            success_strong_color: &value.base08,

            success_weak_text: &value.base09,
            success_base_text: &value.base0a,
            success_strong_text: &value.base0b,

            danger_weak_color: &value.base0c,
            danger_base_color: &value.base0d,
            danger_strong_color: &value.base0e,

            danger_weak_text: &value.base0f,
            // not used currently
            danger_base_text: &value.base0f,
            danger_strong_text: &value.base0f,
        }
    }
}

impl From<TomlBase16> for Extended {
    fn from(val: TomlBase16) -> Self {
        let hex = ExtendedHex::from(&val);
        Extended {
            background: Background {
                base: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.background_base_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.background_base_text, 16).unwrap_or(0) as f32
                    ),
                },
                weak: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.background_weak_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.background_weak_text, 16).unwrap_or(0) as f32
                    ),
                },
                strong: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.background_strong_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.background_strong_text, 16).unwrap_or(0) as f32
                    ),
                },
            },
            primary: Primary {
                base: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.primary_base_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.primary_base_text, 16).unwrap_or(0) as f32
                    ),
                },
                weak: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.primary_weak_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.primary_weak_text, 16).unwrap_or(0) as f32
                    ),
                },
                strong: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.primary_strong_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.primary_strong_text, 16).unwrap_or(0) as f32
                    ),
                },
            },
            secondary: Secondary {
                base: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.secondary_base_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.secondary_base_text, 16).unwrap_or(0) as f32
                    ),
                },
                weak: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.secondary_weak_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.secondary_weak_text, 16).unwrap_or(0) as f32
                    ),
                },
                strong: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.secondary_strong_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.secondary_strong_text, 16).unwrap_or(0) as f32
                    ),
                },
            },
            success: Success {
                base: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.success_base_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.success_base_text, 16).unwrap_or(0) as f32
                    ),
                },
                weak: Pair {
                    color: darken_color(
                        &color!(
                            u32::from_str_radix(&hex.success_weak_color, 16).unwrap_or(0) as f32
                        ),
                        OXITHEME.tint_amount,
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.success_weak_text, 16).unwrap_or(0) as f32
                    ),
                },
                strong: Pair {
                    color: lighten_color(
                        &color!(
                            u32::from_str_radix(&hex.success_strong_color, 16).unwrap_or(0) as f32
                        ),
                        OXITHEME.tint_amount,
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.success_strong_text, 16).unwrap_or(0) as f32
                    ),
                },
            },
            danger: Danger {
                base: Pair {
                    color: color!(
                        u32::from_str_radix(&hex.danger_base_color, 16).unwrap_or(0) as f32
                    ),
                    text: color!(u32::from_str_radix(&hex.danger_base_text, 16).unwrap_or(0) as f32),
                },
                weak: Pair {
                    color: darken_color(
                        &color!(u32::from_str_radix(&hex.danger_weak_color, 16).unwrap_or(0) as f32),
                        OXITHEME.tint_amount,
                    ),
                    text: color!(u32::from_str_radix(&hex.danger_weak_text, 16).unwrap_or(0) as f32),
                },
                strong: Pair {
                    color: lighten_color(
                        &color!(
                            u32::from_str_radix(&hex.danger_strong_color, 16).unwrap_or(0) as f32
                        ),
                        OXITHEME.tint_amount,
                    ),
                    text: color!(
                        u32::from_str_radix(&hex.danger_strong_text, 16).unwrap_or(0) as f32
                    ),
                },
            },
            is_dark: true,
        }
    }
}

impl TomlTheme for TomlBase16 {
    fn name(&self) -> String {
        self.name.clone()
    }
}

#[derive(Deserialize)]
pub struct TomlSimple {
    name: String,
    background: u32,
    text: u32,
    primary: u32,
    success: u32,
    danger: u32,
}
impl From<TomlSimple> for Palette {
    fn from(val: TomlSimple) -> Self {
        Palette {
            background: color!(val.background),
            text: color!(val.text),
            primary: color!(val.primary),
            success: color!(val.success),
            danger: color!(val.danger),
        }
    }
}

#[derive(Deserialize)]
pub struct TomlExtended {
    name: String,
    is_dark: bool,
    background: TomlPalette,
    primary: TomlPalette,
    secondary: TomlPalette,
    success: TomlPalette,
    danger: TomlPalette,
}

impl From<TomlExtended> for Extended {
    fn from(val: TomlExtended) -> Self {
        Extended {
            background: Background {
                base: Pair {
                    color: color!(val.background.base.color),
                    text: color!(val.background.base.text),
                },
                weak: Pair {
                    color: color!(val.background.weak.color),
                    text: color!(val.background.weak.text),
                },
                strong: Pair {
                    color: color!(val.background.strong.color),
                    text: color!(val.background.strong.text),
                },
            },
            primary: Primary {
                base: Pair {
                    color: color!(val.primary.base.color),
                    text: color!(val.primary.base.text),
                },
                weak: Pair {
                    color: color!(val.primary.weak.color),
                    text: color!(val.primary.weak.text),
                },
                strong: Pair {
                    color: color!(val.primary.strong.color),
                    text: color!(val.primary.strong.text),
                },
            },
            secondary: Secondary {
                base: Pair {
                    color: color!(val.secondary.base.color),
                    text: color!(val.secondary.base.text),
                },
                weak: Pair {
                    color: color!(val.secondary.weak.color),
                    text: color!(val.secondary.weak.text),
                },
                strong: Pair {
                    color: color!(val.secondary.strong.color),
                    text: color!(val.secondary.strong.text),
                },
            },
            success: Success {
                base: Pair {
                    color: color!(val.success.base.color),
                    text: color!(val.success.base.text),
                },
                weak: Pair {
                    color: color!(val.success.weak.color),
                    text: color!(val.success.weak.text),
                },
                strong: Pair {
                    color: color!(val.success.strong.color),
                    text: color!(val.success.strong.text),
                },
            },
            danger: Danger {
                base: Pair {
                    color: color!(val.danger.base.color),
                    text: color!(val.danger.base.text),
                },
                weak: Pair {
                    color: color!(val.danger.weak.color),
                    text: color!(val.danger.weak.text),
                },
                strong: Pair {
                    color: color!(val.danger.strong.color),
                    text: color!(val.danger.strong.text),
                },
            },
            is_dark: val.is_dark,
        }
    }
}

impl TomlTheme for TomlExtended {
    fn name(&self) -> String {
        self.name.clone()
    }
}

#[derive(Deserialize)]
pub struct TomlPalette {
    base: TomlPair,
    weak: TomlPair,
    strong: TomlPair,
}

#[derive(Deserialize)]
pub struct TomlPair {
    color: u32,
    text: u32,
}

pub fn get_theme() -> Theme {
    THEME.clone()
}

static THEME: Lazy<Theme> = Lazy::new(|| {
    let theme_string = get_theme_toml();
    if theme_string.is_err() {
        return Theme::custom_with_fn(
            TOKYO_NIGHT_DARK_NAME.into(),
            TOKYO_NIGHT_DARK,
            tokyo_generate,
        );
    }
    let theme_string = theme_string.unwrap();
    let base16_theme = parse_extended_palette::<TomlBase16>(&theme_string);
    if let Ok(theme) = base16_theme {
        return theme;
    }
    let theme = parse_simple_palette(&theme_string);
    if let Ok(theme) = theme {
        return theme;
    }
    let extended_theme = parse_extended_palette::<TomlExtended>(&theme_string);
    if let Ok(theme) = extended_theme {
        return theme;
    }
    Theme::custom_with_fn(
        TOKYO_NIGHT_DARK_NAME.into(),
        TOKYO_NIGHT_DARK,
        tokyo_generate,
    )
});

fn parse_simple_palette(theme_string: &str) -> Result<Theme, toml::de::Error> {
    let parsed_theme: TomlSimple = toml::from_str(theme_string)?;
    Ok(Theme::custom(
        parsed_theme.name.clone(),
        parsed_theme.into(),
    ))
}

pub fn parse_extended_palette<T: DeserializeOwned + TomlTheme + Into<Extended>>(
    theme_string: &str,
) -> Result<Theme, toml::de::Error> {
    let parsed_theme: T = toml::from_str(theme_string)?;
    Ok(Theme::custom_with_fn(
        parsed_theme.name(),
        TOKYO_NIGHT_DARK,
        |_: Palette| parsed_theme.into(),
    ))
}

fn tokyo_generate(palette: Palette) -> Extended {
    Extended {
        background: Background {
            base: Pair {
                color: palette.background, // base background
                text: palette.text,
            },
            weak: Pair {
                color: color!(0x222430), // used for dropdowns etc
                text: palette.text,
            },
            strong: Pair {
                color: palette.background, // used for not hovered borders
                text: palette.text,
            },
        },
        primary: Primary {
            base: Pair {
                color: color!(0x282A38), // used for buttons default and hovered dropdowns
                text: palette.text,
            },
            weak: Pair {
                color: palette.background, // no idea
                text: palette.text,
            },
            strong: Pair {
                // ???????????????????????????????/
                color: color!(0x3E4052), // used for buttons when hovered
                text: palette.text,
            },
        },
        secondary: Secondary {
            base: Pair {
                color: palette.background, // no idea
                text: palette.text,
            },
            weak: Pair {
                color: palette.background, // no idea
                text: palette.text,
            },
            strong: Pair {
                color: palette.background, // no idea
                text: palette.text,
            },
        },
        success: Success {
            base: Pair {
                color: palette.background,
                text: palette.text,
            },
            weak: Pair {
                color: palette.background,
                text: palette.text,
            },
            strong: Pair {
                color: palette.background,
                text: palette.text,
            },
        },
        danger: Danger {
            base: Pair {
                color: palette.background,
                text: palette.text,
            },
            weak: Pair {
                color: palette.background,
                text: palette.text,
            },
            strong: Pair {
                color: palette.background,
                text: palette.text,
            },
        },
        is_dark: true,
    }
}