Krita Source Code Documentation
Loading...
Searching...
No Matches
KisAsyncColorSamplerHelper.cpp
Go to the documentation of this file.
1
2/*
3 * SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
4 * SPDX-FileCopyrightText: 2025 Carsten Hartenfels <carsten.hartenfels@pm.me>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
10
11#include <QApplication>
12#include <QPainter>
13#include <QPainterPath>
14#include <QPalette>
15#include <QPixmap>
16#include <QTransform>
17
20#include "KoShapeManager.h"
21#include "KoViewConverter.h"
22#include "KoIcon.h"
23#include "kis_cursor.h"
26#include "kis_canvas2.h"
27#include "KisViewManager.h"
28#include "KisDocument.h"
33#include "kis_painting_tweaks.h"
34
35
36namespace {
37QColor colorWithAlpha(QColor color, int alpha)
38{
39 color.setAlpha(alpha);
40 return color;
41}
42}
43
45{
46 static constexpr qreal PREVIEW_RECT_SIZE = 48.0;
47
49 : canvas(_canvas)
50 {}
51
53
56 bool updateGlobalColor {true};
57
58 bool isActive {false};
59 bool showPreview {false};
60 bool haveSample {false};
61
64 QScopedPointer<SamplingCompressor> samplingCompressor;
65
67
74
80
84 QPainterPath cacheCrosshairPath;
88
90 QColor baseColor;
92
93 QPixmap cache;
94 qreal cacheRotation = 0.0;
95 bool cacheMirror = false;
96
98 return canvas->image().data();
99 }
100
101 const KoViewConverter &converter() const {
102 return *canvas->imageView()->viewConverter();
103 }
104
106 {
107 // Offsetting to the sides is both vertical and horizontal, when
108 // offsetting above it's only vertical, so it needs a bit more space.
109 constexpr qreal OFFSET = 32.0;
110 constexpr qreal OFFSET_ABOVE = OFFSET * 1.5;
111 constexpr qreal SIZE = PREVIEW_RECT_SIZE;
112
113 bool mirrored = canvas->xAxisMirrored();
114 bool flipped = canvas->yAxisMirrored();
115
121 } else {
122 effectiveStyle = style;
123 }
124
125 qreal width = haveSample ? SIZE * 2.0 : SIZE;
126
127 qreal x, y;
128 switch (effectiveStyle) {
130 x = -(OFFSET + width);
131 y = flipped ? -(OFFSET + SIZE) : OFFSET;
132 break;
134 x = OFFSET;
135 y = flipped ? -(OFFSET + SIZE) : OFFSET;
136 break;
137 default:
138 x = width / -2.0;
139 y = flipped ? OFFSET_ABOVE : -(OFFSET_ABOVE + SIZE);
140 break;
141 }
142
143 QRectF rect(x, y, width, SIZE);
144
145 qreal canvasRotationAngle = canvas->rotationAngle();
146 if (!qFuzzyIsNull(canvasRotationAngle)) {
147 QTransform tf;
148 tf.rotate(mirrored ? canvasRotationAngle : -canvasRotationAngle);
149 rect = tf.mapRect(rect);
150 }
151
152 return rect;
153 }
154
156 {
158
159 // If zoom preview enabled, it's possible to offset the color sampler preview and still be accurate
160 // This can help when sampling color using finger gesture
163
164 constexpr qreal OFFSET = 10.0;
165 constexpr qreal OFFSET_ABOVE = 20.0;
166 bool mirrored = canvas->xAxisMirrored();
167 bool flipped = canvas->yAxisMirrored();
171 }
174 }
175 else effectivePos = circlePreviewPosition;
176
177 qreal x = 0;
178 qreal y = flipped ? circlePreviewDiameter / 2.0 + OFFSET_ABOVE : -circlePreviewDiameter / 2.0 - OFFSET_ABOVE;
179
180 switch (effectivePos) {
182 x = -circlePreviewDiameter / 2.0 - OFFSET;
183 break;
185 x = circlePreviewDiameter / 2.0 + OFFSET;
186 break;
187 default:
188 x = 0;
189 break;
190 }
191
192 QTransform tf;
193
194 qreal canvasRotationAngle = canvas->rotationAngle();
195 if (!qFuzzyIsNull(canvasRotationAngle)) {
196 tf.rotate(mirrored ? canvasRotationAngle : -canvasRotationAngle);
197 }
198
199 QPointF offset = tf.map(QPointF(x,y));
200
201 circleRect.translate(offset.x(), offset.y());
202 }
203
204 return circleRect;
205 }
206
207 QRectF colorPreviewDocRect(const QPointF &outlineDocPoint)
208 {
209 QRectF colorPreviewViewRect;
210 switch (style) {
212 return QRectF();
216 colorPreviewViewRect = colorPreviewRectForRectangle();
217 break;
218 default:
219 // Showing a preview without sampling a color (by just holding a
220 // modifier) is used to compare the foreground color with the
221 // canvas. The circle doesn't work well for that purpose, so we
222 // use the handedness-independent rectangle above instead.
223 if (haveSample) {
224 colorPreviewViewRect = colorPreviewRectForCircle();
225 } else {
226 colorPreviewViewRect = colorPreviewRectForRectangle();
227 }
228 break;
229 }
230
231 const QRectF colorPreviewDocumentRect = converter().viewToDocument(colorPreviewViewRect);
232 return colorPreviewDocumentRect.translated(outlineDocPoint);
233 }
234
235 QRect standardizeZoomPreviewPixelRect(QRect pixelRect) {
236 // If width and height get rounded to different number, the center will be stuck between pixel border
237 if (pixelRect.width() != pixelRect.height()) {
238 if (pixelRect.width() % 2 == 0) pixelRect.setWidth(pixelRect.height());
239 else pixelRect.setHeight(pixelRect.width());
240 }
241 // If width and height is even, the center will be stuck in pixel corner
242 else if (pixelRect.width() % 2 == 0) {
243 pixelRect.setWidth(pixelRect.width() - 1);
244 pixelRect.setHeight(pixelRect.height() - 1);
245 }
246
247 // Minimum sample size should be 3 to be meaningful
248 if (pixelRect.width() < 3 || pixelRect.height() < 3) pixelRect.setSize(QSize(3, 3));
249
250 return pixelRect;
251 }
252
255
257
258 int gapRadius = 3;
259 int crosshairRadius = circlePreviewDiameter / 2 * 0.2;
260 cacheCrosshairPath = QPainterPath(QPointF(-crosshairRadius, 0));
261
262 cacheCrosshairPath.lineTo(-gapRadius, 0);
263 cacheCrosshairPath.moveTo(gapRadius, 0);
264 cacheCrosshairPath.lineTo(crosshairRadius, 0);
265
266 QTransform tf;
267 tf.translate(0, 0);
268 tf.rotate(90);
269
270 cacheCrosshairPath.moveTo(0,0);
271
272 cacheCrosshairPath.addPath(tf.map(cacheCrosshairPath));
273
274 return cacheCrosshairPath;
275 }
276};
277
279 : m_d(new Private(canvas))
280{
281 using namespace std::placeholders; // For _1 placeholder
282 std::function<void(QPointF)> callback =
284 m_d->samplingCompressor.reset(
286
287 m_d->activationDelayTimer.setInterval(100);
288 m_d->activationDelayTimer.setSingleShot(true);
289 connect(&m_d->activationDelayTimer, SIGNAL(timeout()), this, SLOT(activateDelayedPreview()));
290 connect(m_d->canvas->displayColorConverter(), SIGNAL(displayConfigurationChanged()), this, SLOT(slotUpdateBgColor()));
292}
293
298
300{
301 return m_d->isActive;
302}
303
305{
306
308 m_d->isActive = true;
309
310 m_d->sampleResourceId =
311 pickFgColor ?
314
315 m_d->sampleSource = sampleSource;
316 m_d->haveSample = false;
317
318 KisConfig cfg(true);
319 m_d->style = cfg.colorSamplerPreviewStyle();
320
321 m_d->circlePreviewDiameter = cfg.colorSamplerPreviewCircleDiameter();
322 m_d->circlePreviewThickness = cfg.colorSamplerPreviewCircleThickness()/100.0; // saved in percentages
323 m_d->circlePreviewOutlineEnabled = cfg.colorSamplerPreviewCircleOutlineEnabled();
324 m_d->circlePreviewExtraCircles = cfg.colorSamplerPreviewCircleExtraCirclesEnabled();
325 m_d->circleZoomPreviewEnabled = cfg.colorSamplerZoomPreviewEnabled();
326 m_d->circleZoomPreviewScale = cfg.colorSamplerZoomPreviewScale();
327 m_d->circlePreviewPosition = cfg.colorSamplerPreviewCirclePosition();
328
329 m_d->activationDelayTimer.start();
330}
331
333{
334 // the event may come after we have started or even
335 // finished color picking if the user is quick
336 if (!m_d->isActive || m_d->showPreview) {
337 return;
338 }
339
341
343}
344
346{
347 m_d->activationDelayTimer.stop();
348 m_d->showPreview = true;
349
350 const KoColor currentColor =
351 m_d->canvas->resourceManager()->koColorResource(m_d->sampleResourceId);
352 const QColor previewColor = m_d->canvas->displayColorConverter()->convertColorToDisplayColorSpace(currentColor, true);
353
354 m_d->currentColor = previewColor;
355 m_d->baseColor = previewColor;
356 m_d->cache = QPixmap();
357
358 updateCursor(m_d->sampleSource, m_d->sampleResourceId == KoCanvasResource::ForegroundColor);
359}
360
362{
363 const int sampleResourceId =
364 pickFgColor ?
367
368 QCursor cursor;
369
370 switch (sampleSource) {
372 if (sampleResourceId == KoCanvasResource::ForegroundColor) {
374 } else {
376 }
377 break;
379 if (sampleResourceId == KoCanvasResource::ForegroundColor) {
381 } else {
383 }
384 break;
386 default:
387 if (sampleResourceId == KoCanvasResource::ForegroundColor) {
389 } else {
391 }
392 break;
393 }
394
395 Q_EMIT sigRequestCursor(cursor);
396}
397
399{
400 m_d->updateGlobalColor = value;
401}
402
404{
405 return m_d->updateGlobalColor;
406}
407
409{
410 KIS_SAFE_ASSERT_RECOVER(!m_d->strokeId) {
411 endAction();
412 }
413
414 m_d->activationDelayTimer.stop();
415
416 m_d->showPreview = false;
417 m_d->haveSample = false;
418
419 m_d->previewDocRect = QRectF();
420 m_d->currentColor = QColor();
421 m_d->baseColor = QColor();
422 m_d->cache = QPixmap();
423
424 // Reset the cached zoom preview image and rect
425 m_d->cacheCanvasPreviewImage = QImage();
426 m_d->cacheCanvasPreviewRect = QRect();
427 m_d->zoomPreviewHasPainted = false;
428 m_d->zoomDocRectF = QRectF();
429
430 m_d->isActive = false;
431
432 Q_EMIT sigRequestCursorReset();
434}
435
436void KisAsyncColorSamplerHelper::startAction(const QPointF &docPoint, int radius, int blend)
437{
445
447 m_d->haveSample = true;
448 m_d->strokeId = m_d->strokesFacade()->startStroke(strategy);
449 m_d->samplingCompressor->start(docPoint);
450}
451
453{
455 m_d->samplingCompressor->start(docPoint);
456}
457
459{
461
462 m_d->strokesFacade()->addJob(m_d->strokeId,
464
465 m_d->strokesFacade()->endStroke(m_d->strokeId);
466 m_d->strokeId.clear();
467}
468
470{
471 if (!m_d->showPreview) return QRectF();
472
473 m_d->sampleDocPoint = docPoint;
474
475 KisConfig cfg(true);
476 m_d->style = cfg.colorSamplerPreviewStyle();
477 QRectF previewDocRect = m_d->colorPreviewDocRect(docPoint);
478
479 if (previewDocRect != m_d->previewDocRect) prepareZoomPreview(previewDocRect);
480
481 m_d->previewDocRect = previewDocRect;
482
483 return m_d->previewDocRect;
484}
485
487 // This can be called before startAction(), so there may not be a strokeId
488 if (!m_d->circleZoomPreviewEnabled || !m_d->strokeId) return;
489
490 KisImageSP image = m_d->canvas->image();
491 if (!image) return;
492
493 // Prepare zoomed doc rect
494 QRectF zoomDocRectF = docRect;
495
496 zoomDocRectF.setSize(zoomDocRectF.size() / m_d->circleZoomPreviewScale);
497 zoomDocRectF.moveCenter(m_d->sampleDocPoint);
498
499 m_d->zoomDocRectF = zoomDocRectF;
500
501 // Prepare canvas preview because it takes time
502 QRectF canvasPixelRectF = image->documentToPixel(zoomDocRectF);
503 // Floor manually to avoid width != height when using toRect()
504 QRect canvasPixelRect = QRect(canvasPixelRectF.topLeft().toPoint(), QSize(qFloor(canvasPixelRectF.width()), qFloor(canvasPixelRectF.height())));
505
506 canvasPixelRect = m_d->standardizeZoomPreviewPixelRect(canvasPixelRect);
507 // Make sure the center is the pixel currently sampled because standardizing may change the shape
508 canvasPixelRect.moveCenter(image->documentToImagePixelFloored(zoomDocRectF.center()));
509
510 if (!image->bounds().intersects(canvasPixelRect) && !image->wrapAroundModeActive()) {
511 m_d->cacheCanvasPreviewRect = QRect();
512 m_d->cacheCanvasPreviewImage = QImage();
513 return;
514 }
515
516 // If not already have a job fetching canvas image, then do it
517 if (!m_d->canvasPreviewFetchCookie && m_d->cacheCanvasPreviewRect != canvasPixelRect) {
519 new KisColorSamplerStrokeStrategy::GenerateCanvasZoomPreviewData(image->projection(), canvasPixelRect, m_d->canvas->displayColorConverter());
520
521 m_d->canvasPreviewFetchCookie = data->cookie();
522
523 m_d->strokesFacade()->addJob(m_d->strokeId, data);
524 }
525}
526
527void KisAsyncColorSamplerHelper::paint(QPainter &gc, const KoViewConverter &converter)
528{
529 if (!m_d->showPreview) {
530 return;
531 }
532
533 QRectF viewRectF = converter.documentToView(m_d->previewDocRect);
534 QColor currentColor = colorWithAlpha(m_d->currentColor, OPACITY_OPAQUE_U8);
535 QColor baseColor = m_d->haveSample ? colorWithAlpha(m_d->baseColor, OPACITY_OPAQUE_U8) : currentColor;
536
537 switch (m_d->style) {
541 paintRectangle(gc, viewRectF, currentColor, baseColor);
542 break;
543 default:
544 // See comment in colorPreviewDocRect.
545 if (m_d->haveSample) {
546 paintCircle(gc, viewRectF, currentColor, baseColor);
547 } else {
548 paintRectangle(gc, viewRectF, currentColor, baseColor);
549 }
550 break;
551 }
552}
553
555 const QRectF &viewRectF,
556 const QColor &currentColor,
557 const QColor &baseColor)
558{
559 qreal dpr = gc.device()->devicePixelRatioF();
560 QSizeF cacheSizeF = viewRectF.size() * dpr;
561 QSize cacheSize(qCeil(cacheSizeF.width()), qCeil(cacheSizeF.height()));
562 bool needsNewCache = m_d->cache.isNull() || m_d->cache.size() != cacheSize;
563 if (needsNewCache) {
564 m_d->cache = QPixmap(cacheSize);
565 m_d->cache.fill(Qt::transparent);
566 }
567
568 qreal canvasRotationAngle = m_d->canvas->rotationAngle();
569 bool canvasMirror = m_d->canvas->xAxisMirrored();
570 if (needsNewCache || !qFuzzyCompare(canvasRotationAngle, m_d->cacheRotation) || canvasMirror != m_d->cacheMirror) {
571 m_d->cacheRotation = canvasRotationAngle;
572 m_d->cacheMirror = canvasMirror;
573
574 QPainter cachePainter(&m_d->cache);
575 cachePainter.setRenderHint(QPainter::Antialiasing);
576
577 qreal size = Private::PREVIEW_RECT_SIZE * dpr;
578 QRectF rect(0.0, 0.0, m_d->haveSample ? size * 2.0 : size, size);
579 rect.moveTopLeft(-rect.center());
580
581 QTransform tf;
582 QPointF offset = QRectF(m_d->cache.rect()).center();
583 tf.translate(offset.x(), offset.y());
584 tf.rotate(canvasMirror ? canvasRotationAngle : -canvasRotationAngle);
585 cachePainter.setTransform(tf);
586
587 if (m_d->haveSample) {
588 qreal centerX = rect.center().x();
589 QRectF currentRect(rect.topLeft(), QPointF(centerX + 1.0, rect.bottom()));
590 QRectF baseRect(QPointF(centerX, rect.top()), rect.bottomRight());
591 if (m_d->canvas->xAxisMirrored()) {
592 std::swap(currentRect, baseRect);
593 }
594 cachePainter.fillRect(currentRect, currentColor);
595 cachePainter.fillRect(baseRect, baseColor);
596 } else {
597 cachePainter.fillRect(rect, currentColor);
598 }
599 }
600
601 gc.drawPixmap(viewRectF.toRect(), m_d->cache);
602}
603
605 const QRectF &viewRectF,
606 const QColor &currentColor,
607 const QColor &baseColor)
608{
609 if (!m_d->haveSample) {
610 return;
611 }
612
613 gc.save();
614
615 qreal dpr = gc.device()->devicePixelRatioF();
616 QSizeF cacheSizeF = viewRectF.size() * dpr;
617 QSize cacheSize(qCeil(cacheSizeF.width()), qCeil(cacheSizeF.height()));
618 bool needsNewCache = m_d->cache.isNull() || m_d->cache.size() != cacheSize;
619 if (needsNewCache) {
620 m_d->cache = QPixmap(cacheSize);
621 m_d->cache.fill(Qt::transparent);
622 }
623
624 qreal canvasRotationAngle = m_d->canvas->rotationAngle();
625 if (m_d->canvas->xAxisMirrored()) {
626 canvasRotationAngle = -canvasRotationAngle;
627 }
628
629 QPainter cachePainter(&m_d->cache);
630 cachePainter.setRenderHint(QPainter::Antialiasing);
631
632 QRectF cacheRect = m_d->cache.rect();
633
634 // The color sampler preview is an outline and those rotate along
635 // with the canvas. That's undesirable for the sampler preview
636 // though, so we calculate the transformation to counter the rotation here
637 QPointF cacheCenter = cacheRect.center();
638 QTransform tf;
639 tf.translate(cacheCenter.x(), cacheCenter.y());
640 tf.rotate(-canvasRotationAngle);
641 tf.translate(-cacheCenter.x(), -cacheCenter.y());
642
643 bool needsDualColor = currentColor != baseColor;
644 if (needsNewCache || (needsDualColor && !qFuzzyCompare(m_d->cacheRotation, canvasRotationAngle))) {
645 m_d->cacheRotation = canvasRotationAngle;
646
647 QColor backgroundColor = colorWithAlpha(m_d->backgroundColor, OPACITY_OPAQUE_U8 / 2 + 1);
648 qreal penWidth = m_d->circlePreviewDiameter > 100 ? (2.0 * dpr) : (1.0 * dpr);
649 QPen pen = QPen(backgroundColor, penWidth);
650 if (m_d->circlePreviewOutlineEnabled) {
651 cachePainter.setPen(pen);
652 } else {
653 cachePainter.setPen(Qt::NoPen);
654 }
655
656 QRectF outerRect = cacheRect.marginsRemoved(QMarginsF(penWidth, penWidth, penWidth, penWidth));
657
658 if (needsDualColor) {
659 // The color sampler preview is an outline and those rotate along
660 // with the canvas. That's undesirable for the sampler preview
661 // though, so we un-rotate its contents here accordingly.
662
663 QPainterPath clipPath;
664 clipPath.addPolygon(tf.map(QPolygonF(QRectF(0, 0, cacheRect.width(), cacheRect.height() / 2.0 + 1.0))));
665 cachePainter.setClipPath(clipPath);
666
667 bool flipped = m_d->canvas->yAxisMirrored();
668 cachePainter.setBrush(flipped ? baseColor : currentColor);
669 cachePainter.drawEllipse(outerRect);
670
671 cachePainter.setBrush(baseColor);
672 clipPath.clear();
673 clipPath.addPolygon(
674 tf.map(QRectF(0, cacheRect.height() / 2.0, cacheRect.width(), cacheRect.height() / 2.0)));
675 cachePainter.setClipPath(clipPath);
676
677 cachePainter.setBrush(flipped ? currentColor : baseColor);
678 cachePainter.drawEllipse(outerRect);
679
680 cachePainter.setClipPath(QPainterPath(), Qt::NoClip);
681 } else {
682 cachePainter.setBrush(currentColor);
683 cachePainter.drawEllipse(outerRect);
684 }
685
686 qreal innerX = cacheRect.width() * (1.0 - m_d->circlePreviewThickness);
687 qreal innerY = cacheRect.height() * (1.0 - m_d->circlePreviewThickness);
688 QRectF innerRect = cacheRect.marginsRemoved(QMarginsF(innerX, innerY, innerX, innerY));
689 QPainterPath innerEllipse;
690 innerEllipse.addEllipse(innerRect);
691
692 QPainterPath innerPath;
693 innerPath.addPath(innerEllipse);
694
695 if (m_d->circlePreviewThickness < 0.5 && m_d->circlePreviewExtraCircles) {
696 qreal extraMargin = 0.1*m_d->circlePreviewThickness*innerRect.width(); // looks better
697 QPointF leftCenter = QPointF(innerRect.left() - extraMargin, innerRect.top() + innerRect.height()/2.0);
698 QPointF rightCenter = QPointF(innerRect.right() + extraMargin, innerRect.top() + innerRect.height()/2.0);
699
700 innerPath.setFillRule(Qt::OddEvenFill);
701 innerPath.addEllipse(leftCenter, m_d->circlePreviewThickness*cacheRect.width(), m_d->circlePreviewThickness*cacheRect.width());
702 innerPath.addEllipse(rightCenter, m_d->circlePreviewThickness*cacheRect.width(), m_d->circlePreviewThickness*cacheRect.width());
703
704 innerPath = innerPath.intersected(innerEllipse);
705 }
706
707 m_d->cacheCircleInnerClip = innerPath;
708
709 // Draw inner circle outline if enabled
710 if (m_d->circlePreviewOutlineEnabled) {
711 cachePainter.setBrush(Qt::transparent);
712 cachePainter.setPen(pen);
713 cachePainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
714 cachePainter.drawPath(tf.map(m_d->cacheCircleInnerClip));
715 }
716 }
717
718 cachePainter.setPen(Qt::NoPen);
719 cachePainter.setBrush(m_d->backgroundColor);
720
721 // Clear the center
722 cachePainter.setCompositionMode(QPainter::CompositionMode_Clear);
723 cachePainter.drawPath(tf.map(m_d->cacheCircleInnerClip));
724
725 // If canvas preview image is null and it's being fetched (which means canvas preview is needed),
726 // don't paint preview until it's done to avoid flickering
727 bool shouldPaintPreview = !(m_d->cacheCanvasPreviewImage.isNull() && m_d->canvasPreviewFetchCookie);
728
729 // But if zoom preview has already been painted, keeps painting to avoid flickering
730 if (m_d->circleZoomPreviewEnabled && (shouldPaintPreview || m_d->zoomPreviewHasPainted)) {
731 paintCircleCanvasPreview(cachePainter, cacheRect, tf.map(m_d->cacheCircleInnerClip));
732 paintCircleReferenceImagePreview(cachePainter, cacheRect, tf.map(m_d->cacheCircleInnerClip));
733
734 // Draw crosshair if preview is offseted
735 if (m_d->circlePreviewPosition != KisConfig::ColorSamplerPreviewCirclePosition::Center) {
736 paintCircleCrosshair(cachePainter, cacheRect, currentColor);
737 }
738
739 // Fill empty spaces to hide the underlying canvas
740 cachePainter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
741 cachePainter.drawPath(tf.map(m_d->cacheCircleInnerClip));
742
743 m_d->zoomPreviewHasPainted = true;
744 }
745
746 gc.drawPixmap(viewRectF.toRect(), m_d->cache);
747
748 gc.restore();
749}
750
751void KisAsyncColorSamplerHelper::paintCircleCrosshair(QPainter &gc, const QRectF &viewRectF, const QColor &currentColor)
752{
753 QColor crosshairColor = Qt::black;
754 qreal luminance = KisPaintingTweaks::luminosityCoarse(currentColor);
755 if (luminance < 0.5) crosshairColor = Qt::white;
756
757 gc.save();
758
759 gc.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
760 gc.setCompositionMode(QPainter::CompositionMode_SourceOver);
761 gc.setPen(crosshairColor);
762
763 QTransform tf;
764 tf.translate(viewRectF.center().x(), viewRectF.center().y());
765 tf.rotate(-m_d->cacheRotation);
766 gc.drawPath(tf.map(m_d->crosshairForOffsettedCircle()));
767
768 gc.restore();
769}
770
771void KisAsyncColorSamplerHelper::paintCircleCanvasPreview(QPainter &gc, const QRectF &viewRectF, const QPainterPath &clip) {
772 QRect canvasPixelRect = m_d->cacheCanvasPreviewRect;
773 QImage cachedImage = m_d->cacheCanvasPreviewImage;
774
775 if (cachedImage.isNull()) return;
776
777 gc.save();
778 gc.setCompositionMode(QPainter::CompositionMode_SourceOver);
779
780 gc.setClipPath(clip);
781
782 // QtDoc: The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
783 // Since the piece of canvas is (zoomPreviewScale) times smaller than cacheRect
784 // drawImage will scale it up that many times, thus achieving the zoom effect
785 gc.drawImage(viewRectF, cachedImage, canvasPixelRect);
786
787 gc.restore();
788}
789
790// Preview won't respect opacity because the color sampled doesn't respect opacity either
791// But if opacity = 0, preview for that image won't show at all
792void KisAsyncColorSamplerHelper::paintCircleReferenceImagePreview(QPainter &gc, const QRectF &viewRectF, const QPainterPath &clip) {
793 KisDocument *document = m_d->canvas->viewManager()->document();
794 if (!document || !document->referenceImagesLayer()) return;
795
796 gc.save();
797
798 gc.setCompositionMode(QPainter::CompositionMode_SourceOver);
799 gc.setClipPath(clip);
800
801 QRectF zoomDocRectF = m_d->zoomDocRectF;
802
803 QList<KoShape*> shapeList = document->referenceImagesLayer()->shapeManager()->shapesAt(zoomDocRectF);
804 if (shapeList.size() > 1) std::sort(shapeList.begin(), shapeList.end(), KoShape::compareShapeZIndex);
805
806 Q_FOREACH(KoShape *shape, shapeList) {
807 KisReferenceImage *refImage = dynamic_cast<KisReferenceImage*>(shape);
808 if (!refImage) continue;
809
810 if (refImage->transparency() == 1.0) continue;
811
812 gc.save();
813
814 QImage image = refImage->getCachedImage();
815
816 QPoint refPixelPoint = refImage->documentToPixelFloored(zoomDocRectF.center());
817
818 // Avoid using KisReferenceImage::documentToPixel because it use absoluteTransformation.invert()
819 // Which will take rotation into account and cause some quirks when ref image is rotated
820 qreal xScale = refImage->size().width() / image.width();
821 qreal yScale = refImage->size().height() / image.height();
822
823 // This only happens when refImage size is 0, so painting it probably won't make senses
824 if (xScale == 0 || yScale == 0) continue;
825
826 QRect refPixelRect = QRect(refPixelPoint, QSize(zoomDocRectF.width() / xScale, zoomDocRectF.height() / yScale));
827
828 refPixelRect = m_d->standardizeZoomPreviewPixelRect(refPixelRect);
829
830 refPixelRect.moveCenter(refPixelPoint);
831
832 // Rotate the painter, draw, rotate back is seemingly easier than
833 // trying to rotate the image and the shenanighens that follow
834 gc.translate(viewRectF.center());
835 gc.rotate(refImage->rotation());
836 gc.translate(-viewRectF.center());
837
838 gc.drawImage(viewRectF, image, refPixelRect);
839
840 gc.restore();
841 }
842
843 gc.restore();
844}
845
846void KisAsyncColorSamplerHelper::slotCanvasZoomPreviewUpdated(const QImage &canvasImage, QRect canvasRect) {
847 m_d->cacheCanvasPreviewRect = canvasRect;
848 m_d->cacheCanvasPreviewImage = canvasImage;
849
851}
852
854{
859 if (!m_d->strokeId) return;
860
861 KisImageSP image = m_d->canvas->image();
862 const QPoint imagePoint = image->documentToImagePixelFloored(docPoint);
863
864 if (m_d->sampleSource == KisToolUtils::SampleSourceImage) {
865 KisSharedPtr<KisReferenceImagesLayer> referencesLayer = m_d->canvas->imageView()->document()->referenceImagesLayer();
866 if (referencesLayer && m_d->canvas->referenceImagesDecoration()->visible()) {
867 KoColor color = referencesLayer->getPixelForDocPoint(docPoint);
868 if (color.opacityU8() > 0) {
870 return;
871 }
872 }
873 }
874
875 KisPaintDeviceSP device;
876
877 switch (m_d->sampleSource) {
879 device = m_d->canvas->imageView()->currentNode()->parent()->colorSampleSourceDevice();
880 break;
882 device = m_d->canvas->imageView()->currentNode()->colorSampleSourceDevice();
883 break;
885 default:
886 device = image->projection();
887 break;
888 }
889
890 if (device) {
891 // Used for color sampler blending.
892 const KoColor currentColor =
893 m_d->canvas->resourceManager()->koColorResource(m_d->sampleResourceId);
894
895 m_d->strokesFacade()->addJob(m_d->strokeId,
896 new KisColorSamplerStrokeStrategy::Data(device, imagePoint, currentColor));
897 } else {
898 QString message = i18n("Color sampler does not work on this layer.");
899 m_d->canvas->viewManager()->showFloatingMessage(message, koIcon("object-locked"));
900 }
901}
902
904{
905 KoColor color(rawColor);
906
908
909 if (m_d->updateGlobalColor) {
910 m_d->canvas->resourceManager()->setResource(m_d->sampleResourceId, color);
911 }
912
913 Q_EMIT sigRawColorSelected(rawColor);
914 Q_EMIT sigColorSelected(color);
915
916 if (!m_d->showPreview) return;
917
918 const QColor previewColor = m_d->canvas->displayColorConverter()->convertColorToDisplayColorSpace(color, true);
919
920 if (!m_d->haveSample || m_d->currentColor != previewColor) {
921 m_d->haveSample = true;
922 m_d->currentColor = previewColor;
923 m_d->cache = QPixmap();
924 }
925
927}
928
930{
931 KoColor bgColor;
932 bgColor.fromQColor(qApp->palette().color(QPalette::Base));
933 m_d->backgroundColor = m_d->canvas->displayColorConverter()->convertColorToDisplayColorSpace(bgColor);
934}
float value(const T *src, size_t ch)
const quint8 OPACITY_OPAQUE_U8
void paintCircleCrosshair(QPainter &gc, const QRectF &viewRectF, const QColor &currentColor)
void updateCursor(KisToolUtils::ColorSamplerSource sampleSource, bool pickFgColor)
void slotColorSamplingFinished(const KoColor &rawColor)
void sigFinalColorSelected(const KoColor &color)
void slotAddSamplingJob(const QPointF &docPoint)
void sigRequestCursor(const QCursor &cursor)
void paintRectangle(QPainter &gc, const QRectF &viewRectF, const QColor &currentColor, const QColor &baseColor)
void paint(QPainter &gc, const KoViewConverter &converter)
void slotCanvasZoomPreviewUpdated(const QImage &canvasImage, QRect canvasRect)
void prepareZoomPreview(const QRectF &docRect)
void continueAction(const QPointF &docPoint)
void startAction(const QPointF &docPoint, int radius, int blend)
void sigColorSelected(const KoColor &color)
void activate(KisToolUtils::ColorSamplerSource sampleSource, bool pickFgColor)
void paintCircleReferenceImagePreview(QPainter &gc, const QRectF &viewRectF, const QPainterPath &clip)
void sigRawColorSelected(const KoColor &color)
void paintCircle(QPainter &gc, const QRectF &viewRectF, const QColor &currentColor, const QColor &baseColor)
void paintCircleCanvasPreview(QPainter &gc, const QRectF &viewRectF, const QPainterPath &clip)
QRectF colorPreviewDocRect(const QPointF &docPoint)
qreal rotationAngle() const
canvas rotation in degrees
bool xAxisMirrored() const
Bools indicating canvasmirroring.
KisImageWSP image() const
KisViewManager * viewManager() const
QPointer< KisView > imageView() const
bool yAxisMirrored() const
void sigCanvasZoomPreviewUpdated(const QImage &canvasImage, const QRect &canvasRect)
void sigFinalColorSelected(const KoColor &color)
void sigColorUpdated(const KoColor &color)
ColorSamplerPreviewCirclePosition colorSamplerPreviewCirclePosition(bool defaultValue=false) const
bool colorSamplerPreviewCircleOutlineEnabled(bool defaultValue=false) const
bool colorSamplerZoomPreviewEnabled(bool defaultValue=false) const
int colorSamplerPreviewCircleDiameter(bool defaultValue=false) const
ColorSamplerPreviewStyle
Definition kis_config.h:138
qreal colorSamplerPreviewCircleThickness(bool defaultValue=false) const
ColorSamplerPreviewCirclePosition
Definition kis_config.h:168
bool colorSamplerPreviewCircleExtraCirclesEnabled(bool defaultValue=false) const
ColorSamplerPreviewStyle colorSamplerPreviewStyle(bool defaultValue=false) const
qreal colorSamplerZoomPreviewScale(bool defaultValue=false) const
static QCursor samplerImageBackgroundCursor()
static QCursor samplerGroupForegroundCursor()
static QCursor samplerLayerBackgroundCursor()
static QCursor samplerLayerForegroundCursor()
static QCursor samplerGroupBackgroundCursor()
static QCursor samplerImageForegroundCursor()
bool wrapAroundModeActive() const
QPointF documentToPixel(const QPointF &documentCoord) const
KisPaintDeviceSP projection() const
QPoint documentToImagePixelFloored(const QPointF &documentCoord) const
QRect bounds() const override
The KisReferenceImage class represents a single reference image.
QPoint documentToPixelFloored(const QPointF &docPoint)
KisDocument * document() const
void setOpacity(quint8 alpha)
Definition KoColor.cpp:333
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
quint8 opacityU8() const
Definition KoColor.cpp:341
virtual QSizeF size() const
Get the size of the shape in pt.
Definition KoShape.cpp:735
qreal rotation() const
Definition KoShape.cpp:715
static bool compareShapeZIndex(KoShape *s1, KoShape *s2)
Definition KoShape.cpp:388
qreal transparency(bool recursive=false) const
Definition KoShape.cpp:645
virtual QPointF viewToDocument(const QPointF &viewPoint) const
virtual QPointF documentToView(const QPointF &documentPoint) const
static bool qFuzzyCompare(half p1, half p2)
static bool qFuzzyIsNull(half h)
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
#define koIcon(name)
Use these macros for icons without any issues.
Definition kis_icon.h:25
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
qreal luminosityCoarse(const QColor &c, bool sRGBtrc)
luminosityCoarse This calculates the luminosity of the given QColor. It uses a very coarse (10 step) ...
@ BackgroundColor
The active background color selected for this canvas.
@ ForegroundColor
The active foreground color selected for this canvas.
KisToolUtils::ColorSamplerSource sampleSource
KisConfig::ColorSamplerPreviewCirclePosition circlePreviewPosition
KisConfig::ColorSamplerPreviewStyle style
QScopedPointer< SamplingCompressor > samplingCompressor
QRectF colorPreviewDocRect(const QPointF &outlineDocPoint)
KisSignalCompressorWithParam< QPointF > SamplingCompressor
QWeakPointer< boost::none_t > canvasPreviewFetchCookie
KisCanvas2 * canvas