BrightnessTool.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using PixiEditor.Helpers.Extensions;
  7. using PixiEditor.Models.Colors;
  8. using PixiEditor.Models.DataHolders;
  9. using PixiEditor.Models.Enums;
  10. using PixiEditor.Models.Layers;
  11. using PixiEditor.Models.Position;
  12. using PixiEditor.Models.Tools.ToolSettings.Settings;
  13. using PixiEditor.Models.Tools.ToolSettings.Toolbars;
  14. namespace PixiEditor.Models.Tools.Tools
  15. {
  16. public class BrightnessTool : BitmapOperationTool
  17. {
  18. private const float CorrectionFactor = 5f; // Initial correction factor
  19. private List<Coordinates> pixelsVisited = new List<Coordinates>();
  20. public BrightnessTool()
  21. {
  22. ActionDisplay = "Draw on pixel to make it brighter. Hold Ctrl to darken.";
  23. Tooltip = "Makes pixel brighter or darker pixel (U). Hold Ctrl to make pixel darker.";
  24. Toolbar = new BrightnessToolToolbar(CorrectionFactor);
  25. }
  26. public BrightnessMode Mode { get; set; } = BrightnessMode.Default;
  27. public override void OnRecordingLeftMouseDown(MouseEventArgs e)
  28. {
  29. pixelsVisited.Clear();
  30. }
  31. public override void OnKeyDown(KeyEventArgs e)
  32. {
  33. if (e.Key == Key.LeftCtrl)
  34. {
  35. ActionDisplay = "Draw on pixel to make it darker. Release Ctrl to brighten.";
  36. }
  37. }
  38. public override void OnKeyUp(KeyEventArgs e)
  39. {
  40. if (e.Key == Key.LeftCtrl)
  41. {
  42. ActionDisplay = "Draw on pixel to make it brighter. Hold Ctrl to darken.";
  43. }
  44. }
  45. public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
  46. {
  47. int toolSize = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
  48. float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;
  49. Mode = Toolbar.GetEnumSetting<BrightnessMode>("BrightnessMode").Value;
  50. LayerChange[] layersChanges = new LayerChange[1];
  51. if (Keyboard.IsKeyDown(Key.LeftCtrl))
  52. {
  53. layersChanges[0] = new LayerChange(ChangeBrightness(layer, coordinates[0], toolSize, -correctionFactor), layer);
  54. }
  55. else
  56. {
  57. layersChanges[0] = new LayerChange(ChangeBrightness(layer, coordinates[0], toolSize, correctionFactor), layer);
  58. }
  59. return layersChanges;
  60. }
  61. public BitmapPixelChanges ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize, float correctionFactor)
  62. {
  63. DoubleCords centeredCoords = CoordinatesCalculator.CalculateThicknessCenter(coordinates, toolSize);
  64. IEnumerable<Coordinates> rectangleCoordinates = CoordinatesCalculator.RectangleToCoordinates(
  65. centeredCoords.Coords1.X,
  66. centeredCoords.Coords1.Y,
  67. centeredCoords.Coords2.X,
  68. centeredCoords.Coords2.Y);
  69. BitmapPixelChanges changes = new BitmapPixelChanges(new Dictionary<Coordinates, Color>());
  70. foreach (Coordinates coordinate in rectangleCoordinates)
  71. {
  72. if (Mode == BrightnessMode.Default)
  73. {
  74. if (pixelsVisited.Contains(coordinate))
  75. {
  76. continue;
  77. }
  78. pixelsVisited.Add(coordinate);
  79. }
  80. Color pixel = layer.GetPixelWithOffset(coordinate.X, coordinate.Y);
  81. Color newColor = ExColor.ChangeColorBrightness(
  82. Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B),
  83. correctionFactor);
  84. changes.ChangedPixels.Add(
  85. new Coordinates(coordinate.X, coordinate.Y),
  86. newColor);
  87. }
  88. return changes;
  89. }
  90. }
  91. }