1use iced::Color;
8
9#[derive(Debug, Clone, Copy)]
28pub struct Style {
29 pub background: Color,
31 pub text_color: Color,
33 pub gutter_background: Color,
35 pub gutter_border: Color,
37 pub line_number_color: Color,
39 pub scrollbar_background: Color,
41 pub scroller_color: Color,
43 pub current_line_highlight: Color,
45 pub whitespace_color: Color,
47 pub search_match_color: Color,
49 pub search_match_current_color: Color,
51 pub selection_color: Color,
53 pub bracket_match_color: Color,
55 pub ime_preedit_background_color: Color,
57}
58
59pub trait Catalog {
79 type Class<'a>;
81
82 fn default<'a>() -> Self::Class<'a>;
84
85 fn style(&self, class: &Self::Class<'_>) -> Style;
87}
88
89pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme) -> Style + 'a>;
110
111impl Catalog for iced::Theme {
112 type Class<'a> = StyleFn<'a, Self>;
113
114 fn default<'a>() -> Self::Class<'a> {
115 Box::new(from_iced_theme)
116 }
117
118 fn style(&self, class: &Self::Class<'_>) -> Style {
119 class(self)
120 }
121}
122
123pub fn from_iced_theme(theme: &iced::Theme) -> Style {
160 let palette = theme.extended_palette();
161 let is_dark = palette.is_dark;
162
163 let background = palette.background.base.color;
165 let text_color = palette.background.base.text;
166
167 let gutter_background = palette.background.weak.color;
169 let gutter_border = if is_dark {
170 darken(palette.background.strong.color, 0.1)
171 } else {
172 lighten(palette.background.strong.color, 0.1)
173 };
174
175 let line_number_color = if is_dark {
179 dim_color(text_color, 0.5)
180 } else {
181 blend_colors(text_color, background, 0.5)
183 };
184
185 let scrollbar_background = background;
187 let scroller_color = palette.secondary.weak.color;
188
189 let current_line_highlight = with_alpha(
191 palette.primary.weak.color,
192 if is_dark { 0.15 } else { 0.25 },
193 );
194
195 let whitespace_color = if is_dark {
196 dim_color(text_color, 0.65)
197 } else {
198 blend_colors(text_color, background, 0.65)
199 };
200
201 let search_match_color = Color { r: 1.0, g: 1.0, b: 0.0, a: 0.3 };
205 let search_match_current_color = Color { r: 1.0, g: 0.6, b: 0.0, a: 0.4 };
206
207 let selection_color = Color { r: 0.3, g: 0.5, b: 0.8, a: 0.3 };
209
210 let bracket_match_color = Color { r: 0.5, g: 0.5, b: 0.5, a: 0.4 };
212
213 let ime_preedit_background_color =
215 Color { r: 1.0, g: 1.0, b: 1.0, a: 0.08 };
216
217 Style {
218 background,
219 text_color,
220 gutter_background,
221 gutter_border,
222 line_number_color,
223 scrollbar_background,
224 scroller_color,
225 current_line_highlight,
226 whitespace_color,
227 search_match_color,
228 search_match_current_color,
229 selection_color,
230 bracket_match_color,
231 ime_preedit_background_color,
232 }
233}
234
235fn darken(color: Color, factor: f32) -> Color {
237 Color {
238 r: color.r * (1.0 - factor),
239 g: color.g * (1.0 - factor),
240 b: color.b * (1.0 - factor),
241 a: color.a,
242 }
243}
244
245fn lighten(color: Color, factor: f32) -> Color {
247 Color {
248 r: color.r + (1.0 - color.r) * factor,
249 g: color.g + (1.0 - color.g) * factor,
250 b: color.b + (1.0 - color.b) * factor,
251 a: color.a,
252 }
253}
254
255fn dim_color(color: Color, factor: f32) -> Color {
257 Color {
258 r: color.r * factor,
259 g: color.g * factor,
260 b: color.b * factor,
261 a: color.a,
262 }
263}
264
265fn blend_colors(color1: Color, color2: Color, factor: f32) -> Color {
267 Color {
268 r: color1.r + (color2.r - color1.r) * factor,
269 g: color1.g + (color2.g - color1.g) * factor,
270 b: color1.b + (color2.b - color1.b) * factor,
271 a: color1.a + (color2.a - color1.a) * factor,
272 }
273}
274
275fn with_alpha(color: Color, alpha: f32) -> Color {
277 Color { r: color.r, g: color.g, b: color.b, a: alpha }
278}
279
280#[cfg(test)]
281mod tests {
282 use super::*;
283
284 #[test]
285 fn test_from_iced_theme_dark() {
286 let theme = iced::Theme::Dark;
287 let style = from_iced_theme(&theme);
288
289 let brightness =
291 (style.background.r + style.background.g + style.background.b)
292 / 3.0;
293 assert!(brightness < 0.5, "Dark theme should have dark background");
294
295 let text_brightness =
297 (style.text_color.r + style.text_color.g + style.text_color.b)
298 / 3.0;
299 assert!(text_brightness > 0.5, "Dark theme should have bright text");
300 }
301
302 #[test]
303 fn test_from_iced_theme_light() {
304 let theme = iced::Theme::Light;
305 let style = from_iced_theme(&theme);
306
307 let brightness =
309 (style.background.r + style.background.g + style.background.b)
310 / 3.0;
311 assert!(brightness > 0.5, "Light theme should have bright background");
312
313 let text_brightness =
315 (style.text_color.r + style.text_color.g + style.text_color.b)
316 / 3.0;
317 assert!(text_brightness < 0.5, "Light theme should have dark text");
318 }
319
320 #[test]
321 fn test_all_iced_themes_produce_valid_styles() {
322 for theme in iced::Theme::ALL {
324 let style = from_iced_theme(theme);
325
326 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
328 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
329 assert!(
330 style.gutter_background.r >= 0.0
331 && style.gutter_background.r <= 1.0
332 );
333 assert!(
334 style.line_number_color.r >= 0.0
335 && style.line_number_color.r <= 1.0
336 );
337
338 assert!(
340 style.current_line_highlight.a < 1.0,
341 "Current line highlight should be semi-transparent for theme: {:?}",
342 theme
343 );
344
345 for (name, color) in [
348 ("search_match_color", style.search_match_color),
349 (
350 "search_match_current_color",
351 style.search_match_current_color,
352 ),
353 ("selection_color", style.selection_color),
354 ("bracket_match_color", style.bracket_match_color),
355 (
356 "ime_preedit_background_color",
357 style.ime_preedit_background_color,
358 ),
359 ] {
360 assert!(
361 color.r >= 0.0 && color.r <= 1.0,
362 "{name}.r out of range for theme: {theme:?}"
363 );
364 assert!(
365 color.g >= 0.0 && color.g <= 1.0,
366 "{name}.g out of range for theme: {theme:?}"
367 );
368 assert!(
369 color.b >= 0.0 && color.b <= 1.0,
370 "{name}.b out of range for theme: {theme:?}"
371 );
372 assert!(
373 color.a > 0.0 && color.a < 1.0,
374 "{name} should be semi-transparent for theme: {theme:?}"
375 );
376 }
377 }
378 }
379
380 #[test]
381 fn test_overlay_colors_are_theme_independent() {
382 let light = from_iced_theme(&iced::Theme::Light);
385 let dark = from_iced_theme(&iced::Theme::Dark);
386
387 for (light_color, dark_color) in [
388 (light.search_match_color, dark.search_match_color),
389 (light.search_match_current_color, dark.search_match_current_color),
390 (light.selection_color, dark.selection_color),
391 (light.bracket_match_color, dark.bracket_match_color),
392 (
393 light.ime_preedit_background_color,
394 dark.ime_preedit_background_color,
395 ),
396 ] {
397 assert!((light_color.r - dark_color.r).abs() < f32::EPSILON);
398 assert!((light_color.g - dark_color.g).abs() < f32::EPSILON);
399 assert!((light_color.b - dark_color.b).abs() < f32::EPSILON);
400 assert!((light_color.a - dark_color.a).abs() < f32::EPSILON);
401 }
402 }
403
404 #[test]
405 fn test_tokyo_night_themes() {
406 let tokyo_night = iced::Theme::TokyoNight;
408 let style = from_iced_theme(&tokyo_night);
409 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
410
411 let tokyo_storm = iced::Theme::TokyoNightStorm;
412 let style = from_iced_theme(&tokyo_storm);
413 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
414
415 let tokyo_light = iced::Theme::TokyoNightLight;
416 let style = from_iced_theme(&tokyo_light);
417 let brightness =
418 (style.background.r + style.background.g + style.background.b)
419 / 3.0;
420 assert!(
421 brightness > 0.5,
422 "Tokyo Night Light should have bright background"
423 );
424 }
425
426 #[test]
427 fn test_catppuccin_themes() {
428 let themes = [
430 iced::Theme::CatppuccinLatte,
431 iced::Theme::CatppuccinFrappe,
432 iced::Theme::CatppuccinMacchiato,
433 iced::Theme::CatppuccinMocha,
434 ];
435
436 for theme in themes {
437 let style = from_iced_theme(&theme);
438 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
440 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
441 }
442 }
443
444 #[test]
445 fn test_gutter_colors_distinct_from_background() {
446 let theme = iced::Theme::Dark;
447 let style = from_iced_theme(&theme);
448
449 let gutter_diff = (style.gutter_background.r - style.background.r)
451 .abs()
452 + (style.gutter_background.g - style.background.g).abs()
453 + (style.gutter_background.b - style.background.b).abs();
454
455 assert!(
456 gutter_diff > 0.0,
457 "Gutter should be visually distinct from background"
458 );
459 }
460
461 #[test]
462 fn test_line_numbers_visible_but_subtle() {
463 for theme in [iced::Theme::Dark, iced::Theme::Light] {
464 let style = from_iced_theme(&theme);
465 let palette = theme.extended_palette();
466
467 let line_num_brightness = (style.line_number_color.r
469 + style.line_number_color.g
470 + style.line_number_color.b)
471 / 3.0;
472
473 let text_brightness =
474 (style.text_color.r + style.text_color.g + style.text_color.b)
475 / 3.0;
476
477 let bg_brightness =
478 (style.background.r + style.background.g + style.background.b)
479 / 3.0;
480
481 if palette.is_dark {
485 assert!(
487 line_num_brightness < text_brightness,
488 "Dark theme line numbers should be dimmer than text. Line num: {}, Text: {}",
489 line_num_brightness,
490 text_brightness
491 );
492 } else {
493 assert!(
495 line_num_brightness > text_brightness
496 && line_num_brightness < bg_brightness,
497 "Light theme line numbers should be between text and background. Text: {}, Line num: {}, Bg: {}",
498 text_brightness,
499 line_num_brightness,
500 bg_brightness
501 );
502 }
503 }
504 }
505
506 #[test]
507 fn test_color_helper_functions() {
508 let color = Color::from_rgb(0.5, 0.5, 0.5);
509
510 let darker = darken(color, 0.5);
512 assert!(darker.r < color.r);
513 assert!(darker.g < color.g);
514 assert!(darker.b < color.b);
515
516 let lighter = lighten(color, 0.5);
518 assert!(lighter.r > color.r);
519 assert!(lighter.g > color.g);
520 assert!(lighter.b > color.b);
521
522 let dimmed = dim_color(color, 0.5);
524 assert!(dimmed.r < color.r);
525
526 let transparent = with_alpha(color, 0.3);
528 assert!((transparent.a - 0.3).abs() < f32::EPSILON);
529 assert!((transparent.r - color.r).abs() < f32::EPSILON);
530 }
531
532 #[test]
533 fn test_style_copy() {
534 let theme = iced::Theme::Dark;
535 let style1 = from_iced_theme(&theme);
536 let style2 = style1;
537
538 assert!(
540 (style1.background.r - style2.background.r).abs() < f32::EPSILON
541 );
542 assert!(
543 (style1.text_color.r - style2.text_color.r).abs() < f32::EPSILON
544 );
545 assert!(
546 (style1.gutter_background.r - style2.gutter_background.r).abs()
547 < f32::EPSILON
548 );
549 assert!(
550 (style1.selection_color.r - style2.selection_color.r).abs()
551 < f32::EPSILON
552 );
553 }
554
555 #[test]
556 fn test_catalog_default() {
557 let theme = iced::Theme::Dark;
558 let class = <iced::Theme as Catalog>::default();
559 let style = theme.style(&class);
560
561 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
563 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
564 }
565}