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
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
use bitflags::bitflags;
use std::ptr;

use crate::math::MintVec2;
use crate::math::MintVec3;
use crate::math::MintVec4;
use crate::sys;
use crate::Ui;

// /// Mutable reference to an editable color value.
// #[derive(Debug)]
// pub enum EditableColor<'a, T> {
//     /// Color value with three float components (e.g. RGB).
//     Float3(&'a mut T),
//     /// Color value with four float components (e.g. RGBA).
//     Float4(&'a mut T),
// }

// impl<'a> EditableColor<'a> {
//     /// Returns an unsafe mutable pointer to the color slice's buffer.
//     fn as_mut_ptr(&mut self) -> *mut f32 {
//         match *self {
//             EditableColor::Float3(ref mut value) => value.as_mut_ptr(),
//             EditableColor::Float4(ref mut value) => value.as_mut_ptr(),
//         }
//     }
// }

// impl<'a> From<&'a mut [f32; 3]> for EditableColor<'a> {
//     #[inline]
//     fn from(value: &'a mut [f32; 3]) -> EditableColor<'a> {
//         EditableColor::Float3(value)
//     }
// }

// impl<'a> From<&'a mut [f32; 4]> for EditableColor<'a> {
//     #[inline]
//     fn from(value: &'a mut [f32; 4]) -> EditableColor<'a> {
//         EditableColor::Float4(value)
//     }
// }

/// Color editor input mode.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorEditInputMode {
    /// Edit as RGB(A).
    Rgb,
    /// Edit as HSV(A).
    Hsv,
}

impl ColorEditInputMode {
    // Note: Probably no point in deprecating these since they're ~0 maintance burden.
    /// Edit as RGB(A). Alias for [`Self::Rgb`] for backwards-compatibility.
    pub const RGB: Self = Self::Rgb;
    /// Edit as HSV(A). Alias for [`Self::Hsv`] for backwards-compatibility.
    pub const HSV: Self = Self::Hsv;
}

/// Color editor display mode.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorEditDisplayMode {
    /// Display as RGB(A).
    Rgb,
    /// Display as HSV(A).
    Hsv,
    /// Display as hex (e.g. `#AABBCC(DD)`)
    Hex,
}

impl ColorEditDisplayMode {
    // Note: Probably no point in deprecating these since they're ~0 maintance burden.
    /// Display as RGB(A). Alias for [`Self::Rgb`] for backwards-compatibility.
    pub const RGB: Self = Self::Rgb;
    /// Display as HSV(A). Alias for [`Self::Hsv`] for backwards-compatibility.
    pub const HSV: Self = Self::Hsv;
    /// Display as hex. Alias for [`Self::Hex`] for backwards-compatibility.
    pub const HEX: Self = Self::Hex;
}

/// Color picker hue/saturation/value editor mode
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorPickerMode {
    /// Use a bar for hue, rectangle for saturation/value.
    HueBar,
    /// Use a wheel for hue, triangle for saturation/value.
    HueWheel,
}

/// Color component formatting
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorFormat {
    /// Display values formatted as 0..255.
    U8,
    /// Display values formatted as 0.0..1.0.
    Float,
}

/// Color editor preview style
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorPreview {
    /// Don't show the alpha component.
    Opaque,
    /// Half of the preview area shows the alpha component using a checkerboard pattern.
    HalfAlpha,
    /// Show the alpha component using a checkerboard pattern.
    Alpha,
}

