Krita Source Code Documentation
Loading...
Searching...
No Matches
KisGradientMapFilterNearestCachedGradient.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE project
3 *
4 * SPDX-FileCopyrightText: 2016 Spencer Brown <sbrown655@gmail.com>
5 * SPDX-FileCopyrightText: 2020 Deif Lou <ginoba@gmail.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10#include <KoAbstractGradient.h>
11#include <KoStopGradient.h>
12#include <KoSegmentGradient.h>
13#include <KoColorSpace.h>
14
16
18 : m_max(steps - 1)
19 , m_black(KoColor(cs))
20{
21 if (dynamic_cast<KoStopGradient*>(gradient.data())) {
22 KoStopGradient *stopGradient = static_cast<KoStopGradient*>(gradient.data());
23 for (qint32 i = 0; i < steps; i++) {
24 qreal t = static_cast<qreal>(i) / m_max;
25 KoGradientStop leftStop, rightStop;
26 if (!stopGradient->stopsAt(leftStop, rightStop, t)) {
28 } else {
29 if (std::abs(t - leftStop.position) < std::abs(t - rightStop.position)) {
30 m_colors << leftStop.color.convertedTo(cs);
31 } else {
32 m_colors << rightStop.color.convertedTo(cs);
33 }
34 }
35 }
36 } else if (dynamic_cast<KoSegmentGradient*>(gradient.data())) {
37 KoSegmentGradient *segmentGradient = static_cast<KoSegmentGradient*>(gradient.data());
38 for (qint32 i = 0; i < steps; i++) {
39 qreal t = static_cast<qreal>(i) / m_max;
40 KoGradientSegment *segment = segmentGradient->segmentAt(t);
41 if (!segment) {
43 } else {
44 if (std::abs(t - segment->startOffset()) < std::abs(t - segment->endOffset())) {
45 m_colors << segment->startColor().convertedTo(cs);
46 } else {
47 m_colors << segment->endColor().convertedTo(cs);
48 }
49 }
50 }
51 }
52
53}
54
56{
57 qint32 tInt = t * m_max + 0.5;
58 if (m_colors.size() > tInt) {
59 return m_colors[tInt].data();
60 } else {
61 return m_black.data();
62 }
63}
const quint8 * cachedAt(qreal t) const
gets the color data at position 0 <= t <= 1
KisGradientMapFilterNearestCachedGradient(const KoAbstractGradientSP gradient, qint32 steps, const KoColorSpace *cs)
KoColor convertedTo(const KoColorSpace *cs, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags) const
Definition KoColor.cpp:163
quint8 * data()
Definition KoColor.h:144
Write API docs here.
const KoColor & startColor() const
const KoColor & endColor() const
KoGradientSegment * segmentAt(qreal t) const
bool stopsAt(KoGradientStop &leftStop, KoGradientStop &rightStop, qreal t) const
Find stops surrounding position, returns false if position outside gradient.