using System;
using Terminal.Gui;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("Color Picker", "Color Picker.")]
[ScenarioCategory ("Colors")]
[ScenarioCategory ("Controls")]
public class ColorPickers : Scenario
{
/// Background color Label.
private Label _backgroundColorLabel;
/// Demo label.
private View _demoView;
/// Foreground color label.
private Label _foregroundColorLabel;
/// Background ColorPicker.
private ColorPicker backgroundColorPicker;
/// Foreground ColorPicker.
private ColorPicker foregroundColorPicker;
/// Setup the scenario.
public override void Main ()
{
Application.Init ();
Window app = new ()
{
Title = GetQuitKeyAndName (),
};
// Foreground ColorPicker.
foregroundColorPicker = new ColorPicker { Title = "Foreground Color", BorderStyle = LineStyle.Single };
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 (),
BoxHeight = 1,
BoxWidth = 4,
BorderStyle = LineStyle.Single,
};
backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
app.Add (backgroundColorPicker);
_backgroundColorLabel = new Label ()
{
X = Pos.AnchorEnd (),
Y = Pos.Bottom (backgroundColorPicker) + 1
};
app.Add (_backgroundColorLabel);
// 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);
// Set default colors.
foregroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Foreground.GetClosestNamedColor ();
backgroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Background.GetClosestNamedColor ();
app.Initialized += (s, e) => app.LayoutSubviews ();
Application.Run (app);
app.Dispose ();
Application.Shutdown ();
}
/// Fired when background color is changed.
private void BackgroundColor_ColorChanged (object sender, EventArgs e)
{
UpdateColorLabel (_backgroundColorLabel, backgroundColorPicker);
UpdateDemoLabel ();
}
/// Fired when foreground color is changed.
private void ForegroundColor_ColorChanged (object sender, EventArgs e)
{
UpdateColorLabel (_foregroundColorLabel, foregroundColorPicker);
UpdateDemoLabel ();
}
/// Update a color label from his ColorPicker.
private void UpdateColorLabel (Label label, ColorPicker colorPicker)
{
label.Clear ();
var color = new Color (colorPicker.SelectedColor);
label.Text =
$"{colorPicker.SelectedColor} ({(int)colorPicker.SelectedColor}) #{color.R:X2}{color.G:X2}{color.B:X2}";
}
/// Update Demo Label.
private void UpdateDemoLabel ()
{
_demoView.ColorScheme = new ColorScheme
{
Normal = new Attribute (
foregroundColorPicker.SelectedColor,
backgroundColorPicker.SelectedColor
)
};
}
}