bitflags! {
    /// Color edit flags
    #[repr(transparent)]
    pub struct ColorEditFlags: u32 {
        /// ColorEdit, ColorPicker, ColorButton: ignore Alpha component (read only 3 components of
        /// the value).
        const NO_ALPHA = sys::ImGuiColorEditFlags_NoAlpha;
        /// ColorEdit: disable picker when clicking on colored square.
        const NO_PICKER = sys::ImGuiColorEditFlags_NoPicker;
        /// ColorEdit: disable toggling options menu when right-clicking on inputs/small preview.
        const NO_OPTIONS = sys::ImGuiColorEditFlags_NoOptions;
        /// ColorEdit, ColorPicker: disable colored square preview next to the inputs. (e.g. to
        /// show only the inputs)
        const NO_SMALL_PREVIEW = sys::ImGuiColorEditFlags_NoSmallPreview;
        /// ColorEdit, ColorPicker: disable inputs sliders/text widgets (e.g. to show only the
        /// small preview colored square).
        const NO_INPUTS = sys::ImGuiColorEditFlags_NoInputs;
        /// ColorEdit, ColorPicker, ColorButton: disable tooltip when hovering the preview.
        const NO_TOOLTIP = sys::ImGuiColorEditFlags_NoTooltip;
        /// ColorEdit, ColorPicker: disable display of inline text label (the label is still
        /// forwarded to the tooltip and picker).
        const NO_LABEL = sys::ImGuiColorEditFlags_NoLabel;
        /// ColorPicker: disable bigger color preview on right side of the picker, use small
        /// colored square preview instead.
        const NO_SIDE_PREVIEW = sys::ImGuiColorEditFlags_NoSidePreview;
        /// ColorEdit: disable drag and drop target. ColorButton: disable drag and drop source.
        const NO_DRAG_DROP = sys::ImGuiColorEditFlags_NoDragDrop;
        /// ColorButton: disable border (which is enforced by default).
        const NO_BORDER = sys::ImGuiColorEditFlags_NoBorder;

        /// ColorEdit, ColorPicker: show vertical alpha bar/gradient in picker.
        const ALPHA_BAR = sys::ImGuiColorEditFlags_AlphaBar;
        /// ColorEdit, ColorPicker, ColorButton: display preview as a transparent color over a
        /// checkerboard, instead of opaque.
        const ALPHA_PREVIEW = sys::ImGuiColorEditFlags_AlphaPreview;
        /// ColorEdit, ColorPicker, ColorButton: display half opaque / half checkerboard, instead
        /// of opaque.
        const ALPHA_PREVIEW_HALF = sys::ImGuiColorEditFlags_AlphaPreviewHalf;
        /// (WIP) ColorEdit: Currently onlys disable 0.0f..1.0f limits in RGBA editing (note: you
        /// probably want to use `ColorEditFlags::FLOAT` as well).
        const HDR = sys::ImGuiColorEditFlags_HDR;
        /// ColorEdit: display only as RGB. ColorPicker: Enable RGB display.
        const DISPLAY_RGB = sys::ImGuiColorEditFlags_DisplayRGB;
        /// ColorEdit: display only as HSV. ColorPicker: Enable HSV display.
        const DISPLAY_HSV = sys::ImGuiColorEditFlags_DisplayHSV;
        /// ColorEdit: display only as HEX. ColorPicker: Enable Hex display.
        const DISPLAY_HEX = sys::ImGuiColorEditFlags_DisplayHex;
        /// ColorEdit, ColorPicker, ColorButton: _display_ values formatted as 0..255.
        const UINT8 = sys::ImGuiColorEditFlags_Uint8;
        /// ColorEdit, ColorPicker, ColorButton: _display_ values formatted as 0.0f..1.0f floats
        /// instead of 0..255 integers. No round-trip of value via integers.
        const FLOAT = sys::ImGuiColorEditFlags_Float;
        /// ColorPicker: bar for Hue, rectangle for Sat/Value.
        const PICKER_HUE_BAR = sys::ImGuiColorEditFlags_PickerHueBar;
        /// ColorPicker: wheel for Hue, triangle for Sat/Value.
        const PICKER_HUE_WHEEL = sys::ImGuiColorEditFlags_PickerHueWheel;
        /// ColorEdit, ColorPicker: input and output data in RGB format.
        const INPUT_RGB = sys::ImGuiColorEditFlags_InputRGB;
        /// ColorEdit, ColorPicker: input and output data in HSV format.
        const INPUT_HSV = sys::ImGuiColorEditFlags_InputHSV;
    }
}

/// Builder for a color editor widget.
///
/// # Examples
///
/// ```no_run
/// # use imgui::*;
/// # let mut imgui = Context::create();
/// # let ui = imgui.frame();
/// # let mut color = [0.0, 0.0, 0.0, 1.0];
/// if ui.color_edit4("color_edit", &mut color) {
///   println!("The color was changed");
/// }
/// ```
#[derive(Debug)]
#[must_use]
pub struct ColorEdit3<'ui, 'a, Label, C> {
    label: Label,
    value: &'a mut C,
    flags: ColorEditFlags,
    ui: &'ui Ui,
}

