File indexing completed on 2024-12-22 04:11:42
0001 /* 0002 * KDE. Krita Project. 0003 * 0004 * SPDX-FileCopyrightText: 2020 Deif Lou <ginoba@gmail.com> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #ifndef KISGRADIENTCONVERSION_H 0010 #define KISGRADIENTCONVERSION_H 0011 0012 #include <QGradient> 0013 0014 #include <KoAbstractGradient.h> 0015 #include <KoStopGradient.h> 0016 #include <KoSegmentGradient.h> 0017 #include <KoCanvasResourcesInterface.h> 0018 #include "kritapigment_export.h" 0019 0020 /** 0021 * @brief Namespace containing functions to convert to/from different types of gradients 0022 * 0023 */ 0024 namespace KisGradientConversion 0025 { 0026 /** 0027 * @brief Convert a KoAbstractGradientSP to a QGradientStop list 0028 * 0029 * This function makes use of toQGradientStops(KoStopGradientSP, KoCanvasResourcesInterfaceSP) 0030 * and toQGradientStops(KoSegmentGradientSP, KoCanvasResourcesInterfaceSP) 0031 * 0032 * @param gradient A KoAbstractGradientSP to convert from 0033 * @param canvasResourcesInterface KoCanvasResourcesInterfaceSP used if some of the gradient 0034 * stops should take the foreground/background color 0035 * @return QGradientStop list containing the positions and colors of the gradient 0036 */ 0037 KRITAPIGMENT_EXPORT QGradientStops toQGradientStops(KoAbstractGradientSP gradient, 0038 KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr); 0039 0040 /** 0041 * @brief Convert a KoStopGradientSP to a QGradientStop list 0042 * 0043 * For each stop in the stop gradient a QGradientStop is created. 0044 * 0045 * To convert stops that have FOREGROUNDSTOP or BACKGROUNDSTOP type, 0046 * the canvasResourcesInterface is used. If the canvasResourcesInterface 0047 * is null, the color field of the stop is used 0048 * 0049 * @param gradient A KoStopGradientSP to convert from 0050 * @param canvasResourcesInterface KoCanvasResourcesInterfaceSP used if some of the gradient 0051 * stops should take the foreground/background color 0052 * @return QGradientStop list containing the positions and colors of the gradient 0053 */ 0054 KRITAPIGMENT_EXPORT QGradientStops toQGradientStops(KoStopGradientSP gradient, 0055 KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr); 0056 0057 /** 0058 * @brief Convert a KoSegmentGradientSP to a QGradientStop list 0059 * 0060 * For each segment in the segment gradient two QGradientStop are created, 0061 * one for the start point and another one for the end point. 0062 * 0063 * To convert end points that have FOREGROUND_ENDPOINT, BACKGROUND_ENDPOINT, 0064 * FOREGROUND_TRANSPARENT_ENDPOINT or BACKGROUND_TRANSPARENT_ENDPOINT type, 0065 * the canvasResourcesInterface is used. If the canvasResourcesInterface 0066 * is null, the color field of the end point is used. 0067 * 0068 * The opacity of the QGradientStop color is set to 0 if the end point type is 0069 * FOREGROUND_TRANSPARENT_ENDPOINT or BACKGROUND_TRANSPARENT_ENDPOINT. 0070 * 0071 * If two QGradientStop turn out to be in the same position and have the 0072 * same color, they are collapsed and only one stop is added to the list 0073 * (This prevents having duplicated stops due to the end point of a segment 0074 * being equal to the start point of the next segment). 0075 * 0076 * The middle point, interpolation and color interpolation of the segment 0077 * are ignored, so some information may be lost 0078 * 0079 * @param gradient A KoSegmentGradientSP to convert from 0080 * @param canvasResourcesInterface KoCanvasResourcesInterfaceSP used if some of the gradient 0081 * stops should take the foreground/background color 0082 * @return QGradientStop list containing the positions and colors of the gradient 0083 */ 0084 KRITAPIGMENT_EXPORT QGradientStops toQGradientStops(KoSegmentGradientSP gradient, 0085 KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr); 0086 0087 /** 0088 * @brief Convert a KoAbstractGradientSP to a QGradient 0089 * 0090 * This function makes use of toQGradient(KoStopGradientSP, KoCanvasResourcesInterfaceSP) 0091 * and toQGradient(KoSegmentGradientSP, KoCanvasResourcesInterfaceSP) 0092 * 0093 * @param gradient A KoAbstractGradientSP to convert from 0094 * @param canvasResourcesInterface KoCanvasResourcesInterfaceSP used if some of the gradient 0095 * stops should take the foreground/background color 0096 * @return QGradient containing stops with the positions and colors of the gradient 0097 */ 0098 KRITAPIGMENT_EXPORT QGradient* toQGradient(KoAbstractGradientSP gradient, 0099 KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr); 0100 0101 /** 0102 * @brief Convert a KoStopGradientSP to a QGradient 0103 * 0104 * This function doesn't use the stop gradient's type and start/stop 0105 * positions to create different types of QGradient. This just 0106 * creates a QLinearGradient and sets its stops using 0107 * toQGradientStops(KoStopGradientSP, KoCanvasResourcesInterfaceSP). 0108 * To get the correct type of QGradient use KoStopGradient::toQGradient() 0109 * 0110 * @param gradient A KoStopGradientSP to convert from 0111 * @param canvasResourcesInterface KoCanvasResourcesInterfaceSP used if some of the gradient 0112 * stops should take the foreground/background color 0113 * @return QGradient containing stops with the positions and colors of the gradient 0114 */ 0115 KRITAPIGMENT_EXPORT QGradient* toQGradient(KoStopGradientSP gradient, 0116 KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr); 0117 0118 /** 0119 * @brief Convert a KoSegmentGradientSP to a QGradient 0120 * 0121 * Creates a QLinearGradient and sets its stops using 0122 * toQGradientStops(KoSegmentGradientSP, KoCanvasResourcesInterfaceSP) 0123 * 0124 * @param gradient A KoSegmentGradientSP to convert from 0125 * @param canvasResourcesInterface KoCanvasResourcesInterfaceSP used if some of the gradient 0126 * stops should take the foreground/background color 0127 * @return QGradient containing stops with the positions and colors of the gradient 0128 */ 0129 KRITAPIGMENT_EXPORT QGradient* toQGradient(KoSegmentGradientSP gradient, 0130 KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr); 0131 0132 /** 0133 * @brief Convert a QGradientStop list to a krita abstract gradient 0134 * 0135 * This function makes use of toStopGradient(const QGradientStops &) 0136 * to create a KoStopGradientSP that is casted to a KoAbstractGradientSP 0137 * 0138 * @param gradient A QGradientStop list with the stops of the gradient 0139 * @return KoAbstractGradientSP containing stops with the positions and colors of the gradient 0140 */ 0141 KRITAPIGMENT_EXPORT KoAbstractGradientSP toAbstractGradient(const QGradientStops &gradient); 0142 0143 /** 0144 * @brief Convert the stops of a QGradient to a krita abstract gradient 0145 * 0146 * This function makes use of toStopGradient(const QGradientStops &) 0147 * to create a KoStopGradientSP that is casted to a KoAbstractGradientSP 0148 * 0149 * @param gradient A QGradient with the stops of the gradient 0150 * @return KoAbstractGradientSP containing stops with the positions and colors of the gradient 0151 */ 0152 KRITAPIGMENT_EXPORT KoAbstractGradientSP toAbstractGradient(const QGradient *gradient); 0153 0154 /** 0155 * @brief Create a clone of a KoStopGradientSP and return it casted to a 0156 * krita abstract gradient 0157 * 0158 * @param gradient A KoStopGradientSP gradient to convert from 0159 * @return A clone of the input gradient casted to KoAbstractGradientSP 0160 */ 0161 KRITAPIGMENT_EXPORT KoAbstractGradientSP toAbstractGradient(KoStopGradientSP gradient); 0162 0163 /** 0164 * @brief Create a clone of a KoSegmentGradientSP and return it casted to a 0165 * krita abstract gradient 0166 * 0167 * @param gradient A KoSegmentGradientSP gradient to convert from 0168 * @return A clone of the input gradient casted to KoAbstractGradientSP 0169 */ 0170 KRITAPIGMENT_EXPORT KoAbstractGradientSP toAbstractGradient(KoSegmentGradientSP gradient); 0171 0172 /** 0173 * @brief Convert a QGradientStop list to a KoStopGradientSP 0174 * 0175 * @param gradient A QGradientStop list with the stops of the gradient 0176 * @return KoStopGradientSP containing stops with the positions and colors of the gradient 0177 */ 0178 KRITAPIGMENT_EXPORT KoStopGradientSP toStopGradient(const QGradientStops &gradient); 0179 0180 /** 0181 * @brief Convert the stops of a QGradient to a KoStopGradientSP 0182 * 0183 * @param gradient A QGradient with the stops of the gradient 0184 * @return KoStopGradientSP containing stops with the positions and colors of the gradient 0185 */ 0186 KRITAPIGMENT_EXPORT KoStopGradientSP toStopGradient(const QGradient *gradient); 0187 0188 /** 0189 * @brief Convert a KoAbstractGradientSP to a KoStopGradientSP 0190 * 0191 * If the underlying gradient is a segment gradient, 0192 * toStopGradient(KoSegmentGradientSP, KoCanvasResourcesInterfaceSP) 0193 * is used; otherwise, if it is a stop gradient, a clone is returned 0194 * 0195 * @param gradient A KoAbstractGradientSP to convert from 0196 * @return KoStopGradientSP containing stops with the positions and colors of the gradient 0197 */ 0198 KRITAPIGMENT_EXPORT KoStopGradientSP toStopGradient(KoAbstractGradientSP gradient, 0199 KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr); 0200 0201 /** 0202 * @brief Convert a KoSegmentGradientSP to a KoStopGradientSP 0203 * 0204 * For each segment in the segment gradient two stops are created, 0205 * one for the start point and another one for the end point. 0206 * 0207 * To convert end points that have FOREGROUND_TRANSPARENT_ENDPOINT or 0208 * BACKGROUND_TRANSPARENT_ENDPOINT type, the stop type is set to the COLORSTOP 0209 * and the canvasResourcesInterface is used to set the stop color if it is 0210 * not null. If the canvasResourcesInterface is null, the color field of 0211 * the end point is used. 0212 * 0213 * The opacity of the stop color is set to 0 if the end point type is 0214 * FOREGROUND_TRANSPARENT_ENDPOINT or BACKGROUND_TRANSPARENT_ENDPOINT. 0215 * 0216 * If two stops turn out to be in the same position and have the 0217 * same type and color, they are collapsed and only one stop is added to the list 0218 * (This prevents having duplicated stops due to the end point of a segment 0219 * being equal to the start point of the next segment). 0220 * 0221 * The middle point, interpolation and color interpolation of the segment 0222 * are ignored, so some information may be lost 0223 * 0224 * @param gradient A KoSegmentGradientSP gradient to convert from 0225 * @param canvasResourcesInterface KoCanvasResourcesInterfaceSP used if some of the gradient 0226 * end points have FOREGROUND_TRANSPARENT_ENDPOINT or 0227 * BACKGROUND_TRANSPARENT_ENDPOINT type 0228 * @return KoStopGradientSP containing stops with the positions and colors of the gradient 0229 */ 0230 KRITAPIGMENT_EXPORT KoStopGradientSP toStopGradient(KoSegmentGradientSP gradient, 0231 KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr); 0232 0233 /** 0234 * @brief Convert a QGradientStop list to a krita segment gradient 0235 * 0236 * If two stops have the same position a new segment between them is not created 0237 * 0238 * @param gradient A QGradientStop list with the stops of the gradient 0239 * @return KoSegmentGradientSP containing segments with the positions and colors of the gradient 0240 */ 0241 KRITAPIGMENT_EXPORT KoSegmentGradientSP toSegmentGradient(const QGradientStops &gradient); 0242 0243 /** 0244 * @brief Convert a QGradient to a krita segment gradient 0245 * 0246 * Creates a segment gradient from the stops of the QGradient using 0247 * toSegmentGradient(const QGradientStops &) 0248 * 0249 * @param gradient A QGradient with the stops of the gradient 0250 * @return KoSegmentGradientSP containing segments with the positions and colors of the gradient 0251 */ 0252 KRITAPIGMENT_EXPORT KoSegmentGradientSP toSegmentGradient(const QGradient *gradient); 0253 0254 /** 0255 * @brief Convert a krita abstract gradient to a krita segment gradient 0256 * 0257 * If the underlying gradient is a stop gradient, toSegmentGradient(KoStopGradientSP) 0258 * is used; otherwise, if it is a segment gradient, a clone is returned 0259 * 0260 * @param gradient A KoAbstractGradientSP to convert from 0261 * @return KoSegmentGradientSP containing segments with the positions and colors of the gradient 0262 */ 0263 KRITAPIGMENT_EXPORT KoSegmentGradientSP toSegmentGradient(KoAbstractGradientSP gradient); 0264 0265 /** 0266 * @brief Convert a krita stop gradient to a krita segment gradient 0267 * 0268 * If two stops have the same position a new segment is not created 0269 * 0270 * @param gradient A KoStopGradientSP to convert from 0271 * @return KoSegmentGradientSP containing segments with the positions and colors of the gradient 0272 */ 0273 KRITAPIGMENT_EXPORT KoSegmentGradientSP toSegmentGradient(KoStopGradientSP gradient); 0274 } 0275 0276 #endif