File indexing completed on 2025-01-26 04:10:38
0001 /* 0002 * This file is part of the KDE project 0003 * 0004 * SPDX-FileCopyrightText: 2016 Spencer Brown <sbrown655@gmail.com> 0005 * SPDX-FileCopyrightText: 2020 Deif Lou <ginoba@gmail.com> 0006 * 0007 * SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include <KoAbstractGradient.h> 0011 #include <KoStopGradient.h> 0012 #include <KoSegmentGradient.h> 0013 #include <KoColorSpace.h> 0014 0015 #include "KisGradientMapFilterNearestCachedGradient.h" 0016 0017 KisGradientMapFilterNearestCachedGradient::KisGradientMapFilterNearestCachedGradient(const KoAbstractGradientSP gradient, qint32 steps, const KoColorSpace *cs) 0018 : m_max(steps - 1) 0019 , m_black(KoColor(cs)) 0020 { 0021 if (dynamic_cast<KoStopGradient*>(gradient.data())) { 0022 KoStopGradient *stopGradient = static_cast<KoStopGradient*>(gradient.data()); 0023 for (qint32 i = 0; i < steps; i++) { 0024 qreal t = static_cast<qreal>(i) / m_max; 0025 KoGradientStop leftStop, rightStop; 0026 if (!stopGradient->stopsAt(leftStop, rightStop, t)) { 0027 m_colors << m_black; 0028 } else { 0029 if (std::abs(t - leftStop.position) < std::abs(t - rightStop.position)) { 0030 m_colors << leftStop.color.convertedTo(cs); 0031 } else { 0032 m_colors << rightStop.color.convertedTo(cs); 0033 } 0034 } 0035 } 0036 } else if (dynamic_cast<KoSegmentGradient*>(gradient.data())) { 0037 KoSegmentGradient *segmentGradient = static_cast<KoSegmentGradient*>(gradient.data()); 0038 for (qint32 i = 0; i < steps; i++) { 0039 qreal t = static_cast<qreal>(i) / m_max; 0040 KoGradientSegment *segment = segmentGradient->segmentAt(t); 0041 if (!segment) { 0042 m_colors << m_black; 0043 } else { 0044 if (std::abs(t - segment->startOffset()) < std::abs(t - segment->endOffset())) { 0045 m_colors << segment->startColor().convertedTo(cs); 0046 } else { 0047 m_colors << segment->endColor().convertedTo(cs); 0048 } 0049 } 0050 } 0051 } 0052 0053 } 0054 0055 const quint8* KisGradientMapFilterNearestCachedGradient::cachedAt(qreal t) const 0056 { 0057 qint32 tInt = t * m_max + 0.5; 0058 if (m_colors.size() > tInt) { 0059 return m_colors[tInt].data(); 0060 } else { 0061 return m_black.data(); 0062 } 0063 }