impl<'ui, 'a, Label, C> ColorEdit3<'ui, 'a, Label, C>
where
    Label: AsRef<str>,
    C: Copy + Into<MintVec3>,
    MintVec3: Into<C> + Into<[f32; 3]>,
{
    /// Constructs a new color editor builder.
    #[deprecated(since = "0.9.0", note = "Use `ui.color_edit3(...)` instead")]
    pub fn new(ui: &'ui Ui, label: Label, value: &'a mut C) -> Self {
        ColorEdit3 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui,
        }
    }
    /// Replaces all current settings with the given flags.
    #[inline]
    pub fn flags(mut self, flags: ColorEditFlags) -> Self {
        self.flags = flags;
        self
    }
    /// Enables/disables the use of the alpha component.
    #[inline]
    pub fn alpha(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_ALPHA, !value);
        self
    }
    /// Enables/disables the picker that appears when clicking on colored square.
    #[inline]
    pub fn picker(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_PICKER, !value);
        self
    }
    /// Enables/disables toggling of the options menu when right-clicking on inputs or the small
    /// preview.
    #[inline]
    pub fn options(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_OPTIONS, !value);
        self
    }
    /// Enables/disables the colored square preview next to the inputs.
    #[inline]
    pub fn small_preview(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_SMALL_PREVIEW, !value);
        self
    }
    /// Enables/disables the input sliders/text widgets.
    #[inline]
    pub fn inputs(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_INPUTS, !value);
        self
    }
    /// Enables/disables the tooltip that appears when hovering the preview.
    #[inline]
    pub fn tooltip(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_TOOLTIP, !value);
        self
    }
    /// Enables/disables display of the inline text label (the label is in any case forwarded to
    /// the tooltip and picker).
    #[inline]
    pub fn label(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_LABEL, !value);
        self
    }
    /// Enables/disables the vertical alpha bar/gradient in the color picker.
    #[inline]
    pub fn alpha_bar(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::ALPHA_BAR, value);
        self
    }
    /// Sets the preview style.
    #[inline]
    pub fn preview(mut self, preview: ColorPreview) -> Self {
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW_HALF,
            preview == ColorPreview::HalfAlpha,
        );
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW,
            preview == ColorPreview::Alpha,
        );
        self
    }
    /// (WIP) Currently only disables 0.0..1.0 limits in RGBA edition.
    ///
    /// Note: you probably want to use ColorFormat::Float as well.
    #[inline]
    pub fn hdr(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::HDR, value);
        self
    }
    /// Sets the data format for input and output data.
    #[inline]
    pub fn input_mode(mut self, input_mode: ColorEditInputMode) -> Self {
        self.flags.set(
            ColorEditFlags::INPUT_RGB,
            input_mode == ColorEditInputMode::RGB,
        );
        self.flags.set(
            ColorEditFlags::INPUT_HSV,
            input_mode == ColorEditInputMode::HSV,
        );
        self
    }
    /// Sets the color editor display mode.
    #[inline]
    pub fn display_mode(mut self, mode: ColorEditDisplayMode) -> Self {
        self.flags.set(
            ColorEditFlags::DISPLAY_RGB,
            mode == ColorEditDisplayMode::RGB,
        );
        self.flags.set(
            ColorEditFlags::DISPLAY_HSV,
            mode == ColorEditDisplayMode::HSV,
        );
        self.flags.set(
            ColorEditFlags::DISPLAY_HEX,
            mode == ColorEditDisplayMode::HEX,
        );
        self
    }
    /// Sets the formatting style of color components.
    #[inline]
    pub fn format(mut self, format: ColorFormat) -> Self {
        self.flags
            .set(ColorEditFlags::UINT8, format == ColorFormat::U8);
        self.flags
            .set(ColorEditFlags::FLOAT, format == ColorFormat::Float);
        self
    }
    /// Builds the color editor.
    ///
    /// Returns true if the color value was changed.
    pub fn build(mut self) -> bool {
        self.flags.insert(ColorEditFlags::NO_ALPHA);

        let as_vec3: MintVec3 = (*self.value).into();
        let mut as_vec3: [f32; 3] = as_vec3.into();

        let changed = unsafe {
            sys::igColorEdit3(
                self.ui.scratch_txt(self.label),
                as_vec3.as_mut_ptr(),
                self.flags.bits() as _,
            )
        };

        // and go backwards...
        if changed {
            let as_vec3: MintVec3 = as_vec3.into();

            *self.value = as_vec3.into();
        }

        changed
    }
}

impl Ui {
    /// Edits a color of 3 channels. Use [color_edit3_config](Self::color_edit3_config)
    /// for a builder to customize this widget.
    pub fn color_edit3<Label, C>(&self, label: Label, value: &mut C) -> bool
    where
        Label: AsRef<str>,
        C: Copy + Into<MintVec3>,
        MintVec3: Into<C> + Into<[f32; 3]>,
    {
        ColorEdit3 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui: self,
        }
        .build()
    }

    /// Constructs a new color editor builder.
    pub fn color_edit3_config<'a, Label, C>(
        &self,
        label: Label,
        value: &'a mut C,
    ) -> ColorEdit3<'_, 'a, Label, C>
    where
        Label: AsRef<str>,
        C: Copy + Into<MintVec3>,
        MintVec3: Into<C> + Into<[f32; 3]>,
    {
        ColorEdit3 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui: self,
        }
    }
}

