chalk

kis_gradient.h
1/*
2 * kis_gradient.h - part of Krayon
3 *
4 * Copyright (c) 2000 Matthias Elter <elter@kde.org>
5 * 2004 Boudewijn Rempt <boud@valdyas.org>
6 * 2004 Adrian Page <adrian@pagenet.plus.com>
7 * 2004 Sven Langkamp <longamp@reallygood.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
24#ifndef KIS_GRADIENT_H
25#define KIS_GRADIENT_H
26
27#include <tqvaluevector.h>
28#include <tqcolor.h>
29
30#include <tdeio/job.h>
31
32#include "kis_resource.h"
33#include "kis_global.h"
34
35class TQImage;
36
37enum {
38 INTERP_LINEAR = 0,
39 INTERP_CURVED,
40 INTERP_SINE,
41 INTERP_SPHERE_INCREASING,
42 INTERP_SPHERE_DECREASING
43};
44
45enum {
46 COLOR_INTERP_RGB,
47 COLOR_INTERP_HSV_CCW,
48 COLOR_INTERP_HSV_CW
49};
50
51// TODO: Replace TQColor with KisColor
52class Color {
53 public:
54 Color() { m_alpha = 0; }
55 Color(const TQColor& color, double alpha) { m_color = color; m_alpha = alpha; }
56
57 const TQColor& color() const { return m_color; }
58 double alpha() const { return m_alpha; }
59
60 private:
61 TQColor m_color;
62 double m_alpha;
63};
64
65class KisGradientSegment {
66 public:
67 KisGradientSegment(int interpolationType, int colorInterpolationType, double startOffset, double middleOffset, double endOffset, const Color& startColor, const Color& endColor);
68
69 // startOffset <= t <= endOffset
70 Color colorAt(double t) const;
71
72 const Color& startColor() const;
73 const Color& endColor() const;
74
75 void setStartColor(const Color& color) { m_startColor = color; }
76 void setEndColor(const Color& color) { m_endColor = color; }
77
78 double startOffset() const;
79 double middleOffset() const;
80 double endOffset() const;
81
82 void setStartOffset(double t);
83 void setMiddleOffset(double t);
84 void setEndOffset(double t);
85
86 double length() { return m_length; }
87
88 int interpolation() const;
89 int colorInterpolation() const;
90
91 void setInterpolation(int interpolationType);
92 void setColorInterpolation(int colorInterpolationType);
93
94 bool isValid() const;
95 protected:
96
97 class ColorInterpolationStrategy {
98 public:
99 ColorInterpolationStrategy() {}
100 virtual ~ColorInterpolationStrategy() {}
101
102 virtual Color colorAt(double t, Color start, Color end) const = 0;
103 virtual int type() const = 0;
104 };
105
106 class RGBColorInterpolationStrategy : public ColorInterpolationStrategy {
107 public:
108 static RGBColorInterpolationStrategy *instance();
109
110 virtual Color colorAt(double t, Color start, Color end) const;
111 virtual int type() const { return COLOR_INTERP_RGB; }
112
113 private:
114 RGBColorInterpolationStrategy() {}
115
116 static RGBColorInterpolationStrategy *m_instance;
117 };
118
119 class HSVCWColorInterpolationStrategy : public ColorInterpolationStrategy {
120 public:
121 static HSVCWColorInterpolationStrategy *instance();
122
123 virtual Color colorAt(double t, Color start, Color end) const;
124 virtual int type() const { return COLOR_INTERP_HSV_CW; }
125 private:
126 HSVCWColorInterpolationStrategy() {}
127
128 static HSVCWColorInterpolationStrategy *m_instance;
129 };
130
131 class HSVCCWColorInterpolationStrategy : public ColorInterpolationStrategy {
132 public:
133 static HSVCCWColorInterpolationStrategy *instance();
134
135 virtual Color colorAt(double t, Color start, Color end) const;
136 virtual int type() const { return COLOR_INTERP_HSV_CCW; }
137 private:
138 HSVCCWColorInterpolationStrategy() {}
139
140 static HSVCCWColorInterpolationStrategy *m_instance;
141 };
142
143 class InterpolationStrategy {
144 public:
145 InterpolationStrategy() {}
146 virtual ~InterpolationStrategy() {}
147
148 virtual double valueAt(double t, double middle) const = 0;
149 virtual int type() const = 0;
150 };
151
152 class LinearInterpolationStrategy : public InterpolationStrategy {
153 public:
154 static LinearInterpolationStrategy *instance();
155
156 virtual double valueAt(double t, double middle) const;
157 virtual int type() const { return INTERP_LINEAR; }
158
159 // This does the actual calculation and is made
160 // static as an optimisation for the other
161 // strategies that need this for their own calculation.
162 static double calcValueAt(double t, double middle);
163
164 private:
165 LinearInterpolationStrategy() {}
166
167 static LinearInterpolationStrategy *m_instance;
168 };
169
170 class CurvedInterpolationStrategy : public InterpolationStrategy {
171 public:
172 static CurvedInterpolationStrategy *instance();
173
174 virtual double valueAt(double t, double middle) const;
175 virtual int type() const { return INTERP_CURVED; }
176 private:
177 CurvedInterpolationStrategy();
178
179 static CurvedInterpolationStrategy *m_instance;
180 double m_logHalf;
181 };
182
183 class SphereIncreasingInterpolationStrategy : public InterpolationStrategy {
184 public:
185 static SphereIncreasingInterpolationStrategy *instance();
186
187 virtual double valueAt(double t, double middle) const;
188 virtual int type() const { return INTERP_SPHERE_INCREASING; }
189 private:
190 SphereIncreasingInterpolationStrategy() {}
191
192 static SphereIncreasingInterpolationStrategy *m_instance;
193 };
194
195 class SphereDecreasingInterpolationStrategy : public InterpolationStrategy {
196 public:
197 static SphereDecreasingInterpolationStrategy *instance();
198
199 virtual double valueAt(double t, double middle) const;
200 virtual int type() const { return INTERP_SPHERE_DECREASING; }
201 private:
202 SphereDecreasingInterpolationStrategy() {}
203
204 static SphereDecreasingInterpolationStrategy *m_instance;
205 };
206
207 class SineInterpolationStrategy : public InterpolationStrategy {
208 public:
209 static SineInterpolationStrategy *instance();
210
211 virtual double valueAt(double t, double middle) const;
212 virtual int type() const { return INTERP_SINE; }
213 private:
214 SineInterpolationStrategy() {}
215
216 static SineInterpolationStrategy *m_instance;
217 };
218 private:
219 InterpolationStrategy *m_interpolator;
220 ColorInterpolationStrategy *m_colorInterpolator;
221
222 double m_startOffset;
223 double m_middleOffset;
224 double m_endOffset;
225 double m_length;
226 double m_middleT;
227
228 Color m_startColor;
229 Color m_endColor;
230};
231
232class KisGradient : public KisResource {
233 typedef KisResource super;
234 TQ_OBJECT
235
236
237public:
238 KisGradient(const TQString& file);
239 virtual ~KisGradient();
240
241 virtual bool load();
242 virtual bool save();
243 virtual TQImage img();
244 virtual TQImage generatePreview(int width, int height) const;
245
246 void colorAt(double t, TQColor *color, TQ_UINT8 *opacity) const;
247
248 KisGradientSegment *segmentAt(double t) const;
249
250protected:
251 inline void pushSegment( KisGradientSegment* segment ) { m_segments.push_back(segment); };
252 void setImage(const TQImage& img);
253
254 TQValueVector<KisGradientSegment *> m_segments;
255
256private:
257 bool init();
258
259private:
260 TQByteArray m_data;
261 TQImage m_img;
262};
263
264#endif // KIS_GRADIENT_H
265
The KisResource class provides a representation of Chalk image resources.
virtual TQImage img()=0
Returns a TQImage representing this resource.
virtual bool save()=0
Save this resource asynchronously.
virtual bool load()=0
Load this resource.