tinge 0.1.0

A terminal-gui tool for generate, analyze, convert and manipulate colors...
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// RustPixel
// copyright zipxing@hotmail.com 2022~2024

use crate::select::*;
use log::info;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use tinge_lib::{
    find_similar_colors, golden, gradient, random, TingeData, COLORS_WITH_NAME,
    COLORS_WITH_NAME_RGB_INDEX,
};
use rust_pixel::{
    context::Context,
    event::{event_emit, Event, KeyCode, MouseButton, MouseEventKind::*},
    game::Model,
    render::style::{ColorPro, ColorSpace, ColorSpace::*, COLOR_SPACE_COUNT},
};
use std::any::Any;
use TingeState::*;

pub const PALETTEW: u16 = 80;
pub const PALETTEH: u16 = 40;
pub const MENUX: u16 = 12;
pub const MENUY: u16 = 0;
pub const MENUW: u16 = 70;
pub const RANDOM_X: u16 = 6;
pub const RANDOM_Y: u16 = 4;
pub const RANDOM_W: u16 = 13;
pub const GRADIENT_X: u16 = 1;
pub const GRADIENT_Y: u16 = 19;
pub const GRADIENT_INPUT_COUNT: u16 = 8;
pub const GRADIENT_COUNT: u16 = GRADIENT_X * GRADIENT_Y;
pub const PICKER_COUNT_X_GRADIENT: u16 = 57;
pub const PICKER_COUNT_X: u16 = 76;
pub const PICKER_COUNT_Y: u16 = 18;
pub const MAIN_COLOR_MSG_X: u16 = 1;
pub const MAIN_COLOR_MSG_Y: u16 = 8;
pub const ADJX: u16 = 2;
pub const ADJY: u16 = 2;
pub const COL_COUNT: u16 = 4;
pub const ROW_COUNT: u16 = 19;
pub const C_WIDTH: u16 = 19;

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, FromPrimitive)]
pub enum TingeState {
    NameA,
    NameB,
    PickerA,
    PickerB,
    Random,
    Gradient,
    Golden,
}

pub enum MouseArea {
    Menu(u16),                  // x
    Named(u16, u16),            // x, y
    Picker(u16, u16, u16, u16), // picker type, area, x, y
    Random(u16, u16),           // x, y
    Gradient(u16, u16, u16),    // area, x, y
    Golden(u16, u16),           // x, y
}

pub struct TingeModel {
    pub data: TingeData,
    pub card: u8,
    pub main_color: ColorPro,
    pub main_color_similar: (usize, usize, usize),
    pub named_colors: Vec<(&'static str, ColorPro)>,
    pub gradient_input_colors: Vec<ColorPro>,
    pub gradient_colors: Vec<ColorPro>,
    pub random_colors: Vec<ColorPro>,
    pub picker_colors: Vec<ColorPro>,
    pub select: Select,
}

impl TingeModel {
    pub fn new() -> Self {
        let mut ncolors = COLORS_WITH_NAME.clone();
        ncolors.sort_by_key(|nc| (1000.0 - nc.1.brightness() * 1000.0) as i32);
        // ncolors.sort_by_key(|nc| (1000.0 - nc.1.hue() * 1000.0) as i32);
        // ncolors.sort_by_key(|nc| (nc.1.chroma() * 1000.0) as i32);

        Self {
            data: TingeData::new(),
            card: 0,
            main_color: COLORS_WITH_NAME[0].1,
            main_color_similar: (0, 0, 0),
            named_colors: ncolors,
            gradient_input_colors: vec![],
            gradient_colors: vec![],
            random_colors: vec![],
            picker_colors: vec![],
            select: Select::new(),
        }
    }

