lutgen-studio 0.4.0

Offical GUI for Lutgen, the best way to apply popular colorschemes to any image or wallpaper!
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
use std::ops::RangeInclusive;
use std::rc::Rc;

use strum::VariantArray;

use crate::palette::{lutgen_dir, DynamicPalette};
use crate::state::LutAlgorithm;
use crate::App;

/// Helper to add a labeled slider with dynamic DragValue sizing.
/// The DragValue is measured first, then the slider fills remaining space.
fn labeled_slider<Num: egui::emath::Numeric>(
    ui: &mut egui::Ui,
    label: &str,
    value: &mut Num,
    range: RangeInclusive<Num>,
) -> egui::Response {
    ui.label(label);
    ui.horizontal(|ui| {
        ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
            let drag = ui.add(egui::DragValue::new(value).range(range.clone()));
            ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
                ui.style_mut().spacing.slider_width =
                    ui.available_width() - ui.spacing().item_spacing.x;
                let slider = ui.add(egui::Slider::new(value, range).show_value(false));
                drag | slider
            })
            .inner
        })
        .inner
    })
    .inner
}

pub struct PaletteFilterBox {
    items: Vec<Rc<DynamicPalette>>,
    idx: usize,
    filter: String,
    filtered: Vec<Rc<DynamicPalette>>,
}

impl PaletteFilterBox {
    pub fn new(current: &DynamicPalette) -> Self {
        let items: Vec<_> = DynamicPalette::get_all()
            .unwrap()
            .into_iter()
            .map(Rc::new)
            .collect();

        Self {
            filter: String::new(),
            filtered: items.clone(),
            idx: items
                .iter()
                .position(|v| **v == *current)
                .unwrap_or_default(),
            items,
        }
    }

    pub fn reindex(&mut self, current: &DynamicPalette) {
        self.items = DynamicPalette::get_all()
            .unwrap()
            .into_iter()
            .map(Rc::new)
            .collect();
        self.filter();
        self.idx = self
            .filtered
            .iter()
            .position(|v| **v == *current)
            .unwrap_or_default();
    }

    fn filter(&mut self) {
        if self.filter.is_empty() {
            self.filtered = self.items.clone();
        } else {
            self.filtered = self
                .items
                .iter()
                .filter(|palette| palette.as_str().contains(&self.filter.to_lowercase()))
                .cloned()
                .collect();
        }
    }

    pub fn show(&mut self, ui: &mut egui::Ui, current: &mut DynamicPalette) -> egui::Response {
        let mut apply = false;
        let mut res = ui
            .group(|ui| {
                egui::Resize::default()
                    .resizable([true, false])
                    .min_width(ui.available_width())
                    .max_width(ui.available_width())
                    .with_stroke(false)
                    .show(ui, |ui| {
                        ui.horizontal(|ui| {
                            if egui::TextEdit::singleline(&mut self.filter)
                                .hint_text("Search Palettes ...")
                                .desired_width(ui.available_width() - 55.)
                                .show(ui)
                                .response
                                .changed()
                            {
                                // update filtered items
                                self.filter();
                                if !self.filtered.is_empty() {
                                    self.idx = 0;
                                    *current = (*self.filtered[self.idx]).clone();
                                }
                            }

                            if ui.button("<").clicked() && !self.filtered.is_empty() {
                                if self.idx == 0 {
                                    self.idx = self.filtered.len() - 1;
                                } else {
                                    self.idx -= 1;
                                }
                                *current = (*self.filtered[self.idx]).clone();
                                apply = true;
                            }
                            if ui.button(">").clicked() && !self.filtered.is_empty() {
                                if self.idx >= self.filtered.len() - 1 {
                                    self.idx = 0;
                                } else {
                                    self.idx += 1;
                                }
                                *current = (*self.filtered[self.idx]).clone();
                                apply = true;
                            }
                        });

                        ui.separator();

                        egui::ScrollArea::new([true, true])
                            .auto_shrink([false, false])
                            .show(ui, |ui| {
                                for (i, palette) in self.filtered.iter().enumerate() {
                                    let selected = (**palette).clone();
                                    let res = ui.add(
                                        egui::Button::selectable(
                                            *current == selected,
                                            palette.as_str(),
                                        )
                                        .min_size(egui::Vec2::new(ui.available_width() - 1., 16.)),
                                    );
                                    // scroll when item is focused
                                    res.gained_focus()
                                        .then(|| ui.scroll_to_cursor(Some(egui::Align::Center)));
                                    // scroll when we applied above
                                    if apply && *current == **palette {
                                        res.request_focus();
                                        ui.scroll_to_cursor(Some(egui::Align::Center));
                                    }
                                    if res.clicked() {
                                        *current = selected;
                                        self.idx = i;
                                        apply = true;
                                    }
                                }
                            });
                    })
            })
            .response;

        // If we need to apply a new palette, mark the response as changed
        if apply {
            res.mark_changed();
        }

        res
    }
}