/// Builder for a color editor widget.
///
/// # Examples
///
/// ```no_run
/// # use imgui::*;
/// # let mut imgui = Context::create();
/// # let ui = imgui.frame();
/// # let mut color = [0.0, 0.0, 0.0, 1.0];
/// if ui.color_edit4("color_edit", &mut color) {
///   println!("The color was changed");
/// }
/// ```
#[derive(Debug)]
#[must_use]
pub struct ColorEdit4<'ui, 'a, T, C> {
    label: T,
    value: &'a mut C,
    flags: ColorEditFlags,
    ui: &'ui Ui,
}

impl<'ui, 'a, T, C> ColorEdit4<'ui, 'a, T, C>
where
    T: AsRef<str>,
    C: Copy + Into<MintVec4>,
    MintVec4: Into<C> + Into<[f32; 4]>,
{
    /// Constructs a new color editor builder.
    #[deprecated(since = "0.9.0", note = "Use `ui.color_edit4(...)` instead")]
    pub fn new(ui: &'ui Ui, label: T, value: &'a mut C) -> Self {
        Self {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui,
        }
    }
    /// Replaces all current settings with the given flags.
    #[inline]
    pub fn flags(mut self, flags: ColorEditFlags) -> Self {
        self.flags = flags;
        self
    }
    /// Enables/disables the use of the alpha component.
    #[inline]
    pub fn alpha(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_ALPHA, !value);
        self
    }
    /// Enables/disables the picker that appears when clicking on colored square.
    #[inline]
    pub fn picker(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_PICKER, !value);
        self
    }
    /// Enables/disables toggling of the options menu when right-clicking on inputs or the small
    /// preview.
    #[inline]
    pub fn options(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_OPTIONS, !value);
        self
    }
    /// Enables/disables the colored square preview next to the inputs.
    #[inline]
    pub fn small_preview(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_SMALL_PREVIEW, !value);
        self
    }
    /// Enables/disables the input sliders/text widgets.
    #[inline]
    pub fn inputs(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_INPUTS, !value);
        self
    }
    /// Enables/disables the tooltip that appears when hovering the preview.
    #[inline]
    pub fn tooltip(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_TOOLTIP, !value);
        self
    }
    /// Enables/disables display of the inline text label (the label is in any case forwarded to
    /// the tooltip and picker).
    #[inline]
    pub fn label(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_LABEL, !value);
        self
    }
    /// Enables/disables the vertical alpha bar/gradient in the color picker.
    #[inline]
    pub fn alpha_bar(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::ALPHA_BAR, value);
        self
    }
    /// Sets the preview style.
    #[inline]
    pub fn preview(mut self, preview: ColorPreview) -> Self {
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW_HALF,
            preview == ColorPreview::HalfAlpha,
        );
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW,
            preview == ColorPreview::Alpha,
        );
        self
    }
    /// (WIP) Currently only disables 0.0..1.0 limits in RGBA edition.
    ///
    /// Note: you probably want to use ColorFormat::Float as well.
    #[inline]
    pub fn hdr(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::HDR, value);
        self
    }
    /// Sets the data format for input and output data.
    #[inline]
    pub fn input_mode(mut self, input_mode: ColorEditInputMode) -> Self {
        self.flags.set(
            ColorEditFlags::INPUT_RGB,
            input_mode == ColorEditInputMode::RGB,
        );
        self.flags.set(
            ColorEditFlags::INPUT_HSV,
            input_mode == ColorEditInputMode::HSV,
        );
        self
    }
    /// Sets the color editor display mode.
    #[inline]
    pub fn display_mode(mut self, mode: ColorEditDisplayMode) -> Self {
        self.flags.set(
            ColorEditFlags::DISPLAY_RGB,
            mode == ColorEditDisplayMode::RGB,
        );
        self.flags.set(
            ColorEditFlags::DISPLAY_HSV,
            mode == ColorEditDisplayMode::HSV,
        );
        self.flags.set(
            ColorEditFlags::DISPLAY_HEX,
            mode == ColorEditDisplayMode::HEX,
        );
        self
    }
    /// Sets the formatting style of color components.
    #[inline]
    pub fn format(mut self, format: ColorFormat) -> Self {
        self.flags
            .set(ColorEditFlags::UINT8, format == ColorFormat::U8);
        self.flags
            .set(ColorEditFlags::FLOAT, format == ColorFormat::Float);
        self
    }
    /// Builds the color editor.
    ///
    /// Returns true if the color value was changed.
    pub fn build(self) -> bool {
        let as_vec4: MintVec4 = (*self.value).into();
        let mut as_vec4: [f32; 4] = as_vec4.into();

        let changed = unsafe {
            sys::igColorEdit4(
                self.ui.scratch_txt(self.label),
                as_vec4.as_mut_ptr(),
                self.flags.bits() as _,
            )
        };

        // and go backwards...
        if changed {
            let as_vec4: MintVec4 = as_vec4.into();

            *self.value = as_vec4.into();
        }

        changed
    }
}

