Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • tool
eyedroppertool.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17
18#include "eyedroppertool.h"
19
20#include <QPainter>
21#include <QPixmap>
22#include <QBitmap>
23#include <QtMath>
24#include "pointerevent.h"
25
26#include "vectorimage.h"
27#include "layervector.h"
28#include "layerbitmap.h"
29#include "colormanager.h"
30#include "object.h"
31#include "editor.h"
32#include "layermanager.h"
33#include "scribblearea.h"
34#include "util.h"
35
36EyedropperTool::EyedropperTool(QObject* parent) : BaseTool(parent)
37{
38}
39
40void EyedropperTool::loadSettings()
41{
42 properties.width = -1;
43 properties.feather = -1;
44 properties.useFeather = false;
45 properties.useAA = -1;
46}
47
48void EyedropperTool::saveSettings()
49{
50}
51
52QCursor EyedropperTool::cursor()
53{
54 if (mEditor->preference()->isOn(SETTING::TOOL_CURSOR))
55 {
56 return QCursor(QPixmap(":icons/general/cursor-eyedropper.svg"), 0, 15);
57 }
58 else
59 {
60 return QCursor(QPixmap(":icons/general/cross.png"), 10, 10);
61 }
62}
63
64QCursor EyedropperTool::cursor(const QColor color)
65{
66 QPixmap icon(":icons/general/cursor-eyedropper.svg");
67
68 QPixmap pixmap(32, 32);
69 pixmap.fill(Qt::transparent);
70
71 QPainter painter(&pixmap);
72 painter.drawPixmap(0, 0, icon);
73
74 painter.setBrush(Qt::white);
75 painter.drawRect(17, 17, 13, 13);
76 painter.setPen(QPen(Qt::white, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
77 painter.setBrush(color);
78 painter.drawRect(16, 16, 15, 15);
79 painter.end();
80
81 return QCursor(pixmap, 0, 15);
82}
83
84void EyedropperTool::pointerPressEvent(PointerEvent*)
85{}
86
87void EyedropperTool::pointerMoveEvent(PointerEvent* event)
88{
89 Layer* layer = mEditor->layers()->currentLayer();
90 if (layer == nullptr) { return; }
91
92 if (layer->type() == Layer::BITMAP)
93 {
94 QColor pickedColor = getBitmapColor(static_cast<LayerBitmap*>(layer), event->canvasPos());
95 if (pickedColor.isValid())
96 {
97 mScribbleArea->setCursor(cursor(pickedColor));
98 }
99 else
100 {
101 mScribbleArea->setCursor(cursor());
102 }
103 }
104 if (layer->type() == Layer::VECTOR)
105 {
106 int pickedColor = getVectorColor(static_cast<LayerVector*>(layer), event->canvasPos());
107 if (pickedColor >= 0)
108 {
109 mScribbleArea->setCursor(cursor(mEditor->object()->getColor(pickedColor).color));
110 }
111 else
112 {
113 mScribbleArea->setCursor(cursor());
114 }
115 }
116}
117
118void EyedropperTool::pointerReleaseEvent(PointerEvent* event)
119{
120 if (event->button() == Qt::LeftButton)
121 {
122 updateFrontColor(event->canvasPos());
123
124 // reset cursor
125 mScribbleArea->setCursor(cursor());
126 }
127}
128
129void EyedropperTool::updateFrontColor(const QPointF& pos)
130{
131 Layer* layer = mEditor->layers()->currentLayer();
132 if (layer == nullptr) { return; }
133
134 if (layer->type() == Layer::BITMAP)
135 {
136 QColor pickedColor = getBitmapColor(static_cast<LayerBitmap*>(layer), pos);
137 if (pickedColor.isValid())
138 {
139 mEditor->color()->setFrontColor(pickedColor);
140 }
141 }
142 else if (layer->type() == Layer::VECTOR)
143 {
144 int pickedColor = getVectorColor(static_cast<LayerVector*>(layer), pos);
145 if (pickedColor >= 0)
146 {
147 mEditor->color()->setColorNumber(pickedColor);
148 }
149 }
150}
151
152QColor EyedropperTool::getBitmapColor(LayerBitmap* layer, const QPointF& pos)
153{
154 BitmapImage* targetImage = layer->getLastBitmapImageAtFrame(mEditor->currentFrame(), 0);
155 if (targetImage == nullptr || !targetImage->contains(pos)) return QColor();
156
157 QColor pickedColour;
158 const QRgb pixelColor = targetImage->constScanLine(qFloor(pos.x()),
159 qFloor(pos.y()));
160 pickedColour.setRgba(pixelColor);
161
162 if (pickedColour.alpha() <= 0) pickedColour = QColor();
163 return pickedColour;
164}
165
166int EyedropperTool::getVectorColor(LayerVector* layer, const QPointF& pos)
167{
168 auto vectorImage = static_cast<VectorImage*>(layer->getLastKeyFrameAtPosition(mEditor->currentFrame()));
169 if (vectorImage == nullptr) return -1;
170
171 // Check curves
172 const qreal toleranceDistance = 10.0;
173 const QList<int> closestCurves = vectorImage->getCurvesCloseTo(pos, toleranceDistance);
174 const QList<int> visibleClosestCurves = filter(closestCurves, [vectorImage](int i) { return vectorImage->isCurveVisible(i); });
175
176 if (!visibleClosestCurves.isEmpty())
177 {
178 return vectorImage->getCurvesColor(visibleClosestCurves.last());
179 }
180
181 // Check fills
182 int colorNumber = vectorImage->getColorNumber(pos);
183 return colorNumber;
184}
BaseTool
Definition: basetool.h:70
BitmapImage
Definition: bitmapimage.h:28
EyedropperTool::updateFrontColor
void updateFrontColor(const QPointF &pos)
Updates front color for bitmap and color index for vector.
Definition: eyedroppertool.cpp:129
EyedropperTool::getBitmapColor
QColor getBitmapColor(LayerBitmap *layer, const QPointF &pos)
Retrieves color of the pixel under the cursor for a bitmap layer.
Definition: eyedroppertool.cpp:152
EyedropperTool::getVectorColor
int getVectorColor(LayerVector *layer, const QPointF &pos)
Retrieves the color index of the pixel under the cursor for a vector layer.
Definition: eyedroppertool.cpp:166
LayerBitmap
Definition: layerbitmap.h:26
Layer
Definition: layer.h:33
LayerVector
Definition: layervector.h:26
PointerEvent
Definition: pointerevent.h:8
VectorImage
Definition: vectorimage.h:32
QColor
QColor::alpha
int alpha() const const
QColor::isValid
bool isValid() const const
QColor::setRgba
void setRgba(QRgb rgba)
QCursor
QList
QList::isEmpty
bool isEmpty() const const
QList::last
T & last()
QObject
QObject::event
virtual bool event(QEvent *e)
QPainter
QPen
QPixmap
QPointF
QPointF::x
qreal x() const const
QPointF::y
qreal y() const const
Qt::transparent
transparent
Qt::LeftButton
LeftButton
Qt::RoundCap
RoundCap
Qt::RoundJoin
RoundJoin
Qt::SolidLine
SolidLine
QWidget::setCursor
void setCursor(const QCursor &)
Generated on Thu Jun 5 2025 14:06:43 for Pencil2D by doxygen 1.9.6 based on revision 4c63407997b2c03e5048716586dec6fbbb755173