    fn mouse_in(&mut self, ctx: &Context, x: u16, y: u16) -> Option<MouseArea> {
        let st = TingeState::from_usize(ctx.state as usize).unwrap();
        // Menu(u16)
        if y == 0 {
            let menuidx = match x {
                0..=22 => 0,
                23..=33 => 1,
                34..=44 => 2,
                45..=57 => 3,
                58.. => 4,
            };
            return Some(MouseArea::Menu(menuidx));
        }
        match st {
            // Named(u16, u16) x, y
            NameA | NameB => {
                let a = (x - 2) / C_WIDTH;
                let b = y - 2;
                if (b as usize * self.select.cur().width + a as usize) < self.select.cur().count {
                    return Some(MouseArea::Named(a, b));
                }
            }
            // Picker(u16, u16, u16, u16) picker type, area, x, y
            PickerA => {
                let a = 0;
                let c = x - 2;
                let d = y - 2;
                if y >= 2 && y < 20 && x >= 2 && x <= 77 {
                    let b = 0;
                    return Some(MouseArea::Picker(a, b, c, d));
                }
                if y == 20 && x >= 2 && x <= 77 {
                    let b = 1;
                    return Some(MouseArea::Picker(a, b, (c as f64 * 4.0) as u16, 0));
                }
            }
            PickerB => {
                let c = ((x - 1) as f64 / PICKER_COUNT_X as f64 * 255.0) as u16;
                if y == 9 && x >= 2 && x <= 77 {
                    return Some(MouseArea::Picker(1, 0, c, 0));
                }
                if y == 11 && x >= 2 && x <= 77 {
                    return Some(MouseArea::Picker(1, 1, c, 0));
                }
                if y == 13 && x >= 2 && x <= 77 {
                    return Some(MouseArea::Picker(1, 2, c, 0));
                }
            }
            // Random(u16, u16) x, y
            Random => {
                if y >= 2 && y <= 5 && x >= 1 && x <= 78 {
                    let a = (x - 1) / RANDOM_W;
                    let b = y - 2;
                    return Some(MouseArea::Random(a, b));
                }
            }
            // Golden(u16, u16) x, y
            Golden => {
                if y >= 2 && y <= 5 && x >= 1 && x <= 78 {
                    let a = (x - 1) / RANDOM_W;
                    let b = y - 2;
                    return Some(MouseArea::Golden(a, b));
                }
            }
            // Gradient(u16, u16, u16) area, x, y
            Gradient => {
                if y >= 2 && y <= 19 && x >= 2 && x <= 58 {
                    return Some(MouseArea::Gradient(0, x - 2, y - 2));
                }
                if y == 20 && x >= 2 && x <= 58 {
                    return Some(MouseArea::Gradient(1, ((x - 2) as f64 * 4.0) as u16, 0));
                }
                if y >= 2 && y <= 9 && x >= 60 && x <= 67 {
                    if ((y - 2) as usize) < self.select.ranges[2].count {
                        return Some(MouseArea::Gradient(2, 0, y - 2));
                    }
                }
                if y >= 2 && y <= 20 && x >= 69 && x <= 77 {
                    if ((y - 2) as usize) < self.select.ranges[3].count {
                        return Some(MouseArea::Gradient(3, 0, y - 2));
                    }
                }
            }
        }
        info!("mouse...x{}, y{}", x, y);
        None
    }

    fn do_random(&mut self, context: &mut Context) {
        if context.state != Random as u8 {
            return;
        }
        random(
            RANDOM_X as usize * RANDOM_Y as usize,
            &mut self.data.rand,
            &mut self.random_colors,
        );
    }

    fn do_golden(&mut self, context: &mut Context) {
        if context.state != Golden as u8 {
            return;
        }
        golden(
            RANDOM_X as usize * RANDOM_Y as usize,
            &mut self.data.rand,
            &mut self.random_colors,
        );
    }

    fn do_gradient(&mut self, context: &mut Context) {
        if context.state != Gradient as u8 {
            return;
        }
        info!("do gradient..........");
        gradient(
            &self.gradient_input_colors,
            GRADIENT_COUNT as usize,
            &mut self.gradient_colors,
        );
        self.select.ranges[3] = SelectRange::new(
            1,
            self.gradient_colors.len() as usize,
            self.gradient_colors.len() as usize,
        );
        self.update_main_color(context);
        event_emit("Tinge.RedrawGradient");
    }

    fn add_gradient_input(&mut self, context: &mut Context) {
        if context.state != Gradient as u8 {
            return;
        }
        if self.gradient_input_colors.len() >= GRADIENT_INPUT_COUNT as usize {
            return;
        }
        let nc = get_pick_color(
            PICKER_COUNT_X_GRADIENT as usize,
            self.select.ranges[0].x,
            self.select.ranges[0].y,
            self.select.ranges[1].x,
            0,
        );
        self.gradient_input_colors.push(nc);
        self.select.ranges[2] = SelectRange::new(
            1,
            self.gradient_input_colors.len() as usize,
            self.gradient_input_colors.len() as usize,
        );
        self.do_gradient(context);
    }

