elicit_bevy 0.11.1

Elicitation-enabled Bevy ECS tools for code generation and game development
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
//! Bevy color shadow types.
//!
//! Covers `Color` (enum) and all inner color-space structs.

use elicitation::{elicit_newtype, elicit_newtype_traits};
use elicitation_derive::reflect_methods;
use std::sync::Arc;

/// Shared boilerplate for all plain color-space newtypes (all Copy, PartialEq, no Hash).
macro_rules! color_space_newtype {
    ($name:ident, $upstream:path) => {
        elicit_newtype!($upstream, as $name);
        // f32 fields → PartialEq only; no Hash
        elicit_newtype_traits!($name, $upstream, [eq]);

        impl serde::Serialize for $name {
            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                (*self.0).serialize(s)
            }
        }
        impl<'de> serde::Deserialize<'de> for $name {
            fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                <$upstream>::deserialize(d).map(|v| $name(Arc::new(v)))
            }
        }
        /// Unwrap to bevy type; all color-space structs are `Copy`.
        impl From<$name> for $upstream {
            fn from(v: $name) -> Self {
                *v.0
            }
        }
        impl elicitation::ElicitComplete for $name {}
    };
}

// ── Color spaces ──────────────────────────────────────────────────────────────

color_space_newtype!(Srgba, bevy::color::Srgba);

