Skip to content

Commit

Permalink
#44 - fix background bleed into screen thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeclayton committed Jul 29, 2024
1 parent 3f7aada commit bb48404
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/FancyMouse.Common.UnitTests/Helpers/DrawingHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static IEnumerable<object[]> GetTestCases()
yield return new object[]
{
new TestCase(
previewStyle: AppSettings.DefaultSettings.PreviewStyle,
previewStyle: StyleHelper.BezelledPreviewStyle,
screens: new List<RectangleInfo>()
{
new(0, 0, 500, 500),
Expand All @@ -60,7 +60,7 @@ public static IEnumerable<object[]> GetTestCases()
yield return new object[]
{
new TestCase(
previewStyle: AppSettings.DefaultSettings.PreviewStyle,
previewStyle: StyleHelper.BezelledPreviewStyle,
screens: new List<RectangleInfo>()
{
new(5120, 349, 1920, 1080),
Expand Down Expand Up @@ -120,7 +120,7 @@ private static Bitmap LoadImageResource(string filename)
}

/// <summary>
/// Naive / brute force image comparison - we can optimise this later :-)
/// Naive / brute force image comparison - we can optimize this later :-)
/// </summary>
private static void AssertImagesEqual(Bitmap expected, Bitmap actual)
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 88 additions & 0 deletions src/FancyMouse.Common/Helpers/ConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Drawing;
using System.Globalization;

namespace FancyMouse.Common.Helpers;

public static class ConfigHelper
{
public static Color? ToUnnamedColor(Color? value)
{
if (!value.HasValue)
{
return null;
}

var color = value.Value;
return Color.FromArgb(color.A, color.R, color.G, color.B);
}

public static string? SerializeToConfigColorString(Color? value)
{
if (!value.HasValue)
{
return null;
}

var color = value.Value;
return color switch
{
Color { IsNamedColor: true } =>
$"{nameof(Color)}.{color.Name}",
Color { IsSystemColor: true } =>
$"{nameof(SystemColors)}.{color.Name}",
_ =>
$"#{color.R:X2}{color.G:X2}{color.B:X2}",
};
}

public static Color? DeserializeFromConfigColorString(string? value)
{
if (string.IsNullOrEmpty(value))
{
return null;
}

// e.g. "#AABBCC"
if (value.StartsWith('#'))
{
var culture = CultureInfo.InvariantCulture;
if ((value.Length == 7)
&& int.TryParse(value[1..3], NumberStyles.HexNumber, culture, out var r)
&& int.TryParse(value[3..5], NumberStyles.HexNumber, culture, out var g)
&& int.TryParse(value[5..7], NumberStyles.HexNumber, culture, out var b))
{
return Color.FromArgb(0xFF, r, g, b);
}
}

const StringComparison comparison = StringComparison.InvariantCulture;

// e.g. "Color.Red"
const string colorPrefix = $"{nameof(Color)}.";
if (value.StartsWith(colorPrefix, comparison))
{
var colorName = value[colorPrefix.Length..];
var property = typeof(Color).GetProperties()
.SingleOrDefault(property => property.Name == colorName);
if (property is not null)
{
return (Color?)property.GetValue(null, null);
}
}

// e.g. "SystemColors.Highlight"
const string systemColorPrefix = $"{nameof(SystemColors)}.";
if (value.StartsWith(systemColorPrefix, comparison))
{
var colorName = value[systemColorPrefix.Length..];
var property = typeof(SystemColors).GetProperties()
.SingleOrDefault(property => property.Name == colorName);
if (property is not null)
{
return (Color?)property.GetValue(null, null);
}
}

return null;
}
}
5 changes: 5 additions & 0 deletions src/FancyMouse.Common/Helpers/DrawingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public static Bitmap RenderPreview(
previewGraphics, previewLayout.PreviewStyle.ScreenStyle, screenshotBounds);
}

// prevent the background bleeding through into screen images
// (see https://github.com/mikeclayton/FancyMouse/issues/44)
previewGraphics.PixelOffsetMode = PixelOffsetMode.Half;
previewGraphics.InterpolationMode = InterpolationMode.NearestNeighbor;

var refreshRequired = false;
var placeholdersDrawn = false;
for (var i = 0; i < sourceScreens.Count; i++)
Expand Down
98 changes: 98 additions & 0 deletions src/FancyMouse.Common/Helpers/StyleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Drawing;
using FancyMouse.Common.Models.Drawing;
using FancyMouse.Common.Models.Styles;

namespace FancyMouse.Common.Helpers;

public static class StyleHelper
{
/// <summary>
/// Compact (legacy) preview style
/// </summary>
public static readonly PreviewStyle CompactPreviewStyle = new(
canvasSize: new(
width: 1600,
height: 1200
),
canvasStyle: new(
marginStyle: MarginStyle.Empty,
borderStyle: new(
color: SystemColors.Highlight,
all: 6,
depth: 0
),
paddingStyle: new(
all: 0
),
backgroundStyle: new(
color1: Color.FromArgb(0xFF, 0x0D, 0x57, 0xD2),
color2: Color.FromArgb(0xFF, 0x03, 0x44, 0xC0)
)
),
screenStyle: new(
marginStyle: new(
all: 0
),
borderStyle: new(
color: Color.FromArgb(0xFF, 0x22, 0x22, 0x22),
all: 0,
depth: 0
),
paddingStyle: PaddingStyle.Empty,
backgroundStyle: new(
color1: Color.MidnightBlue,
color2: Color.MidnightBlue
)
)
);

/// <summary>
/// Bezelled preview style
/// </summary>
public static readonly PreviewStyle BezelledPreviewStyle = new(
canvasSize: new(
width: 1600,
height: 1200
),
canvasStyle: new(
marginStyle: MarginStyle.Empty,
borderStyle: new(
color: SystemColors.Highlight,
all: 6,
depth: 0
),
paddingStyle: new(
all: 4
),
backgroundStyle: new(
color1: Color.FromArgb(0xFF, 0x0D, 0x57, 0xD2),
color2: Color.FromArgb(0xFF, 0x03, 0x44, 0xC0)
)
),
screenStyle: new(
marginStyle: new(
all: 4
),
borderStyle: new(
color: Color.FromArgb(0xFF, 0x22, 0x22, 0x22),
all: 12,
depth: 4
),
paddingStyle: PaddingStyle.Empty,
backgroundStyle: new(
color1: Color.MidnightBlue,
color2: Color.MidnightBlue
)
)
);

public static PreviewStyle WithCanvasSize(this PreviewStyle previewStyle, SizeInfo canvasSize)
{
ArgumentNullException.ThrowIfNull(previewStyle);
ArgumentNullException.ThrowIfNull(canvasSize);
return new PreviewStyle(
canvasSize: canvasSize,
canvasStyle: previewStyle.CanvasStyle,
screenStyle: previewStyle.ScreenStyle);
}
}
4 changes: 1 addition & 3 deletions src/FancyMouse.Common/Models/Drawing/ScreenInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using static FancyMouse.Common.NativeMethods.Core;

namespace FancyMouse.Common.Models.Drawing;
namespace FancyMouse.Common.Models.Drawing;

/// <summary>
/// Immutable version of a System.Windows.Forms.Screen object so we don't need to
Expand Down

0 comments on commit bb48404

Please sign in to comment.