ColorPickerTool.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using PaintDotNet.Measurement.Enum;
  10. using System.Windows.Forms;
  11. namespace PaintDotNet.Measurement.Tools
  12. {
  13. public class ColorPickerTool : Tool
  14. {
  15. private bool mouseDown;
  16. private Cursor colorPickerToolCursor;
  17. protected override void OnMouseMove(MouseEventArgs e)
  18. {
  19. base.OnMouseMove(e);
  20. if (mouseDown)
  21. {
  22. PickColor(e);
  23. }
  24. }
  25. protected override void OnMouseDown(MouseEventArgs e)
  26. {
  27. base.OnMouseDown(e);
  28. mouseDown = true;
  29. PickColor(e);
  30. }
  31. protected override void OnMouseUp(MouseEventArgs e)
  32. {
  33. base.OnMouseUp(e);
  34. mouseDown = false;
  35. switch (AppEnvironment.ColorPickerClickBehavior())
  36. {
  37. case ColorPickerClickBehavior.NoToolSwitch:
  38. break;
  39. case ColorPickerClickBehavior.SwitchToLastTool:
  40. DocumentWorkspace.SetToolFromType(DocumentWorkspace.PreviousActiveToolType());
  41. break;
  42. case ColorPickerClickBehavior.SwitchToPencilTool:
  43. DocumentWorkspace.SetToolFromType(typeof(PencilTool));
  44. break;
  45. default:
  46. throw new System.ComponentModel.InvalidEnumArgumentException();
  47. }
  48. }
  49. private ColorBgra LiftColor(int x, int y)
  50. {
  51. ColorBgra newColor;
  52. newColor = ((BitmapLayer)ActiveLayer).Surface[x, y];
  53. return newColor;
  54. }
  55. private void PickColor(MouseEventArgs e)
  56. {
  57. if (!Document.Bounds.Contains(e.X, e.Y))
  58. {
  59. return;
  60. }
  61. ColorBgra color;
  62. color = LiftColor(e.X, e.Y);
  63. if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
  64. {
  65. this.AppEnvironment.SetPrimaryColor(color);
  66. }
  67. else if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
  68. {
  69. this.AppEnvironment.SetSecondaryColor(color);
  70. }
  71. }
  72. protected override void OnActivate()
  73. {
  74. this.colorPickerToolCursor = new Cursor(PdnResources.GetResourceStream("Cursors.ColorPickerToolCursor.cur"));
  75. this.Cursor = this.colorPickerToolCursor;
  76. base.OnActivate();
  77. }
  78. protected override void OnDeactivate()
  79. {
  80. if (this.colorPickerToolCursor != null)
  81. {
  82. this.colorPickerToolCursor.Dispose();
  83. this.colorPickerToolCursor = null;
  84. }
  85. base.OnDeactivate();
  86. }
  87. public ColorPickerTool(IDocumentWorkspace documentWorkspace)
  88. : base(documentWorkspace,
  89. PdnResources.GetImageResource("Icons.ColorPickerToolIcon.png"),
  90. PdnResources.GetString("ColorPickerTool.Name"),
  91. PdnResources.GetString("ColorPickerTool.HelpText"),
  92. 'k',
  93. true,
  94. ToolBarConfigItems.ColorPickerBehavior)
  95. {
  96. // initialize any state information you need
  97. mouseDown = false;
  98. }
  99. }
  100. }