pub struct PaletteEditor {
    name: String,
}

impl PaletteEditor {
    pub fn new(current: &DynamicPalette) -> Self {
        Self {
            name: current.to_string(),
        }
    }

    pub fn show(
        &mut self,
        ui: &mut egui::Ui,
        palette: &mut Vec<[u8; 3]>,
        current: &mut DynamicPalette,
    ) -> [bool; 2] {
        // color palette
        let mut apply = false;
        let mut saved = false;
        ui.group(|ui| {
            ui.horizontal(|ui| {
                let enabled = matches!(current, DynamicPalette::Custom(_));
                ui.add_enabled(
                    enabled,
                    egui::TextEdit::singleline(&mut self.name)
                        .desired_width(ui.available_width() - 49.),
                );

                if ui.add_enabled(enabled, egui::Button::new("save")).clicked() {
                    *current = DynamicPalette::Custom(self.name.clone());
                    current.save(palette).unwrap();
                    saved = true;
                }
            });
            ui.separator();
            ui.horizontal_wrapped(|ui| {
                ui.spacing_mut().interact_size.x =
                    calculate_width(ui.available_width() + 7., 40., ui.spacing().item_spacing.x);

                let mut res = Vec::new();
                for color in palette.iter_mut() {
                    res.push(egui::widgets::color_picker::color_edit_button_srgb(
                        ui, color,
                    ));
                }
                for (i, res) in res.iter().enumerate() {
                    if res.changed() {
                        if matches!(current, DynamicPalette::Builtin(_)) {
                            let name = current.to_string() + "-custom";
                            self.name = name.clone();
                            *current = DynamicPalette::Custom(name);
                        }
                        apply = true;
                    }
                    if res.secondary_clicked() {
                        if matches!(current, DynamicPalette::Builtin(_)) {
                            let name = current.to_string() + "-custom";
                            self.name = name.clone();
                            *current = DynamicPalette::Custom(name);
                        }
                        palette.remove(i);
                        apply = true;
                    }
                }

                if ui
                    .add(egui::Button::new("+").min_size(ui.spacing().interact_size))
                    .clicked()
                {
                    if matches!(current, DynamicPalette::Builtin(_)) {
                        let name = current.to_string() + "-custom";
                        self.name = name.clone();
                        *current = DynamicPalette::Custom(name);
                    }
                    palette.push([0u8; 3]);
                    apply = true;
                };
            });
        });
        [apply, saved]
    }
}

/// Calculates optimal button width for a wrapped grid with padding.
pub fn calculate_width(width: f32, target: f32, padding: f32) -> f32 {
    if width <= 0.0 || target <= 0.0 {
        return 0.0;
    }

    let target_with_padding = target + padding;
    if width < target_with_padding {
        return (width - padding).max(0.0);
    }

    let buttons_that_fit = (width / target_with_padding).round();
    if buttons_that_fit <= 0.0 {
        return (width - padding).max(0.0);
    }

    (width / buttons_that_fit) - padding
}