#[reflect_methods]
impl Srgba {
    /// Red channel `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn srgba_red(&self) -> f32 {
        self.0.red
    }

    /// Green channel `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn srgba_green(&self) -> f32 {
        self.0.green
    }

    /// Blue channel `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn srgba_blue(&self) -> f32 {
        self.0.blue
    }

    /// Alpha channel `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn srgba_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with the red channel set to `red`.
    #[tracing::instrument(skip(self))]
    pub fn srgba_with_red(&self, red: f32) -> Srgba {
        Srgba::from((*self.0).with_red(red))
    }

    /// Returns a copy with the green channel set to `green`.
    #[tracing::instrument(skip(self))]
    pub fn srgba_with_green(&self, green: f32) -> Srgba {
        Srgba::from((*self.0).with_green(green))
    }

    /// Returns a copy with the blue channel set to `blue`.
    #[tracing::instrument(skip(self))]
    pub fn srgba_with_blue(&self, blue: f32) -> Srgba {
        Srgba::from((*self.0).with_blue(blue))
    }

    /// Returns a copy with the alpha channel set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn srgba_with_alpha(&self, alpha: f32) -> Srgba {
        use bevy::color::Alpha;
        Srgba::from(self.0.with_alpha(alpha))
    }

    /// Returns the RGBA components as `[R, G, B, A]` bytes.
    #[tracing::instrument(skip(self))]
    pub fn to_u8_array(&self) -> Vec<u8> {
        use bevy::color::ColorToPacked;
        (*self.0).to_u8_array().to_vec()
    }

    /// Returns the RGBA components as `[R, G, B, A]` floats.
    #[tracing::instrument(skip(self))]
    pub fn to_f32_array(&self) -> Vec<f32> {
        use bevy::color::ColorToComponents;
        (*self.0).to_f32_array().to_vec()
    }

    /// Returns the CSS hex representation, e.g. `"#FF8800"`.
    #[tracing::instrument(skip(self))]
    pub fn to_hex(&self) -> String {
        self.0.to_hex()
    }

    /// Parses an `Srgba` from a CSS hex string. The `&self` receiver is unused.
    #[tracing::instrument(skip(self))]
    pub fn from_hex(&self, hex: &str) -> Option<Srgba> {
        bevy::color::Srgba::hex(hex).ok().map(Srgba::from)
    }

    /// Constructs from r, g, b, a (ignores self).
    #[tracing::instrument(skip(self))]
    pub fn srgba_new(&self, r: f32, g: f32, b: f32, a: f32) -> Srgba {
        Srgba::from(bevy::color::Srgba {
            red: r,
            green: g,
            blue: b,
            alpha: a,
        })
    }

    /// Returns `true` if the color is fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn srgba_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if the color is fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn srgba_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor` in sRGB space.
    #[tracing::instrument(skip(self))]
    pub fn srgba_lerp(&self, other: Srgba, factor: f32) -> Srgba {
        use bevy::color::Mix;
        Srgba::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_srgba {
    use super::Srgba;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Srgba {
        fn to_code_literal(&self) -> TokenStream {
            let (r, g, b, a) = (self.0.red, self.0.green, self.0.blue, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::Srgba::from(::bevy::color::Srgba {
                    red: #r, green: #g, blue: #b, alpha: #a,
                })
            }
        }
    }
}

color_space_newtype!(LinearRgba, bevy::color::LinearRgba);

#[reflect_methods]
impl LinearRgba {
    /// Red channel.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_red(&self) -> f32 {
        self.0.red
    }

    /// Green channel.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_green(&self) -> f32 {
        self.0.green
    }

    /// Blue channel.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_blue(&self) -> f32 {
        self.0.blue
    }

    /// Alpha channel.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with the red channel set to `red`.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_with_red(&self, red: f32) -> LinearRgba {
        LinearRgba::from((*self.0).with_red(red))
    }

    /// Returns a copy with the green channel set to `green`.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_with_green(&self, green: f32) -> LinearRgba {
        LinearRgba::from((*self.0).with_green(green))
    }

    /// Returns a copy with the blue channel set to `blue`.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_with_blue(&self, blue: f32) -> LinearRgba {
        LinearRgba::from((*self.0).with_blue(blue))
    }

    /// Returns a copy with the alpha channel set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_with_alpha(&self, alpha: f32) -> LinearRgba {
        use bevy::color::Alpha;
        LinearRgba::from(self.0.with_alpha(alpha))
    }

    /// Perceptual luminance (weighted sum of linear RGB).
    #[tracing::instrument(skip(self))]
    pub fn luminance(&self) -> f32 {
        use bevy::color::Luminance;
        self.0.luminance()
    }

    /// Constructs from r, g, b, a (ignores self).
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_new(&self, r: f32, g: f32, b: f32, a: f32) -> LinearRgba {
        LinearRgba::from(bevy::color::LinearRgba {
            red: r,
            green: g,
            blue: b,
            alpha: a,
        })
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn linear_rgba_lerp(&self, other: LinearRgba, factor: f32) -> LinearRgba {
        use bevy::color::Mix;
        LinearRgba::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_lrgba {
    use super::LinearRgba;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for LinearRgba {
        fn to_code_literal(&self) -> TokenStream {
            let (r, g, b, a) = (self.0.red, self.0.green, self.0.blue, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::LinearRgba::from(::bevy::color::LinearRgba {
                    red: #r, green: #g, blue: #b, alpha: #a,
                })
            }
        }
    }
}

color_space_newtype!(Hsla, bevy::color::Hsla);

#[reflect_methods]
impl Hsla {
    /// Hue `[0, 360)`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_hue(&self) -> f32 {
        self.0.hue
    }

    /// Saturation `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_saturation(&self) -> f32 {
        self.0.saturation
    }

    /// Lightness `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_lightness(&self) -> f32 {
        self.0.lightness
    }

    /// Alpha `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with hue set to `hue` degrees.
    #[tracing::instrument(skip(self))]
    pub fn hsla_with_hue(&self, hue: f32) -> Hsla {
        use bevy::color::Hue;
        Hsla::from(self.0.with_hue(hue))
    }

    /// Returns a copy with saturation set to `saturation`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_with_saturation(&self, saturation: f32) -> Hsla {
        Hsla::from((*self.0).with_saturation(saturation))
    }

    /// Returns a copy with lightness set to `lightness`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_with_lightness(&self, lightness: f32) -> Hsla {
        Hsla::from((*self.0).with_lightness(lightness))
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_with_alpha(&self, alpha: f32) -> Hsla {
        use bevy::color::Alpha;
        Hsla::from(self.0.with_alpha(alpha))
    }

    /// Rotates the hue by `degrees`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_rotate_hue(&self, degrees: f32) -> Hsla {
        use bevy::color::Hue;
        Hsla::from((*self.0).rotate_hue(degrees))
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn hsla_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn hsla_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn hsla_lerp(&self, other: Hsla, factor: f32) -> Hsla {
        use bevy::color::Mix;
        Hsla::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_hsla {
    use super::Hsla;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Hsla {
        fn to_code_literal(&self) -> TokenStream {
            let (h, s, l, a) = (
                self.0.hue,
                self.0.saturation,
                self.0.lightness,
                self.0.alpha,
            );
            quote::quote! {
                ::elicit_bevy::Hsla::from(::bevy::color::Hsla {
                    hue: #h, saturation: #s, lightness: #l, alpha: #a,
                })
            }
        }
    }
}

color_space_newtype!(Hsva, bevy::color::Hsva);

#[reflect_methods]
impl Hsva {
    /// Hue.
    #[tracing::instrument(skip(self))]
    pub fn hsva_hue(&self) -> f32 {
        self.0.hue
    }

    /// Saturation.
    #[tracing::instrument(skip(self))]
    pub fn hsva_saturation(&self) -> f32 {
        self.0.saturation
    }

    /// Value (brightness) `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn value(&self) -> f32 {
        self.0.value
    }

    /// Alpha.
    #[tracing::instrument(skip(self))]
    pub fn hsva_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with hue set to `hue` degrees.
    #[tracing::instrument(skip(self))]
    pub fn hsva_with_hue(&self, hue: f32) -> Hsva {
        use bevy::color::Hue;
        Hsva::from(self.0.with_hue(hue))
    }

    /// Returns a copy with saturation set to `saturation`.
    #[tracing::instrument(skip(self))]
    pub fn hsva_with_saturation(&self, saturation: f32) -> Hsva {
        Hsva::from((*self.0).with_saturation(saturation))
    }

    /// Returns a copy with value set to `value`.
    #[tracing::instrument(skip(self))]
    pub fn with_value(&self, value: f32) -> Hsva {
        Hsva::from((*self.0).with_value(value))
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn hsva_with_alpha(&self, alpha: f32) -> Hsva {
        use bevy::color::Alpha;
        Hsva::from(self.0.with_alpha(alpha))
    }

    /// Rotates the hue by `degrees`.
    #[tracing::instrument(skip(self))]
    pub fn hsva_rotate_hue(&self, degrees: f32) -> Hsva {
        use bevy::color::Hue;
        Hsva::from((*self.0).rotate_hue(degrees))
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn hsva_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn hsva_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn hsva_lerp(&self, other: Hsva, factor: f32) -> Hsva {
        use bevy::color::Mix;
        Hsva::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_hsva {
    use super::Hsva;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Hsva {
        fn to_code_literal(&self) -> TokenStream {
            let (h, s, v, a) = (self.0.hue, self.0.saturation, self.0.value, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::Hsva::from(::bevy::color::Hsva {
                    hue: #h, saturation: #s, value: #v, alpha: #a,
                })
            }
        }
    }
}

color_space_newtype!(Hwba, bevy::color::Hwba);

#[reflect_methods]
impl Hwba {
    /// Hue.
    #[tracing::instrument(skip(self))]
    pub fn hwba_hue(&self) -> f32 {
        self.0.hue
    }

    /// Whiteness `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn whiteness(&self) -> f32 {
        self.0.whiteness
    }

    /// Blackness `[0, 1]`.
    #[tracing::instrument(skip(self))]
    pub fn blackness(&self) -> f32 {
        self.0.blackness
    }

    /// Alpha.
    #[tracing::instrument(skip(self))]
    pub fn hwba_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with hue set to `hue` degrees.
    #[tracing::instrument(skip(self))]
    pub fn hwba_with_hue(&self, hue: f32) -> Hwba {
        use bevy::color::Hue;
        Hwba::from(self.0.with_hue(hue))
    }

    /// Returns a copy with whiteness set to `whiteness`.
    #[tracing::instrument(skip(self))]
    pub fn with_whiteness(&self, whiteness: f32) -> Hwba {
        Hwba::from((*self.0).with_whiteness(whiteness))
    }

    /// Returns a copy with blackness set to `blackness`.
    #[tracing::instrument(skip(self))]
    pub fn with_blackness(&self, blackness: f32) -> Hwba {
        Hwba::from((*self.0).with_blackness(blackness))
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn hwba_with_alpha(&self, alpha: f32) -> Hwba {
        use bevy::color::Alpha;
        Hwba::from(self.0.with_alpha(alpha))
    }

    /// Rotates the hue by `degrees`.
    #[tracing::instrument(skip(self))]
    pub fn hwba_rotate_hue(&self, degrees: f32) -> Hwba {
        use bevy::color::Hue;
        Hwba::from((*self.0).rotate_hue(degrees))
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn hwba_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn hwba_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn hwba_lerp(&self, other: Hwba, factor: f32) -> Hwba {
        use bevy::color::Mix;
        Hwba::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_hwba {
    use super::Hwba;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Hwba {
        fn to_code_literal(&self) -> TokenStream {
            let (h, w, b, a) = (self.0.hue, self.0.whiteness, self.0.blackness, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::Hwba::from(::bevy::color::Hwba {
                    hue: #h, whiteness: #w, blackness: #b, alpha: #a,
                })
            }
        }
    }
}

color_space_newtype!(Laba, bevy::color::Laba);

#[reflect_methods]
impl Laba {
    /// Lightness L*.
    #[tracing::instrument(skip(self))]
    pub fn laba_lightness(&self) -> f32 {
        self.0.lightness
    }

    /// a* axis (green-red).
    #[tracing::instrument(skip(self))]
    pub fn laba_a(&self) -> f32 {
        self.0.a
    }

    /// b* axis (blue-yellow).
    #[tracing::instrument(skip(self))]
    pub fn laba_b(&self) -> f32 {
        self.0.b
    }

    /// Alpha.
    #[tracing::instrument(skip(self))]
    pub fn laba_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with lightness set to `lightness`.
    #[tracing::instrument(skip(self))]
    pub fn laba_with_lightness(&self, lightness: f32) -> Laba {
        Laba::from((*self.0).with_lightness(lightness))
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn laba_with_alpha(&self, alpha: f32) -> Laba {
        use bevy::color::Alpha;
        Laba::from(self.0.with_alpha(alpha))
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn laba_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn laba_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn laba_lerp(&self, other: Laba, factor: f32) -> Laba {
        use bevy::color::Mix;
        Laba::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_laba {
    use super::Laba;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Laba {
        fn to_code_literal(&self) -> TokenStream {
            let (l, a, b, al) = (self.0.lightness, self.0.a, self.0.b, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::Laba::from(::bevy::color::Laba {
                    lightness: #l, a: #a, b: #b, alpha: #al,
                })
            }
        }
    }
}

color_space_newtype!(Lcha, bevy::color::Lcha);

#[reflect_methods]
impl Lcha {
    /// Lightness.
    #[tracing::instrument(skip(self))]
    pub fn lcha_lightness(&self) -> f32 {
        self.0.lightness
    }

    /// Chroma.
    #[tracing::instrument(skip(self))]
    pub fn lcha_chroma(&self) -> f32 {
        self.0.chroma
    }

    /// Hue in degrees.
    #[tracing::instrument(skip(self))]
    pub fn lcha_hue(&self) -> f32 {
        self.0.hue
    }

    /// Alpha.
    #[tracing::instrument(skip(self))]
    pub fn lcha_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with lightness set to `lightness`.
    #[tracing::instrument(skip(self))]
    pub fn lcha_with_lightness(&self, lightness: f32) -> Lcha {
        Lcha::from((*self.0).with_lightness(lightness))
    }

    /// Returns a copy with chroma set to `chroma`.
    #[tracing::instrument(skip(self))]
    pub fn lcha_with_chroma(&self, chroma: f32) -> Lcha {
        Lcha::from((*self.0).with_chroma(chroma))
    }

    /// Returns a copy with hue set to `hue` degrees.
    #[tracing::instrument(skip(self))]
    pub fn lcha_with_hue(&self, hue: f32) -> Lcha {
        use bevy::color::Hue;
        Lcha::from(self.0.with_hue(hue))
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn lcha_with_alpha(&self, alpha: f32) -> Lcha {
        use bevy::color::Alpha;
        Lcha::from(self.0.with_alpha(alpha))
    }

    /// Rotates the hue by `degrees`.
    #[tracing::instrument(skip(self))]
    pub fn lcha_rotate_hue(&self, degrees: f32) -> Lcha {
        use bevy::color::Hue;
        Lcha::from((*self.0).rotate_hue(degrees))
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn lcha_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn lcha_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn lcha_lerp(&self, other: Lcha, factor: f32) -> Lcha {
        use bevy::color::Mix;
        Lcha::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_lcha {
    use super::Lcha;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Lcha {
        fn to_code_literal(&self) -> TokenStream {
            let (l, c, h, a) = (self.0.lightness, self.0.chroma, self.0.hue, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::Lcha::from(::bevy::color::Lcha {
                    lightness: #l, chroma: #c, hue: #h, alpha: #a,
                })
            }
        }
    }
}

color_space_newtype!(Oklaba, bevy::color::Oklaba);

#[reflect_methods]
impl Oklaba {
    /// Lightness L.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_lightness(&self) -> f32 {
        self.0.lightness
    }

    /// a channel.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_a(&self) -> f32 {
        self.0.a
    }

    /// b channel.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_b(&self) -> f32 {
        self.0.b
    }

    /// Alpha.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with lightness set to `lightness`.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_with_lightness(&self, lightness: f32) -> Oklaba {
        Oklaba::from((*self.0).with_lightness(lightness))
    }

    /// Returns a copy with the a channel set to `a`.
    #[tracing::instrument(skip(self))]
    pub fn with_a(&self, a: f32) -> Oklaba {
        Oklaba::from((*self.0).with_a(a))
    }

    /// Returns a copy with the b channel set to `b`.
    #[tracing::instrument(skip(self))]
    pub fn with_b(&self, b: f32) -> Oklaba {
        Oklaba::from((*self.0).with_b(b))
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_with_alpha(&self, alpha: f32) -> Oklaba {
        use bevy::color::Alpha;
        Oklaba::from(self.0.with_alpha(alpha))
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn oklaba_lerp(&self, other: Oklaba, factor: f32) -> Oklaba {
        use bevy::color::Mix;
        Oklaba::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_oklaba {
    use super::Oklaba;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Oklaba {
        fn to_code_literal(&self) -> TokenStream {
            let (l, a, b, al) = (self.0.lightness, self.0.a, self.0.b, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::Oklaba::from(::bevy::color::Oklaba {
                    lightness: #l, a: #a, b: #b, alpha: #al,
                })
            }
        }
    }
}

color_space_newtype!(Oklcha, bevy::color::Oklcha);

#[reflect_methods]
impl Oklcha {
    /// Lightness.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_lightness(&self) -> f32 {
        self.0.lightness
    }

    /// Chroma.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_chroma(&self) -> f32 {
        self.0.chroma
    }

    /// Hue in degrees.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_hue(&self) -> f32 {
        self.0.hue
    }

    /// Alpha.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with lightness set to `lightness`.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_with_lightness(&self, lightness: f32) -> Oklcha {
        Oklcha::from((*self.0).with_lightness(lightness))
    }

    /// Returns a copy with chroma set to `chroma`.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_with_chroma(&self, chroma: f32) -> Oklcha {
        Oklcha::from((*self.0).with_chroma(chroma))
    }

    /// Returns a copy with hue set to `hue` degrees.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_with_hue(&self, hue: f32) -> Oklcha {
        use bevy::color::Hue;
        Oklcha::from(self.0.with_hue(hue))
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_with_alpha(&self, alpha: f32) -> Oklcha {
        use bevy::color::Alpha;
        Oklcha::from(self.0.with_alpha(alpha))
    }

    /// Rotates the hue by `degrees`.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_rotate_hue(&self, degrees: f32) -> Oklcha {
        use bevy::color::Hue;
        Oklcha::from((*self.0).rotate_hue(degrees))
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn oklcha_lerp(&self, other: Oklcha, factor: f32) -> Oklcha {
        use bevy::color::Mix;
        Oklcha::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_oklcha {
    use super::Oklcha;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Oklcha {
        fn to_code_literal(&self) -> TokenStream {
            let (l, c, h, a) = (self.0.lightness, self.0.chroma, self.0.hue, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::Oklcha::from(::bevy::color::Oklcha {
                    lightness: #l, chroma: #c, hue: #h, alpha: #a,
                })
            }
        }
    }
}

color_space_newtype!(Xyza, bevy::color::Xyza);

#[reflect_methods]
impl Xyza {
    /// X component.
    #[tracing::instrument(skip(self))]
    pub fn x(&self) -> f32 {
        self.0.x
    }

    /// Y (luminance).
    #[tracing::instrument(skip(self))]
    pub fn y(&self) -> f32 {
        self.0.y
    }

    /// Z component.
    #[tracing::instrument(skip(self))]
    pub fn z(&self) -> f32 {
        self.0.z
    }

    /// Alpha.
    #[tracing::instrument(skip(self))]
    pub fn xyza_alpha(&self) -> f32 {
        self.0.alpha
    }

    /// Returns a copy with x set to `x`.
    #[tracing::instrument(skip(self))]
    pub fn with_x(&self, x: f32) -> Xyza {
        Xyza::from((*self.0).with_x(x))
    }

    /// Returns a copy with y set to `y`.
    #[tracing::instrument(skip(self))]
    pub fn with_y(&self, y: f32) -> Xyza {
        Xyza::from((*self.0).with_y(y))
    }

    /// Returns a copy with z set to `z`.
    #[tracing::instrument(skip(self))]
    pub fn with_z(&self, z: f32) -> Xyza {
        Xyza::from((*self.0).with_z(z))
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn xyza_with_alpha(&self, alpha: f32) -> Xyza {
        use bevy::color::Alpha;
        Xyza::from(self.0.with_alpha(alpha))
    }

    /// Returns `true` if fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn xyza_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn xyza_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates towards `other` by `factor`.
    #[tracing::instrument(skip(self))]
    pub fn xyza_lerp(&self, other: Xyza, factor: f32) -> Xyza {
        use bevy::color::Mix;
        Xyza::from((*self.0).mix(&*other.0, factor))
    }
}

mod emit_xyza {
    use super::Xyza;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Xyza {
        fn to_code_literal(&self) -> TokenStream {
            let (x, y, z, a) = (self.0.x, self.0.y, self.0.z, self.0.alpha);
            quote::quote! {
                ::elicit_bevy::Xyza::from(::bevy::color::Xyza {
                    x: #x, y: #y, z: #z, alpha: #a,
                })
            }
        }
    }
}

// ── Color (enum) ──────────────────────────────────────────────────────────────

elicit_newtype!(bevy::color::Color, as Color);
// Color is Copy and has PartialEq but no Hash.
elicit_newtype_traits!(Color, bevy::color::Color, [eq]);

impl serde::Serialize for Color {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        (*self.0).serialize(s)
    }
}

impl<'de> serde::Deserialize<'de> for Color {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        bevy::color::Color::deserialize(d).map(|v| Color(Arc::new(v)))
    }
}

/// `Color` is `Copy`, so unwrap via deref.
impl From<Color> for bevy::color::Color {
    fn from(v: Color) -> Self {
        *v.0
    }
}

#[reflect_methods]
impl Color {
    /// Converts to sRGBA color space.
    #[tracing::instrument(skip(self))]
    pub fn to_srgba(&self) -> Srgba {
        Srgba::from(self.0.to_srgba())
    }

    /// Converts to linear RGB color space.
    #[tracing::instrument(skip(self))]
    pub fn to_linear(&self) -> LinearRgba {
        LinearRgba::from(self.0.to_linear())
    }

    /// Converts to HSL color space.
    #[tracing::instrument(skip(self))]
    pub fn to_hsla(&self) -> Hsla {
        Hsla::from(bevy::color::Hsla::from(*self.0))
    }

    /// Converts to Oklab color space.
    #[tracing::instrument(skip(self))]
    pub fn to_oklaba(&self) -> Oklaba {
        Oklaba::from(bevy::color::Oklaba::from(*self.0))
    }

    /// Alpha component.
    #[tracing::instrument(skip(self))]
    pub fn color_alpha(&self) -> f32 {
        use bevy::color::Alpha;
        self.0.alpha()
    }

    /// Returns a copy with alpha set to `alpha`.
    #[tracing::instrument(skip(self))]
    pub fn color_with_alpha(&self, alpha: f32) -> Color {
        use bevy::color::Alpha;
        Color::from(self.0.with_alpha(alpha))
    }

    /// Returns `true` if the color is fully transparent.
    #[tracing::instrument(skip(self))]
    pub fn color_is_fully_transparent(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_transparent()
    }

    /// Returns `true` if the color is fully opaque.
    #[tracing::instrument(skip(self))]
    pub fn color_is_fully_opaque(&self) -> bool {
        use bevy::color::Alpha;
        self.0.is_fully_opaque()
    }

    /// Linearly interpolates between `self` and `end` in linear RGB space.
    #[tracing::instrument(skip(self))]
    pub fn linear_slow_transition(&self, end: Color, f: f32) -> Color {
        use bevy::color::Mix;
        Color::from((*self.0).mix(&*end.0, f))
    }

    /// Variant name, e.g. `"Srgba"`, `"LinearRgba"`.
    #[tracing::instrument(skip(self))]
    pub fn variant_name(&self) -> &'static str {
        match *self.0 {
            bevy::color::Color::Srgba(_) => "Srgba",
            bevy::color::Color::LinearRgba(_) => "LinearRgba",
            bevy::color::Color::Hsla(_) => "Hsla",
            bevy::color::Color::Hsva(_) => "Hsva",
            bevy::color::Color::Hwba(_) => "Hwba",
            bevy::color::Color::Laba(_) => "Laba",
            bevy::color::Color::Lcha(_) => "Lcha",
            bevy::color::Color::Oklaba(_) => "Oklaba",
            bevy::color::Color::Oklcha(_) => "Oklcha",
            bevy::color::Color::Xyza(_) => "Xyza",
        }
    }

    /// Returns `true` if this color is stored in the `Srgba` variant.
    #[tracing::instrument(skip(self))]
    pub fn is_srgba(&self) -> bool {
        matches!(*self.0, bevy::color::Color::Srgba(_))
    }

    /// Returns `true` if this color is stored in the `LinearRgba` variant.
    #[tracing::instrument(skip(self))]
    pub fn is_linear_rgba(&self) -> bool {
        matches!(*self.0, bevy::color::Color::LinearRgba(_))
    }

    /// Returns `true` if this color is stored in the `Hsla` variant.
    #[tracing::instrument(skip(self))]
    pub fn is_hsla(&self) -> bool {
        matches!(*self.0, bevy::color::Color::Hsla(_))
    }

    /// Constructs `Color::WHITE` (ignores self).
    #[tracing::instrument(skip(self))]
    pub fn white(&self) -> Color {
        Color::from(bevy::color::Color::WHITE)
    }

    /// Constructs `Color::BLACK` (ignores self).
    #[tracing::instrument(skip(self))]
    pub fn black(&self) -> Color {
        Color::from(bevy::color::Color::BLACK)
    }

    /// Constructs fully transparent `Color::NONE` (ignores self).
    #[tracing::instrument(skip(self))]
    pub fn none(&self) -> Color {
        Color::from(bevy::color::Color::NONE)
    }

    /// Constructs from sRGB r, g, b with alpha = 1.0 (ignores self).
    #[tracing::instrument(skip(self))]
    pub fn srgb(&self, r: f32, g: f32, b: f32) -> Color {
        Color::from(bevy::color::Color::srgb(r, g, b))
    }

    /// Constructs from a CSS hex string (ignores self).
    #[tracing::instrument(skip(self))]
    pub fn srgb_from_hex(&self, hex: &str) -> Option<Color> {
        bevy::color::Srgba::hex(hex)
            .ok()
            .map(|c| Color::from(bevy::color::Color::Srgba(c)))
    }
}

mod emit_color {
    use super::Color;
    use elicitation::emit_code::ToCodeLiteral;
    use proc_macro2::TokenStream;
    impl ToCodeLiteral for Color {
        fn to_code_literal(&self) -> TokenStream {
            let json = serde_json::to_string(&**self).unwrap_or_default();
            quote::quote! {
                ::elicit_bevy::Color::from(
                    ::serde_json::from_str::<::bevy::color::Color>(#json).unwrap()
                )
            }
        }
    }
}

impl elicitation::ElicitComplete for Color {}