123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /////////////////////////////////////////////////////////////////////////////////
- // Paint.NET //
- // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
- // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
- // See src/Resources/Files/License.txt for full licensing and attribution //
- // details. //
- // . //
- /////////////////////////////////////////////////////////////////////////////////
- using PaintDotNet.Measurement.Enum;
- using System.Windows.Forms;
- namespace PaintDotNet.Measurement.Tools
- {
- public class ColorPickerTool : Tool
- {
- private bool mouseDown;
- private Cursor colorPickerToolCursor;
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- if (mouseDown)
- {
- PickColor(e);
- }
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- mouseDown = true;
- PickColor(e);
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- mouseDown = false;
- switch (AppEnvironment.ColorPickerClickBehavior())
- {
- case ColorPickerClickBehavior.NoToolSwitch:
- break;
- case ColorPickerClickBehavior.SwitchToLastTool:
- DocumentWorkspace.SetToolFromType(DocumentWorkspace.PreviousActiveToolType());
- break;
- case ColorPickerClickBehavior.SwitchToPencilTool:
- DocumentWorkspace.SetToolFromType(typeof(PencilTool));
- break;
- default:
- throw new System.ComponentModel.InvalidEnumArgumentException();
- }
- }
- private ColorBgra LiftColor(int x, int y)
- {
- ColorBgra newColor;
- newColor = ((BitmapLayer)ActiveLayer).Surface[x, y];
- return newColor;
- }
- private void PickColor(MouseEventArgs e)
- {
- if (!Document.Bounds.Contains(e.X, e.Y))
- {
- return;
- }
- ColorBgra color;
- color = LiftColor(e.X, e.Y);
- if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
- {
- this.AppEnvironment.SetPrimaryColor(color);
- }
- else if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
- {
- this.AppEnvironment.SetSecondaryColor(color);
- }
- }
- protected override void OnActivate()
- {
- this.colorPickerToolCursor = new Cursor(PdnResources.GetResourceStream("Cursors.ColorPickerToolCursor.cur"));
- this.Cursor = this.colorPickerToolCursor;
- base.OnActivate();
- }
- protected override void OnDeactivate()
- {
- if (this.colorPickerToolCursor != null)
- {
- this.colorPickerToolCursor.Dispose();
- this.colorPickerToolCursor = null;
- }
- base.OnDeactivate();
- }
- public ColorPickerTool(IDocumentWorkspace documentWorkspace)
- : base(documentWorkspace,
- PdnResources.GetImageResource("Icons.ColorPickerToolIcon.png"),
- PdnResources.GetString("ColorPickerTool.Name"),
- PdnResources.GetString("ColorPickerTool.HelpText"),
- 'k',
- true,
- ToolBarConfigItems.ColorPickerBehavior)
- {
- // initialize any state information you need
- mouseDown = false;
- }
- }
- }
|