impl App {
    fn show_settings(&mut self, ui: &mut egui::Ui) -> bool {
        let mut apply = false;
        ui.group(|ui| {
            // Algorithm dropdown
            ui.horizontal(|ui| {
                ui.with_layout(
                    egui::Layout::right_to_left(egui::Align::Center),
                    |ui| {
                        if ui.button("Reset").clicked() {
                            self.state.reset_current_args();
                            self.apply();
                        }
                        egui::ComboBox::from_id_salt("algorithm")
                            .selected_text(format!("{:?}", self.state.current_alg))
                            .width(ui.available_width())
                            .show_ui(ui, |ui| {
                                for alg in LutAlgorithm::VARIANTS {
                                    let val = ui.selectable_value(
                                        &mut self.state.current_alg,
                                        *alg,
                                        alg.to_string(),
                                    );
                                    apply |= val.clicked();
                                    val.gained_focus().then(|| {
                                        ui.scroll_to_cursor(Some(egui::Align::Center))
                                    });
                                }
                            });
                    },
                );
            });
            ui.separator();

            // common args
            ui.heading("Common Arguments");
            ui.add_space(5.);

            let res = labeled_slider(ui, "Hald-Clut Level", &mut self.state.common.level, 4..=16);
            apply |= res.drag_stopped() | res.lost_focus();
            res.on_hover_text("\
                Hald clut level to generate. Heavy impact on performance for high levels. \n\
                A level of 16 computes a value for the entire sRGB color space.\n\n\
                Range: 4-16",
            );

            let res = labeled_slider(ui, "Luminosity Factor", self.state.common.lum_factor.as_mut(), 0.001..=2.);
            apply |= res.drag_stopped() | res.lost_focus();
            res.on_hover_text("\
                Factor to multiply luminocity values by. \
                Effectively weights the interpolation to prefer more \
                colorful or more greyscale/unsaturated matches.\n\n\
                Tip: Use values below 1.0 for more colorful results, \
                above 1.0 for less colorful results. \
                Extreme values usually are paired with 'Preserve Luminosity'.\n\n\
                Default: 0.7");

            let res = ui
                .checkbox(&mut self.state.common.preserve, "Preserve Luminosity");
            apply |= res.changed();
            res.on_hover_text("\
                Preserve the original image's luminocity values after interpolation. \
                This effectively retains the image's contrast and generally improves gradients.\n\n\
                Default: true");

            // unique algorithm args
            match self.state.current_alg {
                LutAlgorithm::GaussianRbf => {
                    ui.separator();
                    ui.heading("Gaussian Arguments");
                    ui.add_space(5.);

                    let res = labeled_slider(ui, "Shape", self.state.guassian_rbf.shape.as_mut(), 0.001..=512.);
                    apply |= res.drag_stopped() | res.lost_focus();
                    res.on_hover_text("\
                        Shape parameter for the default Gaussian RBF interpolation. \
                        Effectively creates more or less blending between colors in the palette.\n\n\
                        Bigger numbers = less blending (closer to original colors)\n\
                        Smaller numbers = more blending (smoother results)\n\n\
                        Default: 128.0");
                },
                LutAlgorithm::ShepardsMethod => {
                    ui.separator();
                    ui.heading("Shepard's Method Arguments");
                    ui.add_space(10.);

                    let res = labeled_slider(ui, "Power", self.state.shepards_method.power.as_mut(), 0.001..=64.);
                    apply |= res.drag_stopped() | res.lost_focus();
                    res.on_hover_text("\
                        Power parameter for Shepard's method (Inverse Distance RBF).\n\
                        Higher values give more weight to closer palette colors.\n\n\
                        Default: 4.0");
                },
                LutAlgorithm::GaussianSampling => {
                    ui.separator();
                    ui.heading("Guassian Sampling Arguments");
                    ui.add_space(10.);

                    let res = labeled_slider(ui, "Mean", self.state.guassian_sampling.mean.as_mut(), -127.0..=127.);
                    apply |= res.drag_stopped() | res.lost_focus();
                    res.on_hover_text("\
                        Average amount of noise to apply in each iteration. \
                        Controls the bias of the random sampling process, and can lighten \
                        or darken the image overall.\n\n\
                        Default: 0.0\nRange: -127.0 to 127.0");

                    let res = labeled_slider(ui, "Standard Deviation", self.state.guassian_sampling.std_dev.as_mut(), 1.0..=128.);
                    apply |= res.drag_stopped() | res.lost_focus();
                    res.on_hover_text("\
                        Standard deviation parameter for the noise applied in each iteration. \
                        Controls how much variation is applied during sampling.\n\n\
                        Default: 20.0");

                    let res = labeled_slider(ui, "Iterations", &mut self.state.guassian_sampling.iterations, 1..=1024);
                    apply |= res.drag_stopped() | res.lost_focus();
                    res.on_hover_text("\
                        Number of iterations of noise to apply to each pixel.\n\
                        More iterations = better blending but slower processing.\n\n\
                        Default: 512");

                    ui.label("RNG Seed");
                    let res = ui.add(
                        egui::DragValue::new(&mut self.state.guassian_sampling.seed)
                            .speed(2i32.pow(20)),
                    );
                    apply |= res.drag_stopped() | res.lost_focus();
                    res.on_hover_text("\
                        Seed for the random number generator used in noise generation.\n\n\
                        Default: 42080085");
                },
                LutAlgorithm::GaussianBlur => {
                    ui.separator();
                    ui.heading("Gaussian Blur Arguments");
                    ui.add_space(10.);

                    let res = labeled_slider(ui, "Radius", self.state.gaussian_blur.radius.as_mut(), 1.0..=64.0);
                    apply |= res.drag_stopped() | res.lost_focus();
                    res.on_hover_text("\
                        Gaussian blur radius (sigma) applied in OKLab color space.\n\n\
                        Higher values = larger blur kernel = more color blending\n\
                        Lower values = smaller kernel = sharper boundaries\n\n\
                        Default: 8.0");
                },
                _ => {},
            }

            // shared rbf args
            match self.state.current_alg {
                LutAlgorithm::GaussianRbf | LutAlgorithm::ShepardsMethod => {
                    let res = labeled_slider(ui, "Nearest Colors", &mut self.state.common_rbf.nearest, 0..=32);
                    apply |= res.drag_stopped() | res.lost_focus();
                    res.on_hover_text("\
                        Number of nearest colors to consider when interpolating.\n\n\
                        0 = uses all available colors ( O(n) )\n\
                        Lower values = faster processing\n\
                        Higher values = more blending\n\n\
                        Default: 16");
                },
                _ => {},
            }
        });

        ui.horizontal(|ui| {
            let res = ui.add(
                egui::Button::new("Copy CLI Arguments")
                    .min_size(egui::Vec2::new(ui.available_width(), 16.)),
            );
            if res.clicked() {
                let args = self.state.cli_args();
                ui.ctx()
                    .copy_text("lutgen apply ".to_string() + &args.join(" "));
            }
        });

        apply
    }