impl Ui {
    /// Edits a color of 4 channels. Use [color_edit4_config](Self::color_edit4_config)
    /// for a builder to customize this widget.
    pub fn color_edit4<Label, C>(&self, label: Label, value: &mut C) -> bool
    where
        Label: AsRef<str>,
        C: Copy + Into<MintVec4>,
        MintVec4: Into<C> + Into<[f32; 4]>,
    {
        ColorEdit4 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui: self,
        }
        .build()
    }

    /// Constructs a new color editor builder.
    pub fn color_edit4_config<'a, Label, C>(
        &self,
        label: Label,
        value: &'a mut C,
    ) -> ColorEdit4<'_, 'a, Label, C>
    where
        Label: AsRef<str>,
        C: Copy + Into<MintVec4>,
        MintVec4: Into<C> + Into<[f32; 4]>,
    {
        ColorEdit4 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui: self,
        }
    }
}

/// Builder for a color picker widget.
///
/// # Examples
///
/// ```no_run
/// # use imgui::*;
/// # let mut imgui = Context::create();
/// # let ui = imgui.frame();
/// # let mut color = [0.0, 0.0, 0.0, 1.0];
/// if ui.color_picker4("color_picker", &mut color) {
///   println!("A color was picked");
/// }
/// ```
#[derive(Debug)]
#[must_use]
pub struct ColorPicker3<'ui, 'a, Label, Color> {
    label: Label,
    value: &'a mut Color,
    flags: ColorEditFlags,
    ui: &'ui Ui,
}

impl<'ui, 'a, Label, Color> ColorPicker3<'ui, 'a, Label, Color>
where
    Label: AsRef<str>,
    Color: Copy + Into<MintVec3>,
    MintVec3: Into<Color> + Into<[f32; 3]>,
{
    /// Constructs a new color picker builder.
    #[deprecated(since = "0.9.0", note = "Use `ui.color_picker3(...)` instead")]
    pub fn new(ui: &'ui Ui, label: Label, value: &'a mut Color) -> Self {
        ColorPicker3 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui,
        }
    }
    /// Replaces all current settings with the given flags.
    #[inline]
    pub fn flags(mut self, flags: ColorEditFlags) -> Self {
        self.flags = flags;
        self
    }
    /// Enables/disables the use of the alpha component.
    #[inline]
    pub fn alpha(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_ALPHA, !value);
        self
    }
    /// Enables/disables toggling of the options menu when right-clicking on inputs or the small
    /// preview.
    #[inline]
    pub fn options(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_OPTIONS, !value);
        self
    }
    /// Enables/disables the colored square preview next to the inputs.
    #[inline]
    pub fn small_preview(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_SMALL_PREVIEW, !value);
        self
    }
    /// Enables/disables the input sliders/text widgets.
    #[inline]
    pub fn inputs(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_INPUTS, !value);
        self
    }
    /// Enables/disables the tooltip that appears when hovering the preview.
    #[inline]
    pub fn tooltip(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_TOOLTIP, !value);
        self
    }
    /// Enables/disables display of the inline text label (the label is in any case forwarded to
    /// the tooltip and picker).
    #[inline]
    pub fn label(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_LABEL, !value);
        self
    }
    /// Enables/disables the bigger color preview on the right side of the picker.
    #[inline]
    pub fn side_preview(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_SIDE_PREVIEW, !value);
        self
    }
    /// Enables/disables the vertical alpha bar/gradient in the color picker.
    #[inline]
    pub fn alpha_bar(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::ALPHA_BAR, value);
        self
    }
    /// Sets the preview style.
    #[inline]
    pub fn preview(mut self, preview: ColorPreview) -> Self {
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW_HALF,
            preview == ColorPreview::HalfAlpha,
        );
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW,
            preview == ColorPreview::Alpha,
        );
        self
    }
    /// Sets the data format for input and output data.
    #[inline]
    pub fn input_mode(mut self, input_mode: ColorEditInputMode) -> Self {
        self.flags.set(
            ColorEditFlags::INPUT_RGB,
            input_mode == ColorEditInputMode::RGB,
        );
        self.flags.set(
            ColorEditFlags::INPUT_HSV,
            input_mode == ColorEditInputMode::HSV,
        );
        self
    }
    /// Enables/disables displaying the value as RGB.
    #[inline]
    pub fn display_rgb(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::DISPLAY_RGB, value);
        self
    }
    /// Enables/disables displaying the value as HSV.
    #[inline]
    pub fn display_hsv(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::DISPLAY_HSV, value);
        self
    }
    /// Enables/disables displaying the value as hex.
    #[inline]
    pub fn display_hex(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::DISPLAY_HEX, value);
        self
    }
    /// Sets the hue/saturation/value editor mode.
    #[inline]
    pub fn mode(mut self, mode: ColorPickerMode) -> Self {
        self.flags.set(
            ColorEditFlags::PICKER_HUE_BAR,
            mode == ColorPickerMode::HueBar,
        );
        self.flags.set(
            ColorEditFlags::PICKER_HUE_WHEEL,
            mode == ColorPickerMode::HueWheel,
        );
        self
    }
    /// Sets the formatting style of color components.
    #[inline]
    pub fn format(mut self, format: ColorFormat) -> Self {
        self.flags
            .set(ColorEditFlags::UINT8, format == ColorFormat::U8);
        self.flags
            .set(ColorEditFlags::FLOAT, format == ColorFormat::Float);
        self
    }

    /// Builds the color picker.
    ///
    /// Returns true if the color value was changed.
    pub fn build(mut self) -> bool {
        self.flags.insert(ColorEditFlags::NO_ALPHA);
        let mut value: [f32; 3] = (*self.value).into().into();
        let changed = unsafe {
            sys::igColorPicker3(
                self.ui.scratch_txt(self.label),
                value.as_mut_ptr(),
                self.flags.bits() as _,
            )
        };

        if changed {
            let as_vec3: MintVec3 = value.into();

            *self.value = as_vec3.into();
        }

        changed
    }
}

