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 indent_guide_color: Color,
49 pub search_match_color: Color,
51 pub search_match_current_color: Color,
53 pub selection_color: Color,
55 pub bracket_match_color: Color,
57 pub ime_preedit_background_color: Color,
59}
60
61pub trait Catalog {
81 type Class<'a>;
83
84 fn default<'a>() -> Self::Class<'a>;
86
87 fn style(&self, class: &Self::Class<'_>) -> Style;
89}
90
91pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme) -> Style + 'a>;
112
113impl Catalog for iced::Theme {
114 type Class<'a> = StyleFn<'a, Self>;
115
116 fn default<'a>() -> Self::Class<'a> {
117 Box::new(from_iced_theme)
118 }
119
120 fn style(&self, class: &Self::Class<'_>) -> Style {
121 class(self)
122 }
123}
124
125pub fn from_iced_theme(theme: &iced::Theme) -> Style {
162 let palette = theme.extended_palette();
163 let is_dark = palette.is_dark;
164
165 let background = palette.background.base.color;
167 let text_color = palette.background.base.text;
168
169 let gutter_background = palette.background.weak.color;
171 let gutter_border = if is_dark {
172 darken(palette.background.strong.color, 0.1)
173 } else {
174 lighten(palette.background.strong.color, 0.1)
175 };
176
177 let line_number_color = if is_dark {
181 dim_color(text_color, 0.5)
182 } else {
183 blend_colors(text_color, background, 0.5)
185 };
186
187 let scrollbar_background = background;
189 let scroller_color = palette.secondary.weak.color;
190
191 let current_line_highlight = with_alpha(
193 palette.primary.weak.color,
194 if is_dark { 0.15 } else { 0.25 },
195 );
196
197 let whitespace_color = if is_dark {
198 dim_color(text_color, 0.65)
199 } else {
200 blend_colors(text_color, background, 0.65)
201 };
202
203 let indent_guide_color =
206 with_alpha(text_color, if is_dark { 0.18 } else { 0.22 });
207
208 let search_match_color = Color { r: 1.0, g: 1.0, b: 0.0, a: 0.3 };
212 let search_match_current_color = Color { r: 1.0, g: 0.6, b: 0.0, a: 0.4 };
213
214 let selection_color = Color { r: 0.3, g: 0.5, b: 0.8, a: 0.3 };
216
217 let bracket_match_color = Color { r: 0.5, g: 0.5, b: 0.5, a: 0.4 };
219
220 let ime_preedit_background_color =
222 Color { r: 1.0, g: 1.0, b: 1.0, a: 0.08 };
223
224 Style {
225 background,
226 text_color,
227 gutter_background,
228 gutter_border,
229 line_number_color,
230 scrollbar_background,
231 scroller_color,
232 current_line_highlight,
233 whitespace_color,
234 indent_guide_color,
235 search_match_color,
236 search_match_current_color,
237 selection_color,
238 bracket_match_color,
239 ime_preedit_background_color,
240 }
241}
242
243fn darken(color: Color, factor: f32) -> Color {
245 Color {
246 r: color.r * (1.0 - factor),
247 g: color.g * (1.0 - factor),
248 b: color.b * (1.0 - factor),
249 a: color.a,
250 }
251}
252
253fn lighten(color: Color, factor: f32) -> Color {
255 Color {
256 r: color.r + (1.0 - color.r) * factor,
257 g: color.g + (1.0 - color.g) * factor,
258 b: color.b + (1.0 - color.b) * factor,
259 a: color.a,
260 }
261}
262
263fn dim_color(color: Color, factor: f32) -> Color {
265 Color {
266 r: color.r * factor,
267 g: color.g * factor,
268 b: color.b * factor,
269 a: color.a,
270 }
271}
272
273fn blend_colors(color1: Color, color2: Color, factor: f32) -> Color {
275 Color {
276 r: color1.r + (color2.r - color1.r) * factor,
277 g: color1.g + (color2.g - color1.g) * factor,
278 b: color1.b + (color2.b - color1.b) * factor,
279 a: color1.a + (color2.a - color1.a) * factor,
280 }
281}
282
283fn with_alpha(color: Color, alpha: f32) -> Color {
285 Color { r: color.r, g: color.g, b: color.b, a: alpha }
286}
287
288#[cfg(test)]
289mod tests {
290 use super::*;
291
292 #[test]
293 fn test_from_iced_theme_dark() {
294 let theme = iced::Theme::Dark;
295 let style = from_iced_theme(&theme);
296
297 let brightness =
299 (style.background.r + style.background.g + style.background.b)
300 / 3.0;
301 assert!(brightness < 0.5, "Dark theme should have dark background");
302
303 let text_brightness =
305 (style.text_color.r + style.text_color.g + style.text_color.b)
306 / 3.0;
307 assert!(text_brightness > 0.5, "Dark theme should have bright text");
308 }
309
310 #[test]
311 fn test_from_iced_theme_light() {
312 let theme = iced::Theme::Light;
313 let style = from_iced_theme(&theme);
314
315 let brightness =
317 (style.background.r + style.background.g + style.background.b)
318 / 3.0;
319 assert!(brightness > 0.5, "Light theme should have bright background");
320
321 let text_brightness =
323 (style.text_color.r + style.text_color.g + style.text_color.b)
324 / 3.0;
325 assert!(text_brightness < 0.5, "Light theme should have dark text");
326 }
327
328 #[test]
329 fn test_all_iced_themes_produce_valid_styles() {
330 for theme in iced::Theme::ALL {
332 let style = from_iced_theme(theme);
333
334 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
336 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
337 assert!(
338 style.gutter_background.r >= 0.0
339 && style.gutter_background.r <= 1.0
340 );
341 assert!(
342 style.line_number_color.r >= 0.0
343 && style.line_number_color.r <= 1.0
344 );
345
346 assert!(
348 style.current_line_highlight.a < 1.0,
349 "Current line highlight should be semi-transparent for theme: {:?}",
350 theme
351 );
352
353 for (name, color) in [
356 ("search_match_color", style.search_match_color),
357 (
358 "search_match_current_color",
359 style.search_match_current_color,
360 ),
361 ("selection_color", style.selection_color),
362 ("bracket_match_color", style.bracket_match_color),
363 (
364 "ime_preedit_background_color",
365 style.ime_preedit_background_color,
366 ),
367 ] {
368 assert!(
369 color.r >= 0.0 && color.r <= 1.0,
370 "{name}.r out of range for theme: {theme:?}"
371 );
372 assert!(
373 color.g >= 0.0 && color.g <= 1.0,
374 "{name}.g out of range for theme: {theme:?}"
375 );
376 assert!(
377 color.b >= 0.0 && color.b <= 1.0,
378 "{name}.b out of range for theme: {theme:?}"
379 );
380 assert!(
381 color.a > 0.0 && color.a < 1.0,
382 "{name} should be semi-transparent for theme: {theme:?}"
383 );
384 }
385 }
386 }
387
388 #[test]
389 fn test_overlay_colors_are_theme_independent() {
390 let light = from_iced_theme(&iced::Theme::Light);
393 let dark = from_iced_theme(&iced::Theme::Dark);
394
395 for (light_color, dark_color) in [
396 (light.search_match_color, dark.search_match_color),
397 (light.search_match_current_color, dark.search_match_current_color),
398 (light.selection_color, dark.selection_color),
399 (light.bracket_match_color, dark.bracket_match_color),
400 (
401 light.ime_preedit_background_color,
402 dark.ime_preedit_background_color,
403 ),
404 ] {
405 assert!((light_color.r - dark_color.r).abs() < f32::EPSILON);
406 assert!((light_color.g - dark_color.g).abs() < f32::EPSILON);
407 assert!((light_color.b - dark_color.b).abs() < f32::EPSILON);
408 assert!((light_color.a - dark_color.a).abs() < f32::EPSILON);
409 }
410 }
411
412 #[test]
413 fn test_tokyo_night_themes() {
414 let tokyo_night = iced::Theme::TokyoNight;
416 let style = from_iced_theme(&tokyo_night);
417 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
418
419 let tokyo_storm = iced::Theme::TokyoNightStorm;
420 let style = from_iced_theme(&tokyo_storm);
421 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
422
423 let tokyo_light = iced::Theme::TokyoNightLight;
424 let style = from_iced_theme(&tokyo_light);
425 let brightness =
426 (style.background.r + style.background.g + style.background.b)
427 / 3.0;
428 assert!(
429 brightness > 0.5,
430 "Tokyo Night Light should have bright background"
431 );
432 }
433
434 #[test]
435 fn test_catppuccin_themes() {
436 let themes = [
438 iced::Theme::CatppuccinLatte,
439 iced::Theme::CatppuccinFrappe,
440 iced::Theme::CatppuccinMacchiato,
441 iced::Theme::CatppuccinMocha,
442 ];
443
444 for theme in themes {
445 let style = from_iced_theme(&theme);
446 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
448 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
449 }
450 }
451
452 #[test]
453 fn test_gutter_colors_distinct_from_background() {
454 let theme = iced::Theme::Dark;
455 let style = from_iced_theme(&theme);
456
457 let gutter_diff = (style.gutter_background.r - style.background.r)
459 .abs()
460 + (style.gutter_background.g - style.background.g).abs()
461 + (style.gutter_background.b - style.background.b).abs();
462
463 assert!(
464 gutter_diff > 0.0,
465 "Gutter should be visually distinct from background"
466 );
467 }
468
469 #[test]
470 fn test_line_numbers_visible_but_subtle() {
471 for theme in [iced::Theme::Dark, iced::Theme::Light] {
472 let style = from_iced_theme(&theme);
473 let palette = theme.extended_palette();
474
475 let line_num_brightness = (style.line_number_color.r
477 + style.line_number_color.g
478 + style.line_number_color.b)
479 / 3.0;
480
481 let text_brightness =
482 (style.text_color.r + style.text_color.g + style.text_color.b)
483 / 3.0;
484
485 let bg_brightness =
486 (style.background.r + style.background.g + style.background.b)
487 / 3.0;
488
489 if palette.is_dark {
493 assert!(
495 line_num_brightness < text_brightness,
496 "Dark theme line numbers should be dimmer than text. Line num: {}, Text: {}",
497 line_num_brightness,
498 text_brightness
499 );
500 } else {
501 assert!(
503 line_num_brightness > text_brightness
504 && line_num_brightness < bg_brightness,
505 "Light theme line numbers should be between text and background. Text: {}, Line num: {}, Bg: {}",
506 text_brightness,
507 line_num_brightness,
508 bg_brightness
509 );
510 }
511 }
512 }
513
514 #[test]
515 fn test_color_helper_functions() {
516 let color = Color::from_rgb(0.5, 0.5, 0.5);
517
518 let darker = darken(color, 0.5);
520 assert!(darker.r < color.r);
521 assert!(darker.g < color.g);
522 assert!(darker.b < color.b);
523
524 let lighter = lighten(color, 0.5);
526 assert!(lighter.r > color.r);
527 assert!(lighter.g > color.g);
528 assert!(lighter.b > color.b);
529
530 let dimmed = dim_color(color, 0.5);
532 assert!(dimmed.r < color.r);
533
534 let transparent = with_alpha(color, 0.3);
536 assert!((transparent.a - 0.3).abs() < f32::EPSILON);
537 assert!((transparent.r - color.r).abs() < f32::EPSILON);
538 }
539
540 #[test]
541 fn test_style_copy() {
542 let theme = iced::Theme::Dark;
543 let style1 = from_iced_theme(&theme);
544 let style2 = style1;
545
546 assert!(
548 (style1.background.r - style2.background.r).abs() < f32::EPSILON
549 );
550 assert!(
551 (style1.text_color.r - style2.text_color.r).abs() < f32::EPSILON
552 );
553 assert!(
554 (style1.gutter_background.r - style2.gutter_background.r).abs()
555 < f32::EPSILON
556 );
557 assert!(
558 (style1.selection_color.r - style2.selection_color.r).abs()
559 < f32::EPSILON
560 );
561 }
562
563 #[test]
564 fn test_catalog_default() {
565 let theme = iced::Theme::Dark;
566 let class = <iced::Theme as Catalog>::default();
567 let style = theme.style(&class);
568
569 assert!(style.background.r >= 0.0 && style.background.r <= 1.0);
571 assert!(style.text_color.r >= 0.0 && style.text_color.r <= 1.0);
572 }
573}