Skip to content

Commit 5a3f058

Browse files
authored
Checkerboard background for clear visualization of palette colors with transparency (#1759)
1 parent f761b94 commit 5a3f058

File tree

6 files changed

+62
-25
lines changed

6 files changed

+62
-25
lines changed

Pinta.Core/Managers/PaletteManager.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public interface IPaletteService
3737
Color PrimaryColor { get; set; }
3838
Color SecondaryColor { get; set; }
3939
Palette CurrentPalette { get; }
40+
int MaxRecentlyUsedColor { get; }
4041
ReadOnlyCollection<Color> RecentlyUsedColors { get; }
4142
void SetColor (bool setPrimary, Color color, bool addToRecent = true);
42-
public event EventHandler? PrimaryColorChanged;
43-
public event EventHandler? SecondaryColorChanged;
43+
event EventHandler? PrimaryColorChanged;
44+
event EventHandler? SecondaryColorChanged;
45+
event EventHandler? RecentColorsChanged;
4446
}
4547

4648
public sealed class PaletteManager : IPaletteService
@@ -69,10 +71,10 @@ public Color SecondaryColor {
6971

7072
public Palette CurrentPalette { get; }
7173

72-
private readonly SettingsManager settings;
74+
private readonly ISettingsService settings;
7375
private readonly PaletteFormatManager palette_formats;
7476
public PaletteManager (
75-
SettingsManager settings,
77+
ISettingsService settings,
7678
PaletteFormatManager paletteFormats)
7779
{
7880
List<Color> recentlyUsed = new (MAX_RECENT_COLORS);

Pinta.Gui.Widgets/Widgets/ColorPickerDialog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ enum ColorSurfaceType
6767

6868
// hex + sliders
6969
private readonly Gtk.Entry hex_entry;
70-
private readonly PaletteManager palette;
70+
private readonly IPaletteService palette;
7171
private int slider_width = DEFAULT_SLIDER_WIDTH;
7272
private readonly Gtk.Box sliders_box;
7373
private readonly ColorPickerSlider hue_slider;
@@ -189,7 +189,7 @@ private static bool IsPrimary (int colorIndex) // TODO: Get rid of this
189189
/// <param name="windowTitle">Title of the dialog.</param>
190190
internal ColorPickerDialog (
191191
Gtk.Window? parentWindow,
192-
PaletteManager palette,
192+
IPaletteService palette,
193193
ColorPick adjustable,
194194
bool primarySelected, // TODO: Get rid of this
195195
bool livePalette,

Pinta.Gui.Widgets/Widgets/PaletteWidget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal static class PaletteWidget
1010
internal const int PALETTE_MARGIN = 10;
1111

1212
public static int GetSwatchAtLocation (
13-
PaletteManager palette,
13+
IPaletteService palette,
1414
PointD point,
1515
RectangleD palette_bounds,
1616
bool recentColorPalette = false)
@@ -29,7 +29,7 @@ public static int GetSwatchAtLocation (
2929
}
3030

3131
public static RectangleD GetSwatchBounds (
32-
PaletteManager palette,
32+
IPaletteService palette,
3333
int index,
3434
RectangleD palette_bounds,
3535
bool recentColorPalette = false)

Pinta.Gui.Widgets/Widgets/StatusBarColorPaletteWidget.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.Diagnostics;
2929
using System.Linq;
3030
using System.Threading.Tasks;
31+
using Adw;
3132
using Cairo;
3233
using Pinta.Core;
3334

@@ -42,15 +43,13 @@ public sealed class StatusBarColorPaletteWidget : Gtk.DrawingArea
4243
private readonly RectangleD swap_rect = new (27, 2, 15, 15);
4344
private readonly RectangleD reset_rect = new (2, 27, 15, 15);
4445

45-
private readonly ChromeManager chrome;
46-
private readonly PaletteManager palette;
46+
private readonly IChromeService chrome;
47+
private readonly IPaletteService palette;
4748

4849
private RectangleD palette_rect;
4950
private RectangleD recent_palette_rect;
5051

51-
public StatusBarColorPaletteWidget (
52-
ChromeManager chrome,
53-
PaletteManager palette)
52+
public StatusBarColorPaletteWidget (IChromeService chrome, IPaletteService palette)
5453
{
5554
this.chrome = chrome;
5655
this.palette = palette;
@@ -176,12 +175,24 @@ private async void HandleClick (PointD point, uint button)
176175

177176
private void Draw (Context g)
178177
{
178+
const int TILE_SIZE = 16;
179+
using Pattern checkeredPattern =
180+
CairoExtensions.CreateTransparentBackgroundPattern (TILE_SIZE);
181+
179182
// Draw Secondary color swatch
183+
184+
if (palette.SecondaryColor.A < 1)
185+
g.FillRectangle (secondary_rect, checkeredPattern);
186+
180187
g.FillRectangle (secondary_rect, palette.SecondaryColor);
181188
g.DrawRectangle (new RectangleD (secondary_rect.X + 1, secondary_rect.Y + 1, secondary_rect.Width - 2, secondary_rect.Height - 2), new Color (1, 1, 1), 1);
182189
g.DrawRectangle (secondary_rect, new Color (0, 0, 0), 1);
183190

184191
// Draw Primary color swatch
192+
193+
if (palette.PrimaryColor.A < 1)
194+
g.FillRectangle (primary_rect, checkeredPattern);
195+
185196
g.FillRectangle (primary_rect, palette.PrimaryColor);
186197
g.DrawRectangle (new RectangleD (primary_rect.X + 1, primary_rect.Y + 1, primary_rect.Width - 2, primary_rect.Height - 2), new Color (1, 1, 1), 1);
187198
g.DrawRectangle (primary_rect, new Color (0, 0, 0), 1);
@@ -198,14 +209,30 @@ private void Draw (Context g)
198209
// Draw recently used color swatches
199210
var recent = palette.RecentlyUsedColors;
200211

201-
for (int i = 0; i < recent.Count; i++)
202-
g.FillRectangle (PaletteWidget.GetSwatchBounds (palette, i, recent_palette_rect, true), recent.ElementAt (i));
212+
for (int i = 0; i < recent.Count; i++) {
213+
214+
RectangleD swatchBounds = PaletteWidget.GetSwatchBounds (palette, i, recent_palette_rect, true);
215+
Color recentColor = recent.ElementAt (i);
216+
217+
if (recentColor.A < 1) // Only draw checkered pattern if there is transparency
218+
g.FillRectangle (swatchBounds, checkeredPattern);
219+
220+
g.FillRectangle (swatchBounds, recentColor);
221+
}
203222

204223
// Draw color swatches
205224
var currentPalette = palette.CurrentPalette;
206225

207-
for (int i = 0; i < currentPalette.Colors.Count; i++)
208-
g.FillRectangle (PaletteWidget.GetSwatchBounds (palette, i, palette_rect), currentPalette.Colors[i]);
226+
for (int i = 0; i < currentPalette.Colors.Count; i++) {
227+
228+
RectangleD swatchBounds = PaletteWidget.GetSwatchBounds (palette, i, palette_rect);
229+
Color paletteColor = currentPalette.Colors[i];
230+
231+
if (paletteColor.A < 1) // Only draw checkered pattern if there is transparency
232+
g.FillRectangle (swatchBounds, checkeredPattern);
233+
234+
g.FillRectangle (swatchBounds, paletteColor);
235+
}
209236

210237
g.Dispose ();
211238
}

tests/Pinta.Effects.Tests/Mocks/MockPalette.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ namespace Pinta.Effects.Tests;
77

88
internal sealed class MockPalette : IPaletteService
99
{
10-
public Color PrimaryColor { get; set; } = new (0, 0, 0); // Black
11-
public Color SecondaryColor { get; set; } = new (1, 1, 1); // White
10+
public Color PrimaryColor { get; set; } = Color.Black;
11+
public Color SecondaryColor { get; set; } = Color.White;
1212

1313
public Palette CurrentPalette
1414
=> throw new NotImplementedException ();
1515

16+
public int MaxRecentlyUsedColor
17+
=> throw new NotImplementedException ();
18+
1619
public ReadOnlyCollection<Color> RecentlyUsedColors
1720
=> throw new NotImplementedException ();
1821

19-
#pragma warning disable CS0067 // The event 'MockPalette.PrimaryColorChanged' is never used
22+
#pragma warning disable CS0067 // The event 'X' is never used
2023
public event EventHandler? PrimaryColorChanged;
2124
public event EventHandler? SecondaryColorChanged;
22-
#pragma warning restore CS0067 // The event 'MockPalette.PrimaryColorChanged' is never used
25+
public event EventHandler? RecentColorsChanged;
26+
#pragma warning restore CS0067 // The event 'x' is never used
2327

2428
public void SetColor (bool setPrimary, Color color, bool addToRecent = true)
2529
{

tests/PintaBenchmarks/Mocks/MockPalette.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ namespace PintaBenchmarks;
66

77
internal sealed class MockPalette : IPaletteService
88
{
9-
public Color PrimaryColor { get; set; } = new (0, 0, 0); // Black
10-
public Color SecondaryColor { get; set; } = new (1, 1, 1); // White
9+
public Color PrimaryColor { get; set; } = Color.Black;
10+
public Color SecondaryColor { get; set; } = Color.White;
1111

1212
public Palette CurrentPalette
1313
=> throw new NotImplementedException ();
1414

15+
public int MaxRecentlyUsedColor
16+
=> throw new NotImplementedException ();
17+
1518
public ReadOnlyCollection<Color> RecentlyUsedColors
1619
=> throw new NotImplementedException ();
1720

18-
#pragma warning disable CS0067 // The event 'MockPalette.PrimaryColorChanged' is never used
21+
#pragma warning disable CS0067 // The event 'X' is never used
1922
public event EventHandler? PrimaryColorChanged;
2023
public event EventHandler? SecondaryColorChanged;
21-
#pragma warning restore CS0067 // The event 'MockPalette.PrimaryColorChanged' is never used
24+
public event EventHandler? RecentColorsChanged;
25+
#pragma warning restore CS0067 // The event 'X' is never used
2226

2327
public void SetColor (bool setPrimary, Color color, bool addToRecent = true)
2428
{

0 commit comments

Comments
 (0)