impl Ui {
    /// Edits a color of 3 channels. Use [color_picker3](Self::color_picker3)
    /// for a builder to customize this widget.
    pub fn color_picker3<Label, C>(&self, label: Label, value: &mut C) -> bool
    where
        Label: AsRef<str>,
        C: Copy + Into<MintVec3>,
        MintVec3: Into<C> + Into<[f32; 3]>,
    {
        ColorPicker3 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui: self,
        }
        .build()
    }

    /// Constructs a new color picker editor builder.
    pub fn color_picker3_config<'a, Label, C>(
        &self,
        label: Label,
        value: &'a mut C,
    ) -> ColorPicker3<'_, 'a, Label, C>
    where
        Label: AsRef<str>,
        C: Copy + Into<MintVec3>,
        MintVec3: Into<C> + Into<[f32; 3]>,
    {
        ColorPicker3 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ui: self,
        }
    }
}

// Builder for a color picker widget.
///
/// # Examples
///
/// ```no_run
/// # use imgui::*;
/// # let mut imgui = Context::create();
/// # let ui = imgui.frame();
/// # let mut color = [0.0, 0.0, 0.0, 1.0];
/// if ui.color_picker4("color_picker", &mut color) {
///   println!("A color was picked");
/// }
/// ```
#[derive(Debug)]
#[must_use]
pub struct ColorPicker4<'ui, 'a, Label, Color> {
    label: Label,
    value: &'a mut Color,
    flags: ColorEditFlags,
    ref_color: Option<[f32; 4]>,
    ui: &'ui Ui,
}

