BitmapPixelChanges.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using PixiEditor.Exceptions;
  2. using PixiEditor.Helpers.Extensions;
  3. using PixiEditor.Models.Position;
  4. using SkiaSharp;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace PixiEditor.Models.DataHolders
  9. {
  10. public struct BitmapPixelChanges
  11. {
  12. public BitmapPixelChanges(Dictionary<Coordinates, SKColor> changedPixels)
  13. {
  14. ChangedPixels = changedPixels;
  15. WasBuiltAsSingleColored = false;
  16. }
  17. public static BitmapPixelChanges Empty => new BitmapPixelChanges(new Dictionary<Coordinates, SKColor>());
  18. public bool WasBuiltAsSingleColored { get; private set; }
  19. public Dictionary<Coordinates, SKColor> ChangedPixels { get; set; }
  20. /// <summary>
  21. /// Builds BitmapPixelChanges with only one color for specified coordinates.
  22. /// </summary>
  23. /// <returns>Single-colored BitmapPixelChanges.</returns>
  24. public static BitmapPixelChanges FromSingleColoredArray(IEnumerable<Coordinates> coordinates, SKColor color)
  25. {
  26. Dictionary<Coordinates, SKColor> dict = new Dictionary<Coordinates, SKColor>();
  27. foreach (Coordinates coordinate in coordinates)
  28. {
  29. if (dict.ContainsKey(coordinate))
  30. {
  31. continue;
  32. }
  33. dict.Add(coordinate, color);
  34. }
  35. return new BitmapPixelChanges(dict) { WasBuiltAsSingleColored = true };
  36. }
  37. /// <summary>
  38. /// Combines pixel changes array with overriding values.
  39. /// </summary>
  40. /// <param name="changes">BitmapPixelChanges to combine.</param>
  41. /// <returns>Combined BitmapPixelChanges.</returns>
  42. public static BitmapPixelChanges CombineOverride(BitmapPixelChanges[] changes)
  43. {
  44. if (changes == null || changes.Length == 0)
  45. {
  46. throw new ArgumentException();
  47. }
  48. BitmapPixelChanges output = Empty;
  49. for (int i = 0; i < changes.Length; i++)
  50. {
  51. output.ChangedPixels.AddRangeOverride(changes[i].ChangedPixels);
  52. }
  53. return output;
  54. }
  55. public static BitmapPixelChanges CombineOverride(BitmapPixelChanges changes1, BitmapPixelChanges changes2)
  56. {
  57. return CombineOverride(new[] { changes1, changes2 });
  58. }
  59. /// <summary>
  60. /// Builds BitmapPixelChanges using 2 same-length enumerables of coordinates and colors.
  61. /// </summary>
  62. public static BitmapPixelChanges FromArrays(IEnumerable<Coordinates> coordinates, IEnumerable<SKColor> color)
  63. {
  64. Coordinates[] coordinateArray = coordinates.ToArray();
  65. SKColor[] colorArray = color.ToArray();
  66. if (coordinateArray.Length != colorArray.Length)
  67. {
  68. throw new ArrayLengthMismatchException();
  69. }
  70. Dictionary<Coordinates, SKColor> dict = new Dictionary<Coordinates, SKColor>();
  71. for (int i = 0; i < coordinateArray.Length; i++)
  72. {
  73. dict.Add(coordinateArray[i], colorArray[i]);
  74. }
  75. return new BitmapPixelChanges(dict);
  76. }
  77. public BitmapPixelChanges WithoutTransparentPixels()
  78. {
  79. return new BitmapPixelChanges(ChangedPixels.Where(x => x.Value.Alpha > 0).ToDictionary(y => y.Key, y => y.Value));
  80. }
  81. }
  82. }