using PixiEditor.Exceptions; using PixiEditor.Helpers.Extensions; using PixiEditor.Models.Position; using SkiaSharp; using System; using System.Collections.Generic; using System.Linq; namespace PixiEditor.Models.DataHolders { public struct BitmapPixelChanges { public BitmapPixelChanges(Dictionary changedPixels) { ChangedPixels = changedPixels; WasBuiltAsSingleColored = false; } public static BitmapPixelChanges Empty => new BitmapPixelChanges(new Dictionary()); public bool WasBuiltAsSingleColored { get; private set; } public Dictionary ChangedPixels { get; set; } /// /// Builds BitmapPixelChanges with only one color for specified coordinates. /// /// Single-colored BitmapPixelChanges. public static BitmapPixelChanges FromSingleColoredArray(IEnumerable coordinates, SKColor color) { Dictionary dict = new Dictionary(); foreach (Coordinates coordinate in coordinates) { if (dict.ContainsKey(coordinate)) { continue; } dict.Add(coordinate, color); } return new BitmapPixelChanges(dict) { WasBuiltAsSingleColored = true }; } /// /// Combines pixel changes array with overriding values. /// /// BitmapPixelChanges to combine. /// Combined BitmapPixelChanges. public static BitmapPixelChanges CombineOverride(BitmapPixelChanges[] changes) { if (changes == null || changes.Length == 0) { throw new ArgumentException(); } BitmapPixelChanges output = Empty; for (int i = 0; i < changes.Length; i++) { output.ChangedPixels.AddRangeOverride(changes[i].ChangedPixels); } return output; } public static BitmapPixelChanges CombineOverride(BitmapPixelChanges changes1, BitmapPixelChanges changes2) { return CombineOverride(new[] { changes1, changes2 }); } /// /// Builds BitmapPixelChanges using 2 same-length enumerables of coordinates and colors. /// public static BitmapPixelChanges FromArrays(IEnumerable coordinates, IEnumerable color) { Coordinates[] coordinateArray = coordinates.ToArray(); SKColor[] colorArray = color.ToArray(); if (coordinateArray.Length != colorArray.Length) { throw new ArrayLengthMismatchException(); } Dictionary dict = new Dictionary(); for (int i = 0; i < coordinateArray.Length; i++) { dict.Add(coordinateArray[i], colorArray[i]); } return new BitmapPixelChanges(dict); } public BitmapPixelChanges WithoutTransparentPixels() { return new BitmapPixelChanges(ChangedPixels.Where(x => x.Value.Alpha > 0).ToDictionary(y => y.Key, y => y.Value)); } } }