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}
43
44QCursor EyedropperTool::cursor()
45{
46 if (mEditor->preference()->isOn(SETTING::TOOL_CURSOR))
47 {
48 return QCursor(QPixmap(":icons/general/cursor-eyedropper.svg"), 0, 15);
49 }
50 else
51 {
52 return QCursor(QPixmap(":icons/general/cross.png"), 10, 10);
53 }
54}
55
56QCursor EyedropperTool::cursor(const QColor color)
57{
58 QPixmap icon(":icons/general/cursor-eyedropper.svg");
59
60 QPixmap pixmap(32, 32);
61 pixmap.fill(Qt::transparent);
62
63 QPainter painter(&pixmap);
64 painter.drawPixmap(0, 0, icon);
65
66 painter.setBrush(Qt::white);
67 painter.drawRect(17, 17, 13, 13);
68 painter.setPen(QPen(Qt::white, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
69 painter.setBrush(color);
70 painter.drawRect(16, 16, 15, 15);
71 painter.end();
72
73 return QCursor(pixmap, 0, 15);
74}
75
76void EyedropperTool::pointerPressEvent(PointerEvent*)
77{}
78
79void EyedropperTool::pointerMoveEvent(PointerEvent* event)
80{
81 Layer* layer = mEditor->layers()->currentLayer();
82 if (layer == nullptr) { return; }
83
84 if (layer->type() == Layer::BITMAP)
85 {
86 QColor pickedColor = getBitmapColor(static_cast<LayerBitmap*>(layer), event->canvasPos());
87 if (pickedColor.isValid())
88 {
89 mScribbleArea->setCursor(cursor(pickedColor));
90 }
91 else
92 {
93 mScribbleArea->setCursor(cursor());
94 }
95 }
96 if (layer->type() == Layer::VECTOR)
97 {
98 int pickedColor = getVectorColor(static_cast<LayerVector*>(layer), event->canvasPos());
99 if (pickedColor >= 0)
100 {
101 mScribbleArea->setCursor(cursor(mEditor->object()->getColor(pickedColor).color));
102 }
103 else
104 {
105 mScribbleArea->setCursor(cursor());
106 }
107 }
108}
109
110void EyedropperTool::pointerReleaseEvent(PointerEvent* event)
111{
112 if (event->button() == Qt::LeftButton)
113 {
114 updateFrontColor(event->canvasPos());
115
116 // reset cursor
117 mScribbleArea->setCursor(cursor());
118 }
119}
120
121void EyedropperTool::updateFrontColor(const QPointF& pos)
122{
123 Layer* layer = mEditor->layers()->currentLayer();
124 if (layer == nullptr) { return; }
125
126 if (layer->type() == Layer::BITMAP)
127 {
128 QColor pickedColor = getBitmapColor(static_cast<LayerBitmap*>(layer), pos);
129 if (pickedColor.isValid())
130 {
131 mEditor->color()->setFrontColor(pickedColor);
132 }
133 }
134 else if (layer->type() == Layer::VECTOR)
135 {
136 int pickedColor = getVectorColor(static_cast<LayerVector*>(layer), pos);
137 if (pickedColor >= 0)
138 {
139 mEditor->color()->setColorNumber(pickedColor);
140 }
141 }
142}
143
144QColor EyedropperTool::getBitmapColor(LayerBitmap* layer, const QPointF& pos)
145{
146 BitmapImage* targetImage = layer->getLastBitmapImageAtFrame(mEditor->currentFrame(), 0);
147 if (targetImage == nullptr || !targetImage->contains(pos)) return QColor();
148
149 QColor pickedColour;
150 const QRgb pixelColor = targetImage->constScanLine(qFloor(pos.x()),
151 qFloor(pos.y()));
152 pickedColour.setRgba(pixelColor);
153
154 if (pickedColour.alpha() <= 0) pickedColour = QColor();
155 return pickedColour;
156}
157
158int EyedropperTool::getVectorColor(LayerVector* layer, const QPointF& pos)
159{
160 auto vectorImage = static_cast<VectorImage*>(layer->getLastKeyFrameAtPosition(mEditor->currentFrame()));
161 if (vectorImage == nullptr) return -1;
162
163 // Check curves
164 const qreal toleranceDistance = 10.0;
165 const QList<int> closestCurves = vectorImage->getCurvesCloseTo(pos, toleranceDistance);
166 const QList<int> visibleClosestCurves = filter(closestCurves, [vectorImage](int i) { return vectorImage->isCurveVisible(i); });
167
168 if (!visibleClosestCurves.isEmpty())
169 {
170 return vectorImage->getCurvesColor(visibleClosestCurves.last());
171 }
172
173 // Check fills
174 int colorNumber = vectorImage->getColorNumber(pos);
175 return colorNumber;
176}
BaseTool
Definition: basetool.h:47
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:121
EyedropperTool::getBitmapColor
QColor getBitmapColor(LayerBitmap *layer, const QPointF &pos)
Retrieves color of the pixel under the cursor for a bitmap layer.
Definition: eyedroppertool.cpp:144
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:158
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 Sun Jan 4 2026 06:39:21 for Pencil2D by doxygen 1.9.6 based on revision f960686b9664502345cde3fe2ff693b0a779c087