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 line_number_active_color: Color,
41 pub scrollbar_background: Color,
43 pub scroller_color: Color,
45 pub current_line_highlight: Color,
47 pub whitespace_color: Color,
49 pub indent_guide_color: Color,
51 pub search_match_color: Color,
53 pub search_match_current_color: Color,
55 pub selection_color: Color,
57 pub bracket_match_color: Color,
59 pub ime_preedit_background_color: Color,
61}
62
63pub trait Catalog {
83 type Class<'a>;
85
86 fn default<'a>() -> Self::Class<'a>;
88
89 fn style(&self, class: &Self::Class<'_>) -> Style;
91}
92
93pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme) -> Style + 'a>;
114
115impl Catalog for iced::Theme {
116 type Class<'a> = StyleFn<'a, Self>;
117
118 fn default<'a>() -> Self::Class<'a> {
119 Box::new(from_iced_theme)
120 }
121
122 fn style(&self, class: &Self::Class<'_>) -> Style {
123 class(self)
124 }
125}
126
127pub fn from_iced_theme(theme: &iced::Theme) -> Style {
165 let palette = theme.extended_palette();
166 let is_dark = palette.is_dark;
167
168 let background = palette.background.base.color;
170 let text_color = palette.background.base.text;
171
172 let gutter_background = palette.background.weak.color;
174 let gutter_border = if is_dark {
175 darken(palette.background.strong.color, 0.1)
176 } else {
177 lighten(palette.background.strong.color, 0.1)
178 };
179
180 let line_number_color = if is_dark {
184 dim_color(text_color, 0.5)
185 } else {
186 blend_colors(text_color, background, 0.5)
188 };
189 let line_number_active_color = text_color;
191
192 let scrollbar_background = background;
194 let scroller_color = palette.secondary.weak.color;
195
196 let current_line_highlight = with_alpha(
198 palette.primary.weak.color,
199 if is_dark {
200 0.15
201 } else {
202 0.25
203 },
204 );
205
206 let whitespace_color = if is_dark {
207 dim_color(text_color, 0.65)
208 } else {
209 blend_colors(text_color, background, 0.65)
210 };
211
212 let indent_guide_color = with_alpha(
215 text_color,
216 if is_dark {
217 0.18
218 } else {
219 0.22
220 },
221 );
222
223 let search_match_color = Color {
227 r: 1.0,
228 g: 1.0,
229 b: 0.0,
230 a: 0.3,
231 };
232 let search_match_current_color = Color {
233 r: 1.0,
234 g: 0.6,
235 b: 0.0,
236 a: 0.4,
237 };
238
239 let selection_color = Color {
241 r: 0.3,
242 g: 0.5,
243 b: 0.8,
244 a: 0.3,
245 };
246
247 let bracket_match_color = Color {
249 r: 0.5,
250 g: 0.5,
251 b: 0.5,
252 a: 0.4,
253 };
254
255 let ime_preedit_background_color = Color {
257 r: 1.0,
258 g: 1.0,
259 b: 1.0,
260 a: 0.08,
261 };
262
263 Style {
264 background,
265 text_color,
266 gutter_background,
267 gutter_border,
268 line_number_color,
269 line_number_active_color,
270 scrollbar_background,
271 scroller_color,
272 current_line_highlight,
273 whitespace_color,
274 indent_guide_color,
275 search_match_color,
276 search_match_current_color,
277 selection_color,
278 bracket_match_color,
279 ime_preedit_background_color,
280 }
281}
282
283fn darken(color: Color, factor: f32) -> Color {
285 Color {
286 r: color.r * (1.0 - factor),
287 g: color.g * (1.0 - factor),
288 b: color.b * (1.0 - factor),
289 a: color.a,
290 }
291}
292
293fn lighten(color: Color, factor: f32) -> Color {
295 Color {
296 r: color.r + (1.0 - color.r) * factor,
297 g: color.g + (1.0 - color.g) * factor,
298 b: color.b + (1.0 - color.b) * factor,
299 a: color.a,
300 }
301}
302
303fn dim_color(color: Color, factor: f32) -> Color {
305 Color {
306 r: color.r * factor,
307 g: color.g * factor,
308 b: color.b * factor,
309 a: color.a,
310 }
311}
312
313fn blend_colors(color1: Color, color2: Color, factor: f32) -> Color {
315 Color {
316 r: color1.r + (color2.r - color1.r) * factor,
317 g: color1.g + (color2.g - color1.g) * factor,
318 b: color1.b + (color2.b - color1.b) * factor,
319 a: color1.a + (color2.a - color1.a) * factor,
320 }
321}
322
323fn with_alpha(color: Color, alpha: f32) -> Color {
325 Color {
326 r: color.r,
327 g: color.g,
328 b: color.b,
329 a: alpha,
330 }
331}
332
333#[cfg(test)]
334mod tests {
335 use super::*;
336
337 #[test]
338 fn test_from_iced_theme_dark() {
339 let theme = iced::Theme::Dark;
340 let style = from_iced_theme(&theme);
341
342 let brightness = (style.background.r + style.background.g + style.background.b) / 3.0;
344 assert!(brightness < 0.5, "Dark theme should have dark background");
345
346 let text_brightness = (style.text_color.r + style.text_color.g + style.text_color.b) / 3.0;
348 assert!(text_brightness > 0.5, "Dark theme should have bright text");
349 }
350
351 #[test]
352 fn test_from_iced_theme_light() {
353 let theme = iced::Theme::Light;
354 let style = from_iced_theme(&theme);
355
356 let brightness = (style.background.r + style.background.g + style.background.b) / 3.0;
358 assert!(
359 brightness > 0.5,
360 "Light theme should have bright background"
361 );
362
363 let text_brightness = (style.text_color.r + style.text_color.g + style.text_color.b) / 3.0;
365 assert!(text_brightness < 0.5, "Light theme should have dark text");
366 }
367
368 #[test]
369 fn test_all_iced_themes_produce_valid_styles() {
370 for theme in iced::Theme::ALL {
372 let style = from_iced_theme(theme);
373
374 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
376 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
377 assert!(style.gutter_background.r >= 0.0 && style.gutter_background.r <= 1.0);
378 assert!(style.line_number_color.r >= 0.0 && style.line_number_color.r <= 1.0);
379
380 assert!(
382 style.current_line_highlight.a < 1.0,
383 "Current line highlight should be semi-transparent for theme: {:?}",
384 theme
385 );
386
387 for (name, color) in [
390 ("search_match_color", style.search_match_color),
391 (
392 "search_match_current_color",
393 style.search_match_current_color,
394 ),
395 ("selection_color", style.selection_color),
396 ("bracket_match_color", style.bracket_match_color),
397 (
398 "ime_preedit_background_color",
399 style.ime_preedit_background_color,
400 ),
401 ] {
402 assert!(
403 color.r >= 0.0 && color.r <= 1.0,
404 "{name}.r out of range for theme: {theme:?}"
405 );
406 assert!(
407 color.g >= 0.0 && color.g <= 1.0,
408 "{name}.g out of range for theme: {theme:?}"
409 );
410 assert!(
411 color.b >= 0.0 && color.b <= 1.0,
412 "{name}.b out of range for theme: {theme:?}"
413 );
414 assert!(
415 color.a > 0.0 && color.a < 1.0,
416 "{name} should be semi-transparent for theme: {theme:?}"
417 );
418 }
419 }
420 }
421
422 #[test]
423 fn test_overlay_colors_are_theme_independent() {
424 let light = from_iced_theme(&iced::Theme::Light);
427 let dark = from_iced_theme(&iced::Theme::Dark);
428
429 for (light_color, dark_color) in [
430 (light.search_match_color, dark.search_match_color),
431 (
432 light.search_match_current_color,
433 dark.search_match_current_color,
434 ),
435 (light.selection_color, dark.selection_color),
436 (light.bracket_match_color, dark.bracket_match_color),
437 (
438 light.ime_preedit_background_color,
439 dark.ime_preedit_background_color,
440 ),
441 ] {
442 assert!((light_color.r - dark_color.r).abs() < f32::EPSILON);
443 assert!((light_color.g - dark_color.g).abs() < f32::EPSILON);
444 assert!((light_color.b - dark_color.b).abs() < f32::EPSILON);
445 assert!((light_color.a - dark_color.a).abs() < f32::EPSILON);
446 }
447 }
448
449 #[test]
450 fn test_tokyo_night_themes() {
451 let tokyo_night = iced::Theme::TokyoNight;
453 let style = from_iced_theme(&tokyo_night);
454 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
455
456 let tokyo_storm = iced::Theme::TokyoNightStorm;
457 let style = from_iced_theme(&tokyo_storm);
458 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
459
460 let tokyo_light = iced::Theme::TokyoNightLight;
461 let style = from_iced_theme(&tokyo_light);
462 let brightness = (style.background.r + style.background.g + style.background.b) / 3.0;
463 assert!(
464 brightness > 0.5,
465 "Tokyo Night Light should have bright background"
466 );
467 }
468
469 #[test]
470 fn test_catppuccin_themes() {
471 let themes = [
473 iced::Theme::CatppuccinLatte,
474 iced::Theme::CatppuccinFrappe,
475 iced::Theme::CatppuccinMacchiato,
476 iced::Theme::CatppuccinMocha,
477 ];
478
479 for theme in themes {
480 let style = from_iced_theme(&theme);
481 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
483 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
484 }
485 }
486
487 #[test]
488 fn test_gutter_colors_distinct_from_background() {
489 let theme = iced::Theme::Dark;
490 let style = from_iced_theme(&theme);
491
492 let gutter_diff = (style.gutter_background.r - style.background.r).abs()
494 + (style.gutter_background.g - style.background.g).abs()
495 + (style.gutter_background.b - style.background.b).abs();
496
497 assert!(
498 gutter_diff > 0.0,
499 "Gutter should be visually distinct from background"
500 );
501 }
502
503 #[test]
504 fn test_from_iced_theme_active_line_number_stands_out() {
505 for theme in [iced::Theme::Dark, iced::Theme::Light] {
506 let style = from_iced_theme(&theme);
507 let contrast = |c: Color| {
508 (c.r - style.background.r).abs()
509 + (c.g - style.background.g).abs()
510 + (c.b - style.background.b).abs()
511 };
512 assert!(
513 contrast(style.line_number_active_color) > contrast(style.line_number_color),
514 "active line number should contrast more with the background than inactive ones"
515 );
516 }
517 }
518
519 #[test]
520 fn test_line_numbers_visible_but_subtle() {
521 for theme in [iced::Theme::Dark, iced::Theme::Light] {
522 let style = from_iced_theme(&theme);
523 let palette = theme.extended_palette();
524
525 let line_num_brightness =
527 (style.line_number_color.r + style.line_number_color.g + style.line_number_color.b)
528 / 3.0;
529
530 let text_brightness =
531 (style.text_color.r + style.text_color.g + style.text_color.b) / 3.0;
532
533 let bg_brightness =
534 (style.background.r + style.background.g + style.background.b) / 3.0;
535
536 if palette.is_dark {
540 assert!(
542 line_num_brightness < text_brightness,
543 "Dark theme line numbers should be dimmer than text. Line num: {}, Text: {}",
544 line_num_brightness,
545 text_brightness
546 );
547 } else {
548 assert!(
550 line_num_brightness > text_brightness && line_num_brightness < bg_brightness,
551 "Light theme line numbers should be between text and background. Text: {}, Line num: {}, Bg: {}",
552 text_brightness,
553 line_num_brightness,
554 bg_brightness
555 );
556 }
557 }
558 }
559
560 #[test]
561 fn test_color_helper_functions() {
562 let color = Color::from_rgb(0.5, 0.5, 0.5);
563
564 let darker = darken(color, 0.5);
566 assert!(darker.r < color.r);
567 assert!(darker.g < color.g);
568 assert!(darker.b < color.b);
569
570 let lighter = lighten(color, 0.5);
572 assert!(lighter.r > color.r);
573 assert!(lighter.g > color.g);
574 assert!(lighter.b > color.b);
575
576 let dimmed = dim_color(color, 0.5);
578 assert!(dimmed.r < color.r);
579
580 let transparent = with_alpha(color, 0.3);
582 assert!((transparent.a - 0.3).abs() < f32::EPSILON);
583 assert!((transparent.r - color.r).abs() < f32::EPSILON);
584 }
585
586 #[test]
587 fn test_style_copy() {
588 let theme = iced::Theme::Dark;
589 let style1 = from_iced_theme(&theme);
590 let style2 = style1;
591
592 assert!((style1.background.r - style2.background.r).abs() < f32::EPSILON);
594 assert!((style1.text_color.r - style2.text_color.r).abs() < f32::EPSILON);
595 assert!((style1.gutter_background.r - style2.gutter_background.r).abs() < f32::EPSILON);
596 assert!((style1.selection_color.r - style2.selection_color.r).abs() < f32::EPSILON);
597 }
598
599 #[test]
600 fn test_catalog_default() {
601 let theme = iced::Theme::Dark;
602 let class = <iced::Theme as Catalog>::default();
603 let style = theme.style(&class);
604
605 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
607 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
608 }
609}