    fn del_gradient_input(&mut self, context: &mut Context) {
        if context.state != Gradient as u8 {
            return;
        }
        if self.gradient_input_colors.len() == 0 {
            return;
        }
        self.gradient_input_colors.pop();
        self.select.ranges[2] = SelectRange::new(
            1,
            self.gradient_input_colors.len() as usize,
            self.gradient_input_colors.len() as usize,
        );
        self.do_gradient(context);
    }

    fn update_main_color(&mut self, context: &mut Context) {
        match TingeState::from_usize(context.state as usize).unwrap() {
            NameA => {
                self.main_color = self.named_colors
                    [self.select.cur().y * self.select.cur().width + self.select.cur().x]
                    .1;
            }
            NameB => {
                let idx = (COL_COUNT * ROW_COUNT) as usize
                    + self.select.cur().y * self.select.cur().width
                    + self.select.cur().x;
                self.main_color = self.named_colors[idx].1;
            }
            PickerA => {
                self.main_color = get_pick_color(
                    PICKER_COUNT_X as usize,
                    self.select.ranges[0].x,
                    self.select.ranges[0].y,
                    self.select.ranges[1].x,
                    0,
                );
            }
            PickerB => {
                info!(
                    "r...{}g...{}b...{}",
                    self.select.ranges[0].x, self.select.ranges[1].x, self.select.ranges[2].x
                );
                self.main_color = get_pick_color(
                    PICKER_COUNT_X as usize,
                    self.select.ranges[0].x,
                    self.select.ranges[1].x,
                    self.select.ranges[2].x,
                    2,
                );
            }
            Random | Golden => {
                self.main_color = self.random_colors
                    [self.select.cur().y * self.select.cur().width + self.select.cur().x];
            }
            Gradient => match self.select.area {
                0..=1 => {
                    self.main_color = get_pick_color(
                        PICKER_COUNT_X_GRADIENT as usize,
                        self.select.ranges[0].x,
                        self.select.ranges[0].y,
                        self.select.ranges[1].x,
                        0,
                    );
                }
                2 => {
                    if self.gradient_input_colors.len() != 0 {
                        self.main_color = self.gradient_input_colors[self.select.ranges[2].y];
                    }
                }
                3 => {
                    if self.gradient_colors.len() != 0 {
                        self.main_color = self.gradient_colors[self.select.ranges[3].y];
                    }
                }
                _ => {}
            },
        }
        // find similar colors by ciede2000...
        self.main_color_similar = find_similar_colors(&self.main_color);
        event_emit("Tinge.RedrawMainColor");
        event_emit("Tinge.RedrawSelect");
        event_emit("Tinge.RedrawPicker");
    }

