krita

kis_gradient.h

00001 /*
00002  *  kis_gradient.h - part of Krayon
00003  *
00004  *  Copyright (c) 2000 Matthias Elter  <elter@kde.org>
00005  *                2004 Boudewijn Rempt <boud@valdyas.org>
00006  *                2004 Adrian Page <adrian@pagenet.plus.com>
00007  *                2004 Sven Langkamp <longamp@reallygood.de>
00008  *
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00022  */
00023 
00024 #ifndef KIS_GRADIENT_H
00025 #define KIS_GRADIENT_H
00026 
00027 #include <qvaluevector.h>
00028 #include <qcolor.h>
00029 
00030 #include <kio/job.h>
00031 
00032 #include "kis_resource.h"
00033 #include "kis_global.h"
00034 
00035 class QImage;
00036 
00037 enum {
00038     INTERP_LINEAR = 0,
00039     INTERP_CURVED,
00040     INTERP_SINE,
00041     INTERP_SPHERE_INCREASING,
00042     INTERP_SPHERE_DECREASING
00043 };
00044 
00045 enum {
00046     COLOR_INTERP_RGB,
00047     COLOR_INTERP_HSV_CCW,
00048     COLOR_INTERP_HSV_CW
00049 };
00050 
00051 // TODO: Replace QColor with KisColor
00052 class Color {
00053     public:
00054         Color() { m_alpha = 0; }
00055         Color(const QColor& color, double alpha) { m_color = color; m_alpha = alpha; }
00056 
00057         const QColor& color() const { return m_color; }
00058         double alpha() const { return m_alpha; }
00059 
00060     private:
00061         QColor m_color;
00062         double m_alpha;
00063 };
00064 
00065 class KisGradientSegment {
00066     public:
00067         KisGradientSegment(int interpolationType, int colorInterpolationType, double startOffset, double middleOffset, double endOffset, const Color& startColor, const Color& endColor);
00068 
00069         // startOffset <= t <= endOffset
00070         Color colorAt(double t) const;
00071 
00072         const Color& startColor() const;
00073         const Color& endColor() const;
00074 
00075         void setStartColor(const Color& color) { m_startColor = color; }
00076         void setEndColor(const Color& color) { m_endColor = color; }
00077 
00078         double startOffset() const;
00079         double middleOffset() const;
00080         double endOffset() const;
00081 
00082         void setStartOffset(double t);
00083         void setMiddleOffset(double t);
00084         void setEndOffset(double t);
00085 
00086         double length() { return m_length; }
00087 
00088         int interpolation() const;
00089         int colorInterpolation() const;
00090 
00091         void setInterpolation(int interpolationType);
00092         void setColorInterpolation(int colorInterpolationType);
00093 
00094         bool isValid() const;
00095     protected:
00096 
00097         class ColorInterpolationStrategy {
00098         public:
00099             ColorInterpolationStrategy() {}
00100             virtual ~ColorInterpolationStrategy() {}
00101 
00102             virtual Color colorAt(double t, Color start, Color end) const = 0;
00103             virtual int type() const = 0;
00104         };
00105 
00106         class RGBColorInterpolationStrategy : public ColorInterpolationStrategy {
00107         public:
00108             static RGBColorInterpolationStrategy *instance();
00109 
00110             virtual Color colorAt(double t, Color start, Color end) const;
00111             virtual int type() const { return COLOR_INTERP_RGB; }
00112 
00113         private:
00114             RGBColorInterpolationStrategy() {}
00115 
00116             static RGBColorInterpolationStrategy *m_instance;
00117         };
00118 
00119         class HSVCWColorInterpolationStrategy : public ColorInterpolationStrategy {
00120         public:
00121             static HSVCWColorInterpolationStrategy *instance();
00122 
00123             virtual Color colorAt(double t, Color start, Color end) const;
00124             virtual int type() const { return COLOR_INTERP_HSV_CW; }
00125         private:
00126             HSVCWColorInterpolationStrategy() {}
00127 
00128             static HSVCWColorInterpolationStrategy *m_instance;
00129         };
00130 
00131         class HSVCCWColorInterpolationStrategy : public ColorInterpolationStrategy {
00132         public:
00133             static HSVCCWColorInterpolationStrategy *instance();
00134 
00135             virtual Color colorAt(double t, Color start, Color end) const;
00136             virtual int type() const { return COLOR_INTERP_HSV_CCW; }
00137         private:
00138             HSVCCWColorInterpolationStrategy() {}
00139 
00140             static HSVCCWColorInterpolationStrategy *m_instance;
00141         };
00142 
00143         class InterpolationStrategy {
00144         public:
00145             InterpolationStrategy() {}
00146             virtual ~InterpolationStrategy() {}
00147 
00148             virtual double valueAt(double t, double middle) const = 0;
00149             virtual int type() const = 0;
00150         };
00151 
00152         class LinearInterpolationStrategy : public InterpolationStrategy {
00153         public:
00154             static LinearInterpolationStrategy *instance();
00155 
00156             virtual double valueAt(double t, double middle) const;
00157             virtual int type() const { return INTERP_LINEAR; }
00158 
00159             // This does the actual calculation and is made
00160             // static as an optimisation for the other
00161             // strategies that need this for their own calculation.
00162             static double calcValueAt(double t, double middle);
00163 
00164         private:
00165             LinearInterpolationStrategy() {}
00166 
00167             static LinearInterpolationStrategy *m_instance;
00168         };
00169 
00170         class CurvedInterpolationStrategy : public InterpolationStrategy {
00171         public:
00172             static CurvedInterpolationStrategy *instance();
00173 
00174             virtual double valueAt(double t, double middle) const;
00175             virtual int type() const { return INTERP_CURVED; }
00176         private:
00177             CurvedInterpolationStrategy();
00178 
00179             static CurvedInterpolationStrategy *m_instance;
00180             double m_logHalf;
00181         };
00182 
00183         class SphereIncreasingInterpolationStrategy : public InterpolationStrategy {
00184         public:
00185             static SphereIncreasingInterpolationStrategy *instance();
00186 
00187             virtual double valueAt(double t, double middle) const;
00188             virtual int type() const { return INTERP_SPHERE_INCREASING; }
00189         private:
00190             SphereIncreasingInterpolationStrategy() {}
00191 
00192             static SphereIncreasingInterpolationStrategy *m_instance;
00193         };
00194 
00195         class SphereDecreasingInterpolationStrategy : public InterpolationStrategy {
00196         public:
00197             static SphereDecreasingInterpolationStrategy *instance();
00198 
00199             virtual double valueAt(double t, double middle) const;
00200             virtual int type() const { return INTERP_SPHERE_DECREASING; }
00201         private:
00202             SphereDecreasingInterpolationStrategy() {}
00203 
00204             static SphereDecreasingInterpolationStrategy *m_instance;
00205         };
00206 
00207         class SineInterpolationStrategy : public InterpolationStrategy {
00208         public:
00209             static SineInterpolationStrategy *instance();
00210 
00211             virtual double valueAt(double t, double middle) const;
00212             virtual int type() const { return INTERP_SINE; }
00213         private:
00214             SineInterpolationStrategy() {}
00215 
00216             static SineInterpolationStrategy *m_instance;
00217         };
00218     private:
00219         InterpolationStrategy *m_interpolator;
00220         ColorInterpolationStrategy *m_colorInterpolator;
00221 
00222         double m_startOffset;
00223         double m_middleOffset;
00224         double m_endOffset;
00225         double m_length;
00226         double m_middleT;
00227 
00228         Color m_startColor;
00229         Color m_endColor;
00230 };
00231 
00232 class KisGradient : public KisResource {
00233     typedef KisResource super;
00234     Q_OBJECT
00235 
00236 public:
00237     KisGradient(const QString& file);
00238     virtual ~KisGradient();
00239 
00240     virtual bool load();
00241     virtual bool save();
00242     virtual QImage img();
00243     virtual QImage generatePreview(int width, int height) const;
00244 
00245     void colorAt(double t, QColor *color, Q_UINT8 *opacity) const;
00246     
00247     KisGradientSegment *segmentAt(double t) const;
00248 
00249 protected:
00250     inline void pushSegment( KisGradientSegment* segment ) { m_segments.push_back(segment); };
00251     void setImage(const QImage& img);
00252 
00253     QValueVector<KisGradientSegment *> m_segments;
00254 
00255 private:
00256     bool init();
00257 
00258 private:
00259     QByteArray m_data;
00260     QImage m_img;
00261 };
00262 
00263 #endif // KIS_GRADIENT_H
00264 
KDE Home | KDE Accessibility Home | Description of Access Keys