impl<'ui, 'a, Label, Color> ColorPicker4<'ui, 'a, Label, Color>
where
    Label: AsRef<str>,
    Color: Copy + Into<MintVec4>,
    MintVec4: Into<Color> + Into<[f32; 4]>,
{
    /// Constructs a new color picker builder.
    #[deprecated(since = "0.9.0", note = "Use `ui.color_picker4(...)` instead")]
    pub fn new(ui: &'ui Ui, label: Label, value: &'a mut Color) -> Self {
        Self {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ref_color: None,
            ui,
        }
    }
    /// Replaces all current settings with the given flags.
    #[inline]
    pub fn flags(mut self, flags: ColorEditFlags) -> Self {
        self.flags = flags;
        self
    }
    /// Enables/disables the use of the alpha component.
    #[inline]
    pub fn alpha(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_ALPHA, !value);
        self
    }
    /// Enables/disables toggling of the options menu when right-clicking on inputs or the small
    /// preview.
    #[inline]
    pub fn options(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_OPTIONS, !value);
        self
    }
    /// Enables/disables the colored square preview next to the inputs.
    #[inline]
    pub fn small_preview(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_SMALL_PREVIEW, !value);
        self
    }
    /// Enables/disables the input sliders/text widgets.
    #[inline]
    pub fn inputs(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_INPUTS, !value);
        self
    }
    /// Enables/disables the tooltip that appears when hovering the preview.
    #[inline]
    pub fn tooltip(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_TOOLTIP, !value);
        self
    }
    /// Enables/disables display of the inline text label (the label is in any case forwarded to
    /// the tooltip and picker).
    #[inline]
    pub fn label(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_LABEL, !value);
        self
    }
    /// Enables/disables the bigger color preview on the right side of the picker.
    #[inline]
    pub fn side_preview(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_SIDE_PREVIEW, !value);
        self
    }
    /// Enables/disables the vertical alpha bar/gradient in the color picker.
    #[inline]
    pub fn alpha_bar(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::ALPHA_BAR, value);
        self
    }
    /// Sets the preview style.
    #[inline]
    pub fn preview(mut self, preview: ColorPreview) -> Self {
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW_HALF,
            preview == ColorPreview::HalfAlpha,
        );
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW,
            preview == ColorPreview::Alpha,
        );
        self
    }
    /// Sets the data format for input and output data.
    #[inline]
    pub fn input_mode(mut self, input_mode: ColorEditInputMode) -> Self {
        self.flags.set(
            ColorEditFlags::INPUT_RGB,
            input_mode == ColorEditInputMode::RGB,
        );
        self.flags.set(
            ColorEditFlags::INPUT_HSV,
            input_mode == ColorEditInputMode::HSV,
        );
        self
    }
    /// Enables/disables displaying the value as RGB.
    #[inline]
    pub fn display_rgb(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::DISPLAY_RGB, value);
        self
    }
    /// Enables/disables displaying the value as HSV.
    #[inline]
    pub fn display_hsv(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::DISPLAY_HSV, value);
        self
    }
    /// Enables/disables displaying the value as hex.
    #[inline]
    pub fn display_hex(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::DISPLAY_HEX, value);
        self
    }
    /// Sets the hue/saturation/value editor mode.
    #[inline]
    pub fn mode(mut self, mode: ColorPickerMode) -> Self {
        self.flags.set(
            ColorEditFlags::PICKER_HUE_BAR,
            mode == ColorPickerMode::HueBar,
        );
        self.flags.set(
            ColorEditFlags::PICKER_HUE_WHEEL,
            mode == ColorPickerMode::HueWheel,
        );
        self
    }
    /// Sets the formatting style of color components.
    #[inline]
    pub fn format(mut self, format: ColorFormat) -> Self {
        self.flags
            .set(ColorEditFlags::UINT8, format == ColorFormat::U8);
        self.flags
            .set(ColorEditFlags::FLOAT, format == ColorFormat::Float);
        self
    }
    /// Sets the shown reference color.
    #[inline]
    pub fn reference_color(mut self, ref_color: impl Into<MintVec4>) -> Self {
        self.ref_color = Some(ref_color.into().into());
        self
    }

    /// Builds the color picker.
    ///
    /// Returns true if the color value was changed.
    pub fn build(self) -> bool {
        let mut value: [f32; 4] = (*self.value).into().into();
        let ref_color = self.ref_color.map(|c| c.as_ptr()).unwrap_or(ptr::null());

        let changed = unsafe {
            sys::igColorPicker4(
                self.ui.scratch_txt(self.label),
                value.as_mut_ptr(),
                self.flags.bits() as _,
                ref_color,
            )
        };

        if changed {
            let as_vec3: MintVec4 = value.into();

            *self.value = as_vec3.into();
        }

        changed
    }
}

impl Ui {
    /// Edits a color of 4 channels. Use [color_picker4_config](Self::color_picker4_config)
    /// for a builder to customize this widget.
    pub fn color_picker4<Label, C>(&self, label: Label, value: &mut C) -> bool
    where
        Label: AsRef<str>,
        C: Copy + Into<MintVec4>,
        MintVec4: Into<C> + Into<[f32; 4]>,
    {
        ColorPicker4 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ref_color: None,
            ui: self,
        }
        .build()
    }

    /// Constructs a new color picker editor builder.
    pub fn color_picker4_config<'a, Label, C>(
        &self,
        label: Label,
        value: &'a mut C,
    ) -> ColorPicker4<'_, 'a, Label, C>
    where
        Label: AsRef<str>,
        C: Copy + Into<MintVec4>,
        MintVec4: Into<C> + Into<[f32; 4]>,
    {
        ColorPicker4 {
            label,
            value,
            flags: ColorEditFlags::empty(),
            ref_color: None,
            ui: self,
        }
    }
}