    fn switch_state(&mut self, context: &mut Context, st: TingeState) {
        context.state = st as u8;
        match st {
            NameA => {
                self.select.clear();
                self.select.add_range(SelectRange::new(
                    COL_COUNT as usize,
                    ROW_COUNT as usize,
                    (COL_COUNT * ROW_COUNT) as usize,
                ));
                self.update_main_color(context);
            }
            NameB => {
                self.select.clear();
                self.select.add_range(SelectRange::new(
                    COL_COUNT as usize,
                    ROW_COUNT as usize - 3,
                    self.named_colors.len() - (COL_COUNT * ROW_COUNT) as usize,
                ));
                self.update_main_color(context);
            }
            PickerA => {
                self.select.clear();
                let w = PICKER_COUNT_X as usize;
                let h = PICKER_COUNT_Y as usize;
                self.select.add_range(SelectRange::new(w, h, w * h));
                self.select.add_range(SelectRange::new(w * 4, 1, w * 4));
                self.update_main_color(context);
                event_emit("Tinge.RedrawPicker");
            }
            PickerB => {
                self.select.clear();
                self.select.add_range(SelectRange::new(256, 1, 256));
                self.select.add_range(SelectRange::new(256, 1, 256));
                self.select.add_range(SelectRange::new(256, 1, 256));
                self.update_main_color(context);
                event_emit("Tinge.RedrawPicker");
            }
            Random => {
                self.select.clear();
                let w = RANDOM_X as usize;
                let h = RANDOM_Y as usize;
                self.select.add_range(SelectRange::new(w, h, w * h));
                self.do_random(context);
                self.update_main_color(context);
                event_emit("Tinge.RedrawRandom");
            }
            Gradient => {
                self.select.clear();
                let w = PICKER_COUNT_X_GRADIENT as usize;
                let h = PICKER_COUNT_Y as usize;
                self.select.add_range(SelectRange::new(w, h, w * h));
                self.select.add_range(SelectRange::new(w * 4, 1, w * 4));
                // gradient input ...
                self.select.add_range(SelectRange::new(
                    1,
                    self.gradient_input_colors.len() as usize,
                    self.gradient_input_colors.len() as usize,
                ));
                // gradient ...
                self.select.add_range(SelectRange::new(
                    GRADIENT_X as usize,
                    GRADIENT_Y as usize,
                    GRADIENT_COUNT as usize,
                ));
                self.do_gradient(context);
                self.update_main_color(context);
                event_emit("Tinge.RedrawPicker");
            }
            Golden => {
                self.select.clear();
                let w = RANDOM_X as usize;
                let h = RANDOM_Y as usize;
                self.select.add_range(SelectRange::new(w, h, w * h));
                self.do_golden(context);
                self.update_main_color(context);
                event_emit("Tinge.RedrawRandom");
            }
        }
        event_emit("Tinge.RedrawMenu");
        event_emit("Tinge.RedrawPanel");
    }
}

impl Model for TingeModel {
    fn init(&mut self, context: &mut Context) {
        self.data.shuffle();
        self.card = self.data.next();

        context.state = TingeState::NameA as u8;

        let ctest = ColorPro::from_space_f64(SRGBA, 0.0, 1.0, 0.0, 1.0);
        for i in 0..COLOR_SPACE_COUNT {
            info!(
                "{}:{:?}",
                ColorSpace::from_usize(i).unwrap(),
                ctest.space_matrix[i].unwrap()
            );
        }

        // get gradient colors...
        self.gradient_input_colors = vec![
            ColorPro::from_space_f64(SRGBA, 1.0, 0.0, 0.0, 1.0),
            ColorPro::from_space_f64(SRGBA, 1.0, 1.0, 0.0, 1.0),
            ColorPro::from_space_f64(SRGBA, 0.0, 1.0, 1.0, 1.0),
            ColorPro::from_space_f64(SRGBA, 1.0, 0.0, 0.8, 1.0),
        ];

        // init hsv picker
        for y in 0..PICKER_COUNT_X {
            for x in 0..PICKER_COUNT_Y {
                let rx = (x as f64) / (PICKER_COUNT_X as f64);
                let ry = (y as f64) / (PICKER_COUNT_X as f64);

                let h = 360.0 * rx;
                let s = 0.6;
                let l = 0.95 * ry;

                // Start with HSL
                let color1 = ColorPro::from_space_f64(HSLA, h, s, l, 1.0);

                // But (slightly) normalize the luminance
                let mut l = color1[LchA].unwrap().v[0];
                l = (l + ry * 100.0) / 2.0;
                let color = ColorPro::from_space_f64(
                    LchA,
                    l,
                    color1[LchA].unwrap().v[1],
                    color1[LchA].unwrap().v[2],
                    1.0,
                );
                self.picker_colors.push(color);
            }
        }

        self.switch_state(context, NameA);
    }

