Skip to content

Commit

Permalink
Added raw text drag n drop into palette viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKreu committed Apr 28, 2023
1 parent 718bbe1 commit 76a30cd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/PixiEditor/Helpers/ColorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class ColorHelper
public static bool ParseAnyFormat(IDataObject data, [NotNullWhen(true)] out DrawingApi.Core.ColorsImpl.Color? result) =>
ParseAnyFormat(((DataObject)data).GetText().Trim(), out result);

public static bool ParseAnyFormatList(IDataObject data, [NotNullWhen(true)] out List<DrawingApi.Core.ColorsImpl.Color> result) =>
ParseAnyFormatList(((DataObject)data).GetText().Trim(), out result);

public static bool ParseAnyFormat(string value, [NotNullWhen(true)] out DrawingApi.Core.ColorsImpl.Color? result)
{
bool hex = Regex.IsMatch(value, "^#?([a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})$");
Expand All @@ -34,6 +37,43 @@ public static bool ParseAnyFormat(string value, [NotNullWhen(true)] out DrawingA

result = new DrawingApi.Core.ColorsImpl.Color(r, g, b, a);
return true;
}

public static bool ParseAnyFormatList(string value, [NotNullWhen(true)] out List<DrawingApi.Core.ColorsImpl.Color> result)
{
result = new List<DrawingApi.Core.ColorsImpl.Color>();

// Regex patterns for hex and RGB(A) formats
const string hexPattern = @"#?([a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})";
const string rgbaPattern = @"(?:rgba?\(?)? *(?<r>\d{1,3})(?:, *| +)(?<g>\d{1,3})(?:, *| +)(?<b>\d{1,3})(?:(?:, *| +)(?<a>\d{0,3}))?\)?";

// Combined pattern for both hex and RGB(A) formats
const string combinedPattern = $@"({hexPattern})|({rgbaPattern})";
var matches = Regex.Matches(value, combinedPattern);

if (matches.Count == 0)
{
return false;
}

foreach (Match match in matches)
{
if (Regex.IsMatch(match.Value, $"^{hexPattern}$"))
{
result.Add(DrawingApi.Core.ColorsImpl.Color.Parse(match.Value));
}
else if (match.Groups["r"].Success && match.Groups["g"].Success && match.Groups["b"].Success)
{
byte r = byte.Parse(match.Groups["r"].ValueSpan);
byte g = byte.Parse(match.Groups["g"].ValueSpan);
byte b = byte.Parse(match.Groups["b"].ValueSpan);
byte a = match.Groups["a"].Success ? byte.Parse(match.Groups["a"].ValueSpan) : (byte)255;

result.Add(new DrawingApi.Core.ColorsImpl.Color(r, g, b, a));
}
}

return result.Count > 0;
}

}
2 changes: 2 additions & 0 deletions src/PixiEditor/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private void MainWindow_Drop(object sender, DragEventArgs e)
return;
}

e.Effects = DragDropEffects.Copy;
DataContext.ColorsSubViewModel.PrimaryColor = color.Value;
return;
}
Expand All @@ -222,6 +223,7 @@ private void MainWindow_DragEnter(object sender, DragEventArgs e)
if (ColorHelper.ParseAnyFormat(e.Data, out _))
{
DataContext.ActionDisplays[nameof(MainWindow_Drop)] = "Paste as primary color";
e.Effects = DragDropEffects.Copy;
return;
}

Expand Down
14 changes: 14 additions & 0 deletions src/PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
dragDropGrid.Visibility = Visibility.Visible;
ViewModelMain.Current.ActionDisplays[nameof(PaletteViewer)] = "Import palette file";
}
else if (ColorHelper.ParseAnyFormatList(e.Data, out var list))
{
e.Effects = DragDropEffects.Copy;
ViewModelMain.Current.ActionDisplays[nameof(PaletteViewer)] = list.Count > 1 ? "Import colors into palette" : "Import color into palette";
e.Handled = true;
}
}

private void Grid_PreviewDragLeave(object sender, DragEventArgs e)
Expand All @@ -163,6 +169,14 @@ private async void Grid_Drop(object sender, DragEventArgs e)

if (!IsSupportedFilePresent(e, out string filePath))
{
if (!ColorHelper.ParseAnyFormatList(e.Data, out var colors))
{
return;
}

e.Effects = DragDropEffects.Copy;
Colors.AddRange(colors.Where(x => !Colors.Contains(x)).ToList());
e.Handled = true;
return;
}

Expand Down

0 comments on commit 76a30cd

Please sign in to comment.