    pub fn show_sidebar_inner(&mut self, ui: &mut egui::Ui) {
        let mut apply = false;
        ui.add_space(4.);

        // palette menu
        if self
            .palette_box
            .show(ui, &mut self.state.palette_selection)
            .changed()
        {
            self.state.palette = self.state.palette_selection.get().to_vec();
            self.palette_edit.name = self.state.palette_selection.to_string();
            apply = true;
        }

        // palette editor
        let [changed, saved] = self.palette_edit.show(
            ui,
            &mut self.state.palette,
            &mut self.state.palette_selection,
        );
        apply |= changed;
        if saved {
            self.palette_box.reindex(&self.state.palette_selection);
            self.state.last_event = format!(
                "Saved custom palette to {}",
                lutgen_dir().join(&self.palette_edit.name).display()
            );
        }

        // settings panel
        apply |= self.show_settings(ui);

        if apply {
            self.apply();
        }
    }

    /// side panel for lut args
    pub fn show_sidebar(&mut self, ctx: &egui::Context) {
        if !self.inline_layout {
            egui::SidePanel::left("args")
                .resizable(true)
                .min_width(214.)
                .show(ctx, |ui| {
                    ui.take_available_width();
                    egui::ScrollArea::vertical().show(ui, |ui| {
                        self.show_sidebar_inner(ui);
                    });
                });
        }
    }
}