    fn handle_input(&mut self, context: &mut Context, _dt: f32) {
        let es = context.input_events.clone();
        for e in &es {
            match e {
                Event::Mouse(mou) => {
                    if mou.kind == Up(MouseButton::Left) {
                        match self.mouse_in(context, mou.column, mou.row) {
                            Some(MouseArea::Menu(i)) => {
                                let sts = [NameA, PickerA, Random, Gradient, Golden];
                                self.switch_state(context, sts[i as usize]);
                            }
                            Some(MouseArea::Named(a, b)) => {
                                self.select.cur().x = a as usize;
                                self.select.cur().y = b as usize;
                                self.update_main_color(context);
                            }
                            Some(MouseArea::Picker(_a, b, c, d)) => {
                                self.select.area = b as usize;
                                self.select.ranges[b as usize].x = c as usize;
                                self.select.ranges[b as usize].y = d as usize;
                                self.update_main_color(context);
                            }
                            Some(MouseArea::Random(a, b)) => {
                                self.select.cur().x = a as usize;
                                self.select.cur().y = b as usize;
                                self.update_main_color(context);
                            }
                            Some(MouseArea::Gradient(b, c, d)) => {
                                self.select.area = b as usize;
                                self.select.ranges[b as usize].x = c as usize;
                                self.select.ranges[b as usize].y = d as usize;
                                self.update_main_color(context);
                            }
                            Some(MouseArea::Golden(a, b)) => {
                                self.select.cur().x = a as usize;
                                self.select.cur().y = b as usize;
                                self.update_main_color(context);
                            }
                            _ => {}
                        }
                    }
                }
                Event::Key(key) => match key.code {
                    KeyCode::Char('1') => {
                        self.switch_state(context, NameA);
                    }
                    KeyCode::Char('n') => {
                        if context.state == NameA as u8 {
                            self.switch_state(context, NameB);
                        } else if context.state == NameB as u8 {
                            self.switch_state(context, NameA);
                        } else if context.state == PickerA as u8 {
                            self.switch_state(context, PickerB);
                        } else if context.state == PickerB as u8 {
                            self.switch_state(context, PickerA);
                        }
                    }
                    KeyCode::Char('2') => {
                        self.switch_state(context, PickerA);
                    }
                    KeyCode::Char('3') => {
                        self.switch_state(context, Random);
                    }
                    KeyCode::Char('4') => {
                        self.switch_state(context, Gradient);
                    }
                    KeyCode::Char('5') => {
                        self.switch_state(context, Golden);
                    }
                    KeyCode::Char('a') => {
                        self.add_gradient_input(context);
                    }
                    KeyCode::Char('d') => {
                        self.del_gradient_input(context);
                    }
                    KeyCode::Char('g') => {
                        self.do_gradient(context);
                    }
                    KeyCode::Up => {
                        self.select.cur().backward_y();
                        self.update_main_color(context);
                    }
                    KeyCode::Down => {
                        self.select.cur().forward_y();
                        self.update_main_color(context);
                    }
                    KeyCode::Left => {
                        self.select.cur().backward_x();
                        self.update_main_color(context);
                    }
                    KeyCode::Right => {
                        self.select.cur().forward_x();
                        self.update_main_color(context);
                    }
                    KeyCode::Tab => {
                        self.select.switch_area();
                        self.update_main_color(context);
                    }
                    _ => {
                        // context.state = TingeState::Picker as u8;
                    }
                },
            }
        }
        context.input_events.clear();
    }

    fn handle_auto(&mut self, _context: &mut Context, _dt: f32) {}
    fn handle_event(&mut self, _context: &mut Context, _dt: f32) {}
    fn handle_timer(&mut self, _context: &mut Context, _dt: f32) {}
    fn as_any(&mut self) -> &mut dyn Any {
        self
    }
}

pub fn get_pick_color(width: usize, x0: usize, y0: usize, x1: usize, t: usize) -> ColorPro {
    let h = 360.0 / 4.0 / width as f64 * x1 as f64;
    let s = 1.0 / width as f64 * x0 as f64;
    let v = 1.0 / PICKER_COUNT_Y as f64 * y0 as f64;

    let r = x0 as f64;
    let g = y0 as f64;
    let b = x1 as f64;

    match t {
        0 => ColorPro::from_space_f64(HSVA, h, s, 1.0 - v, 1.0),
        1 => ColorPro::from_space_f64(HSVA, h, 1.0, 1.0, 1.0),
        2.. => ColorPro::from_space_f64(SRGBA, r / 255.0, g / 255.0, b / 255.0, 1.0),
    }
}

pub fn get_color_info(c: ColorPro, idx: u16) -> String {
    match idx {
        0 => {
            let rgb = c.get_srgba_u8();
            if let Some(cp) = COLORS_WITH_NAME_RGB_INDEX.get(&rgb) {
                format!(
                    "#{:02X}{:02X}{:02X} {},{},{} {:20}",
                    rgb.0,
                    rgb.1,
                    rgb.2,
                    rgb.0,
                    rgb.1,
                    rgb.2,
                    COLORS_WITH_NAME[*cp].0.to_string(),
                )
            } else {
                format!(
                    "#{:02X}{:02X}{:02X} {},{},{} {:20}",
                    rgb.0, rgb.1, rgb.2, rgb.0, rgb.1, rgb.2, " "
                )
            }
        }
        1..=8 => {
            let display_space = [2, 4, 6, 7, 8, 9, 11, 12];
            let cidx = display_space[idx as usize - 1];
            format!(
                "{} :{:?}",
                ColorSpace::from_usize(cidx).unwrap(),
                c.space_matrix[cidx].unwrap()
            )
        }
        _ => "".to_string(),
    }
}