Skip to content

Commit

Permalink
Added a custom colour picker
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-zhao committed Jun 29, 2024
1 parent c140182 commit 168f329
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
12 changes: 12 additions & 0 deletions LibSidWiz/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ public int TriggerLookaheadOnFailureFrames

[Category("Appearance")]
[Description("The line colour")]
[Editor(typeof(MyColorEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(MyColorConverter))]
public Color LineColor
{
get => _lineColor;
Expand All @@ -292,6 +294,8 @@ public float LineWidth

[Category("Appearance")]
[Description("The fill colour. Set to transparent to have no fill.")]
[Editor(typeof(MyColorEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(MyColorConverter))]
public Color FillColor
{
get => _fillColor;
Expand Down Expand Up @@ -340,6 +344,8 @@ public float ZeroLineWidth

[Category("Appearance")]
[Description("The color of the zero line")]
[Editor(typeof(MyColorEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(MyColorConverter))]
public Color ZeroLineColor
{
get => _zeroLineColor;
Expand All @@ -352,6 +358,8 @@ public Color ZeroLineColor

[Category("Appearance")]
[Description("The color of the border")]
[Editor(typeof(MyColorEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(MyColorConverter))]
public Color BorderColor
{
get => _borderColor;
Expand Down Expand Up @@ -388,6 +396,8 @@ public bool BorderEdges

[Category("Appearance")]
[Description("A background colour for the channel. This is layered above any background image, and can be transparent.")]
[Editor(typeof(MyColorEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(MyColorConverter))]
public Color BackgroundColor
{
get => _backgroundColor;
Expand Down Expand Up @@ -425,6 +435,8 @@ public Font LabelFont

[Category("Appearance")]
[Description("The color for the channel label")]
[Editor(typeof(MyColorEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(MyColorConverter))]
public Color LabelColor
{
get => _labelColor;
Expand Down
1 change: 1 addition & 0 deletions LibSidWiz/LibSidWiz.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Reference Include="System.Design" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Cyotek.Windows.Forms.ColorPicker" Version="1.7.2" />
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
Expand Down
15 changes: 15 additions & 0 deletions LibSidWiz/MyColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel;
using System.Drawing;

namespace LibSidWiz;

/// <summary>
/// This overrides ColorConverter which interferes with the colour editor.
/// </summary>
public class MyColorConverter : ColorConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return false;
}
}
68 changes: 68 additions & 0 deletions LibSidWiz/MyColorEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace LibSidWiz;

/// <summary>
/// This allows us to implement a custom colour picker
/// </summary>
public class MyColorEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (value is not Color c || provider == null)
{
return value;
}

var svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

if (svc == null)
{
return c;
}

using var form = new Cyotek.Windows.Forms.ColorPickerDialog();
form.Color = c;
form.ShowAlphaChannel = true;
return svc.ShowDialog(form) == DialogResult.OK ? form.Color : c;
}

public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}

public override void PaintValue(PaintValueEventArgs e)
{
// This is just for the little rectangle on the left
// TODO: indicate transparency?
var color = (Color)e.Value;
if (color.A < 255)
{
// Draw a checkerboard of 4x4 using the system colours
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
for (var x = 0; x < e.Bounds.Width; x += 4)
for (var y = 0; y < e.Bounds.Height; y += 4)
if (((x/4 ^ y/4) & 1) == 0)
{
e.Graphics.FillRectangle(SystemBrushes.WindowText, x, y, 4, 4);
}
}
using (var brush = new SolidBrush(color))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}

e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds);
}
}

0 comments on commit 168f329

Please sign in to comment.