123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- using System;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Color Picker", "Color Picker.")]
- [ScenarioCategory ("Colors")]
- [ScenarioCategory ("Controls")]
- public class ColorPickers : Scenario
- {
- /// <summary>Background color Label.</summary>
- private Label _backgroundColorLabel;
- /// <summary>Demo label.</summary>
- private View _demoView;
- /// <summary>Foreground color label.</summary>
- private Label _foregroundColorLabel;
- /// <summary>Background ColorPicker.</summary>
- private ColorPicker backgroundColorPicker;
- /// <summary>Foreground ColorPicker.</summary>
- private ColorPicker foregroundColorPicker;
- /// <summary>Background ColorPicker.</summary>
- private ColorPicker16 backgroundColorPicker16;
- /// <summary>Foreground ColorPicker.</summary>
- private ColorPicker16 foregroundColorPicker16;
- /// <summary>Setup the scenario.</summary>
- public override void Main ()
- {
- Application.Init ();
- Window app = new ()
- {
- Title = GetQuitKeyAndName (),
- };
- ///////////////////////////////////////
- // True Color Pickers
- ///////////////////////////////////////
- // Foreground ColorPicker.
- foregroundColorPicker = new ColorPicker {
- Title = "_Foreground Color",
- BorderStyle = LineStyle.Single,
- Width = Dim.Percent (50)
- };
- foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
- app.Add (foregroundColorPicker);
- _foregroundColorLabel = new Label
- {
- X = Pos.Left (foregroundColorPicker), Y = Pos.Bottom (foregroundColorPicker) + 1
- };
- app.Add (_foregroundColorLabel);
- // Background ColorPicker.
- backgroundColorPicker = new ColorPicker
- {
- Title = "_Background Color",
- X = Pos.AnchorEnd (),
- Width = Dim.Percent (50),
- BorderStyle = LineStyle.Single
- };
- backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
- app.Add (backgroundColorPicker);
- _backgroundColorLabel = new Label ()
- {
- X = Pos.AnchorEnd (),
- Y = Pos.Bottom (backgroundColorPicker) + 1
- };
- app.Add (_backgroundColorLabel);
- ///////////////////////////////////////
- // 16 Color Pickers
- ///////////////////////////////////////
- // Foreground ColorPicker 16.
- foregroundColorPicker16 = new ColorPicker16
- {
- Title = "_Foreground Color",
- BorderStyle = LineStyle.Single,
- Width = Dim.Percent (50),
- Visible = false // We default to HSV so hide old one
- };
- foregroundColorPicker16.ColorChanged += ForegroundColor_ColorChanged;
- app.Add (foregroundColorPicker16);
- // Background ColorPicker 16.
- backgroundColorPicker16 = new ColorPicker16
- {
- Title = "_Background Color",
- X = Pos.AnchorEnd (),
- Width = Dim.Percent (50),
- BorderStyle = LineStyle.Single,
- Visible = false // We default to HSV so hide old one
- };
- backgroundColorPicker16.ColorChanged += BackgroundColor_ColorChanged;
- app.Add (backgroundColorPicker16);
- // Demo Label.
- _demoView = new View
- {
- Title = "Color Sample",
- Text = "Lorem Ipsum",
- TextAlignment = Alignment.Center,
- VerticalTextAlignment = Alignment.Center,
- BorderStyle = LineStyle.Heavy,
- X = Pos.Center (),
- Y = Pos.Center (),
- Height = 5,
- Width = 20
- };
- app.Add (_demoView);
- // Radio for switching color models
- var rgColorModel = new RadioGroup ()
- {
- Y = Pos.Bottom (_demoView),
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- RadioLabels = new []
- {
- "_RGB",
- "_HSV",
- "H_SL",
- "_16 Colors"
- },
- SelectedItem = (int)foregroundColorPicker.Style.ColorModel,
- };
- rgColorModel.SelectedItemChanged += (_, e) =>
- {
- // 16 colors
- if (e.SelectedItem == 3)
- {
- foregroundColorPicker16.Visible = true;
- foregroundColorPicker.Visible = false;
- backgroundColorPicker16.Visible = true;
- backgroundColorPicker.Visible = false;
- // Switching to 16 colors
- ForegroundColor_ColorChanged (null,null);
- BackgroundColor_ColorChanged (null, null);
- }
- else
- {
- foregroundColorPicker16.Visible = false;
- foregroundColorPicker.Visible = true;
- foregroundColorPicker.Style.ColorModel = (ColorModel)e.SelectedItem;
- foregroundColorPicker.ApplyStyleChanges ();
- backgroundColorPicker16.Visible = false;
- backgroundColorPicker.Visible = true;
- backgroundColorPicker.Style.ColorModel = (ColorModel)e.SelectedItem;
- backgroundColorPicker.ApplyStyleChanges ();
- // Switching to true colors
- foregroundColorPicker.SelectedColor = foregroundColorPicker16.SelectedColor;
- backgroundColorPicker.SelectedColor = backgroundColorPicker16.SelectedColor;
- }
- };
- app.Add (rgColorModel);
- // Checkbox for switching show text fields on and off
- var cbShowTextFields = new CheckBox ()
- {
- Text = "Show _Text Fields",
- Y = Pos.Bottom (rgColorModel)+1,
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- CheckedState = foregroundColorPicker.Style.ShowTextFields ? CheckState.Checked: CheckState.UnChecked,
- };
- cbShowTextFields.CheckedStateChanging += (_, e) =>
- {
- foregroundColorPicker.Style.ShowTextFields = e.NewValue == CheckState.Checked;
- foregroundColorPicker.ApplyStyleChanges ();
- backgroundColorPicker.Style.ShowTextFields = e.NewValue == CheckState.Checked;
- backgroundColorPicker.ApplyStyleChanges ();
- };
- app.Add (cbShowTextFields);
- // Checkbox for switching show text fields on and off
- var cbShowName = new CheckBox ()
- {
- Text = "Show Color _Name",
- Y = Pos.Bottom (cbShowTextFields) + 1,
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- CheckedState = foregroundColorPicker.Style.ShowColorName ? CheckState.Checked : CheckState.UnChecked,
- };
- cbShowName.CheckedStateChanging += (_, e) =>
- {
- foregroundColorPicker.Style.ShowColorName = e.NewValue == CheckState.Checked;
- foregroundColorPicker.ApplyStyleChanges ();
- backgroundColorPicker.Style.ShowColorName = e.NewValue == CheckState.Checked;
- backgroundColorPicker.ApplyStyleChanges ();
- };
- app.Add (cbShowName);
- // Set default colors.
- foregroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Foreground.GetClosestNamedColor16 ();
- backgroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Background.GetClosestNamedColor16 ();
- Application.Run (app);
- app.Dispose ();
- Application.Shutdown ();
- }
- /// <summary>Fired when background color is changed.</summary>
- private void BackgroundColor_ColorChanged (object sender, EventArgs e)
- {
- UpdateColorLabel (_backgroundColorLabel,
- backgroundColorPicker.Visible ?
- backgroundColorPicker.SelectedColor :
- backgroundColorPicker16.SelectedColor
- );
- UpdateDemoLabel ();
- }
- /// <summary>Fired when foreground color is changed.</summary>
- private void ForegroundColor_ColorChanged (object sender, EventArgs e)
- {
- UpdateColorLabel (_foregroundColorLabel,
- foregroundColorPicker.Visible ?
- foregroundColorPicker.SelectedColor :
- foregroundColorPicker16.SelectedColor
- );
- UpdateDemoLabel ();
- }
- /// <summary>Update a color label from his ColorPicker.</summary>
- private void UpdateColorLabel (Label label, Color color)
- {
- label.ClearViewport ();
- label.Text =
- $"{color} ({(int)color}) #{color.R:X2}{color.G:X2}{color.B:X2}";
- }
- /// <summary>Update Demo Label.</summary>
- private void UpdateDemoLabel ()
- {
- _demoView.ColorScheme = new ColorScheme
- {
- Normal = new Attribute (
- foregroundColorPicker.Visible ?
- foregroundColorPicker.SelectedColor :
- foregroundColorPicker16.SelectedColor,
- backgroundColorPicker.Visible ?
- backgroundColorPicker.SelectedColor :
- backgroundColorPicker16.SelectedColor
- )
- };
- }
- }
|