Skip to content

Commit 64ef7a6

Browse files
committed
refactor gui and add safety features
1 parent 62563ef commit 64ef7a6

File tree

7 files changed

+283
-267
lines changed

7 files changed

+283
-267
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ egui = { git = "https://github.com/emilk/egui" }
1919
eframe = { git = "https://github.com/emilk/egui" }
2020
dirs = "4.0.0"
2121
nfd2 = "0.3.0"
22+
23+
[features]

res/obj_icon.png

10.7 KB
Loading

src/color.rs

+51-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
use cgmath::Vector4;
22

3+
pub fn ftoi(v: f32) -> u8 {
4+
(v * 255.).round() as u8
5+
}
6+
7+
pub fn itof(v: u8) -> f32 {
8+
(v as f32) / 255.
9+
}
10+
311
pub fn modulus(a: f32, b: f32) -> f32 {
412
((a % b) + b) % b
513
}
@@ -10,50 +18,36 @@ pub fn float_equals(a: f32, b: f32) -> bool {
1018
}
1119

1220
pub fn rgb2hsv(rgb: Vector4<u8>) -> Vector4<f32> {
13-
let r = (rgb[0] as f32) / 255f32;
14-
let g = (rgb[1] as f32) / 255f32;
15-
let b = (rgb[2] as f32) / 255f32;
16-
let a = (rgb[3] as f32) / 255f32;
21+
let (r, g, b, a) =
22+
(itof(rgb[0]), itof(rgb[1]), itof(rgb[2]), itof(rgb[3]));
1723

1824
// max of rgb is equivalent to V in HSV
19-
let mut max = r;
20-
if g > max {
21-
max = g
22-
}
23-
if b > max {
24-
max = b
25-
}
25+
let max = r.max(g).max(b);
2626

2727
// min of rgb is V - C where C is chroma (range)
28-
let mut min = r;
29-
if g < min {
30-
min = g
31-
}
32-
if b < min {
33-
min = b
34-
}
28+
let min = r.min(g).min(b);
3529

3630
let mid = max - min;
3731

38-
let mut hue = if float_equals(mid, 0f32) {
39-
0f32
32+
let mut hue = if float_equals(mid, 0.) {
33+
0.
4034
} else if float_equals(max, r) {
41-
modulus((g - b) / mid, 6f32)
35+
modulus((g - b) / mid, 6.)
4236
} else if float_equals(max, g) {
43-
(b - r) / mid + 2f32
37+
(b - r) / mid + 2.
4438
} else if float_equals(max, b) {
45-
(r - g) / mid + 4f32
39+
(r - g) / mid + 4.
4640
} else {
47-
0f32
41+
0.
4842
};
4943

5044
hue *= std::f32::consts::PI / 3f32;
51-
if hue < 0f32 {
52-
hue += 2f32 * std::f32::consts::PI
45+
if hue < 0. {
46+
hue += 2. * std::f32::consts::PI
5347
}
5448

55-
let saturation = if float_equals(max, 0f32) {
56-
0f32
49+
let saturation = if float_equals(max, 0.) {
50+
0.
5751
} else {
5852
mid / max
5953
};
@@ -62,11 +56,13 @@ pub fn rgb2hsv(rgb: Vector4<u8>) -> Vector4<f32> {
6256
}
6357

6458
fn color_conversion(color: u8) -> u8 {
65-
if (color as f64 / 255.0) > 0.04045 {
66-
((((color as f64 / 255.0) / 1.055) + 0.052_132_7).powf(2.4) * 255.0) as u8
59+
let color = itof(color);
60+
let c = if color > 0.04045 {
61+
((color / 1.055) + 0.052_132_7).powf(2.4)
6762
} else {
68-
((color as f64 / 255.0) / 12.92 * 255.0) as u8
69-
}
63+
color / 12.92
64+
};
65+
ftoi(c)
7066
}
7167

7268
pub fn gamma_correct(rgb: Vector4<u8>) -> Vector4<u8> {
@@ -79,69 +75,60 @@ pub fn gamma_correct(rgb: Vector4<u8>) -> Vector4<u8> {
7975
}
8076

8177
pub fn hsv2rgb(hsv: Vector4<f32>) -> Vector4<u8> {
82-
let hue = hsv[0] * 180f32 / std::f32::consts::PI;
78+
let hue = hsv[0] * 180. / std::f32::consts::PI;
8379
let saturation = hsv[1];
8480
let value = hsv[2];
8581

8682
let chroma = value * saturation;
87-
let x = chroma * (1f32 - (modulus(hue / 60f32, 2f32) - 1f32).abs());
83+
let x = chroma * (1f32 - (modulus(hue / 60., 2f32) - 1f32).abs());
8884
let match_value = value - chroma;
8985

9086
let (r, g, b) = match hue {
91-
hh if hh < 60f32 => (chroma, x, 0f32),
92-
hh if hh < 120f32 => (x, chroma, 0f32),
93-
hh if hh < 180f32 => (0f32, chroma, x),
94-
hh if hh < 240f32 => (0f32, x, chroma),
95-
hh if hh < 300f32 => (x, 0f32, chroma),
96-
hh if hh < 360f32 => (chroma, 0f32, x),
97-
_ => (0f32, 0f32, 0f32),
87+
hh if hh < 60. => (chroma, x, 0.),
88+
hh if hh < 120. => (x, chroma, 0.),
89+
hh if hh < 180. => (0., chroma, x),
90+
hh if hh < 240. => (0., x, chroma),
91+
hh if hh < 300. => (x, 0., chroma),
92+
hh if hh < 360. => (chroma, 0., x),
93+
_ => (0., 0., 0.),
9894
};
9995

10096
Vector4::new(
101-
((r + match_value) * 255f32) as u8,
102-
((g + match_value) * 255f32) as u8,
103-
((b + match_value) * 255f32) as u8,
104-
(hsv[3] * 255f32) as u8,
97+
((r + match_value) * 255.) as u8,
98+
((g + match_value) * 255.) as u8,
99+
((b + match_value) * 255.) as u8,
100+
(hsv[3] * 255.) as u8,
105101
)
106102
}
107103

108104
pub fn hsv_distance(a: &Vector4<f32>, b: &Vector4<f32>) -> f32 {
109-
// (a.x - b.x).powf(2.0) * 8./21.
110-
// + (a.y - b.y).powf(2.0) * 5./21.
111-
// + (a.z - b.z).powf(2.0) * 4./21.
112-
// + (a.w - b.w).powf(2.0) * 4./21.
113-
114105
(a.x.sin() * a.y - b.x.sin() * b.y).powf(2.0)
115106
+ (a.x.cos() * a.y - b.x.cos() * b.y).powf(2.0)
116107
+ (a.z - b.z).powf(2.0)
117108
+ (a.w - b.w).powf(2.0)
118109
}
119110

120111
pub fn hsv_average(colors: &[Vector4<u8>]) -> Vector4<f32> {
121-
let n = colors.len() as f32;
122-
let mut h_avg = 0f32;
123-
let mut s_avg = 0f32;
124-
let mut v_avg = 0f32;
125-
let mut a_avg = 0f32;
126-
112+
let (mut h_sum, mut s_sum, mut v_sum, mut a_sum) = (0., 0., 0., 0.);
127113
for c in colors {
128114
let color = rgb2hsv(*c);
129-
h_avg += color.x;
130-
s_avg += color.y;
131-
v_avg += color.z;
132-
a_avg += color.w;
115+
h_sum += color.x;
116+
s_sum += color.y;
117+
v_sum += color.z;
118+
a_sum += color.w;
133119
}
134120

135-
Vector4::<f32>::new(h_avg / n, s_avg / n, v_avg / n, a_avg / n)
121+
let n = colors.len() as f32;
122+
Vector4::<f32>::new(h_sum / n, s_sum / n, v_sum / n, a_sum / n)
136123
}
137124

138125
pub fn convert_colorset_to_hsv(colorset: &[brickadia::save::Color]) -> Vec<Vector4<f32>> {
139-
let mut new = Vec::<Vector4<f32>>::with_capacity(colorset.len());
126+
let mut converted_colorset = Vec::<Vector4<f32>>::with_capacity(colorset.len());
140127
for c in colorset {
141-
new.push(rgb2hsv(Vector4::new(c.r, c.g, c.b, c.a)));
128+
converted_colorset.push(rgb2hsv(Vector4::new(c.r, c.g, c.b, c.a)));
142129
}
143130

144-
new
131+
converted_colorset
145132
}
146133

147134
pub fn match_hsv_to_colorset(colorset: &[Vector4<f32>], color: &Vector4<f32>) -> usize {

src/gui.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use egui::special_emojis::GITHUB;
2+
use egui::{
3+
Button,
4+
Color32,
5+
CtxRef,
6+
Grid,
7+
Hyperlink,
8+
Label,
9+
RichText,
10+
Separator,
11+
TopBottomPanel,
12+
Ui,
13+
};
14+
15+
const BUTTON_COLOR: Color32 = Color32::from_rgb(15, 98, 254);
16+
const ERROR_COLOR: Color32 = Color32::from_rgb(255, 168, 168);
17+
const FOLDER_COLOR: Color32 = Color32::from_rgb(255, 206, 70);
18+
19+
pub fn add_grid(ui: &mut Ui, mut contents: impl FnMut(&mut Ui)) {
20+
Grid::new("")
21+
.num_columns(2)
22+
.spacing([40.0, 4.0])
23+
.striped(true)
24+
.show(ui, |ui| {
25+
contents(ui)
26+
});
27+
}
28+
29+
pub fn add_horizontal_line(ui: &mut Ui) {
30+
ui.add(Separator::default().spacing(20.));
31+
}
32+
33+
pub fn info_text(ui: &mut Ui) {
34+
ui.horizontal(|ui| {
35+
ui.label("❓ You can find your own Brickadia ID by visiting");
36+
ui.add(Hyperlink::from_label_and_url("brickadia.com/account", "https://brickadia.com/account"));
37+
ui.label("and clicking View Profile");
38+
});
39+
ui.label("Your ID will be shown in the URL");
40+
}
41+
42+
pub fn button(ui: &mut Ui, text: &str, enabled: bool) -> bool {
43+
let text = RichText::new(text).color(Color32::WHITE);
44+
let b = Button::new(text).fill(BUTTON_COLOR);
45+
ui.add_enabled(enabled, b).on_hover_text("WARNING! WILL OVERWRITE ANY EXISTING BRS").clicked()
46+
}
47+
48+
pub fn file_button(ui: &mut Ui) -> bool {
49+
ui.button(RichText::new("🗁").color(FOLDER_COLOR)).clicked()
50+
}
51+
52+
pub fn bool_color(b: bool) -> Color32 {
53+
if b {
54+
Color32::WHITE
55+
} else {
56+
ERROR_COLOR
57+
}
58+
}
59+
60+
pub fn footer(ctx: &CtxRef) {
61+
TopBottomPanel::bottom("footer").show(ctx, |ui: &mut Ui| {
62+
ui.vertical_centered(|ui| {
63+
ui.add_space(10.);
64+
ui.add(Label::new(RichText::new("obj2brs").monospace()));
65+
ui.label("by Smallguy/Kmschr and French Fries/CheezBarger");
66+
let text = format!("{} {}", GITHUB, "GitHub");
67+
ui.add(Hyperlink::from_label_and_url(text, "https://github.com/kmschr/obj2brs"));
68+
ui.add_space(10.);
69+
});
70+
});
71+
}

0 commit comments

Comments
 (0)