/// Builder for a color button widget.
///
/// # Examples
///
/// ```no_run
/// # use imgui::*;
/// # let mut imgui = Context::create();
/// # let ui = imgui.frame();
/// if ui.color_button("color_button", [1.0, 0.0, 0.0, 1.0]) {
///     println!("pressed!");
/// }
/// ```
#[derive(Copy, Clone, Debug)]
#[must_use]
pub struct ColorButton<'ui, T> {
    desc_id: T,
    color: [f32; 4],
    flags: ColorEditFlags,
    size: [f32; 2],
    ui: &'ui Ui,
}

impl<'ui, T: AsRef<str>> ColorButton<'ui, T> {
    /// Constructs a new color button builder.
    #[deprecated(since = "0.9.0", note = "Use `ui.color_button_config(...)` instead")]
    pub fn new(ui: &'ui Ui, desc_id: T, color: impl Into<MintVec4>) -> Self {
        ColorButton {
            desc_id,
            color: color.into().into(),
            flags: ColorEditFlags::empty(),
            size: [0.0, 0.0],
            ui,
        }
    }
    /// Replaces all current settings with the given flags.
    #[inline]
    pub fn flags(mut self, flags: ColorEditFlags) -> Self {
        self.flags = flags;
        self
    }
    /// Enables/disables the use of the alpha component.
    #[inline]
    pub fn alpha(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_ALPHA, !value);
        self
    }
    /// Enables/disables the tooltip that appears when hovering the preview.
    #[inline]
    pub fn tooltip(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_TOOLTIP, !value);
        self
    }
    /// Sets the preview style.
    #[inline]
    pub fn preview(mut self, preview: ColorPreview) -> Self {
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW_HALF,
            preview == ColorPreview::HalfAlpha,
        );
        self.flags.set(
            ColorEditFlags::ALPHA_PREVIEW,
            preview == ColorPreview::Alpha,
        );
        self
    }
    /// Sets the data format for input data.
    #[inline]
    pub fn input_mode(mut self, input_mode: ColorEditInputMode) -> Self {
        self.flags.set(
            ColorEditFlags::INPUT_RGB,
            input_mode == ColorEditInputMode::RGB,
        );
        self.flags.set(
            ColorEditFlags::INPUT_HSV,
            input_mode == ColorEditInputMode::HSV,
        );
        self
    }
    /// Enables/disables using the button as drag&drop source.
    ///
    /// Enabled by default.
    pub fn drag_drop(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_DRAG_DROP, !value);
        self
    }
    /// Enables/disables the button border.
    ///
    /// Enabled by default.
    pub fn border(mut self, value: bool) -> Self {
        self.flags.set(ColorEditFlags::NO_BORDER, !value);
        self
    }
    /// Sets the button size.
    ///
    /// Use 0.0 for width and/or height to use the default size.
    #[inline]
    pub fn size(mut self, size: impl Into<MintVec2>) -> Self {
        self.size = size.into().into();
        self
    }
    /// Builds the color button.
    ///
    /// Returns true if this color button was clicked.
    pub fn build(self) -> bool {
        unsafe {
            sys::igColorButton(
                self.ui.scratch_txt(self.desc_id),
                self.color.into(),
                self.flags.bits() as _,
                self.size.into(),
            )
        }
    }
}

impl Ui {
    /// Builds a [ColorButton].
    ///
    /// Returns true if this color button was clicked.
    pub fn color_button<Label: AsRef<str>>(
        &self,
        desc_id: Label,
        color: impl Into<MintVec4>,
    ) -> bool {
        ColorButton {
            desc_id,
            color: color.into().into(),
            flags: ColorEditFlags::empty(),
            size: [0.0, 0.0],
            ui: self,
        }
        .build()
    }

    /// Returns a [ColorButton] builder.
    pub fn color_button_config<Label: AsRef<str>>(
        &self,
        desc_id: Label,
        color: impl Into<MintVec4>,
    ) -> ColorButton<'_, Label> {
        ColorButton {
            desc_id,
            color: color.into().into(),
            flags: ColorEditFlags::empty(),
            size: [0.0, 0.0],
            ui: self,
        }
    }
}

/// # Widgets: Color Editor/Picker
impl Ui {
    /// Initializes current color editor/picker options (generally on application startup) if you
    /// want to select a default format, picker type, etc. Users will be able to change many
    /// settings, unless you use .options(false) in your widget builders.
    #[doc(alias = "SetColorEditOptions")]
    pub fn set_color_edit_options(&self, flags: ColorEditFlags) {
        unsafe {
            sys::igSetColorEditOptions(flags.bits() as i32);
        }
    }
}