make sure to attach to the document also when a resize event is received prior of...
[kdegraphics.git] / kolourpaint / imagelib / effects / kpEffectHSV.cpp
blob839dce2f769bdb4eafaecf837486703c28a9323e
2 /*
3 Copyright (c) 2007 Mike Gashler <gashlerm@yahoo.com>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // TODO: Clarence's code review
30 #include <kpEffectHSV.h>
32 #include <math.h>
34 #include <qbitmap.h>
35 #include <qimage.h>
37 #include <kdebug.h>
39 #include <kpPixmapFX.h>
42 static void ColorToHSV(unsigned int c, float* pHue, float* pSaturation, float* pValue)
44 int r = qRed(c);
45 int g = qGreen(c);
46 int b = qBlue(c);
47 int min;
48 if(b >= g && b >= r)
50 // Blue
51 min = qMin(r, g);
52 if(b != min)
54 *pHue = (float)(r - g) / ((b - min) * 6) + (float)2 / 3;
55 *pSaturation = (float)1 - (float)min / (float)b;
57 else
59 *pHue = 0;
60 *pSaturation = 0;
62 *pValue = (float)b / 255;
64 else if(g >= r)
66 // Green
67 min = qMin(b, r);
68 if(g != min)
70 *pHue = (float)(b - r) / ((g - min) * 6) + (float)1 / 3;
71 *pSaturation = (float)1 - (float)min / (float)g;
73 else
75 *pHue = 0;
76 *pSaturation = 0;
78 *pValue = (float)g / 255;
80 else
82 // Red
83 min = qMin(g, b);
84 if(r != min)
86 *pHue = (float)(g - b) / ((r - min) * 6);
87 if(*pHue < 0)
88 (*pHue) += (float)1;
89 *pSaturation = (float)1 - (float)min / (float)r;
91 else
93 *pHue = 0;
94 *pSaturation = 0;
96 *pValue = (float)r / 255;
100 static unsigned int HSVToColor(int alpha, float hue, float saturation, float value)
102 //Q_ASSERT (hue >= 0 && hue <= 1 && saturation >= 0 && saturation <= 1 && value >= 0 && value <= 1);
104 hue *= (float)5.999999;
105 int h = (int)hue;
106 float f = hue - h;
107 float p = value * ((float)1 - saturation);
108 float q = value * ((float)1 - ((h & 1) == 0 ? (float)1 - f : f) * saturation);
109 switch(h)
111 case 0: return qRgba((int)(value * 255.999999), (int)(q * 255.999999), (int)(p * 255.999999), alpha);
112 case 1: return qRgba((int)(q * 255.999999), (int)(value * 255.999999), (int)(p * 255.999999), alpha);
113 case 2: return qRgba((int)(p * 255.999999), (int)(value * 255.999999), (int)(q * 255.999999), alpha);
114 case 3: return qRgba((int)(p * 255.999999), (int)(q * 255.999999), (int)(value * 255.999999), alpha);
115 case 4: return qRgba((int)(q * 255.999999), (int)(p * 255.999999), (int)(value * 255.999999), alpha);
116 case 5: return qRgba((int)(value * 255.999999), (int)(p * 255.999999), (int)(q * 255.999999), alpha);
118 return qRgba(0, 0, 0, alpha);
121 static QRgb AdjustHSVInternal (QRgb pix, double hueDiv360, double saturation, double value)
123 float h, s, v;
124 ::ColorToHSV(pix, &h, &s, &v);
126 const int alpha = qAlpha(pix);
128 h += (float)hueDiv360;
129 h -= floor(h);
131 s = qMax((float)0, qMin((float)1, s + (float)saturation));
133 v = qMax((float)0, qMin((float)1, v + (float)value));
135 return ::HSVToColor(alpha, h, s, v);
138 static void AdjustHSV (QImage* pImage, double hue, double saturation, double value)
140 hue /= 360;
142 if (pImage->depth () > 8)
144 for (int y = 0; y < pImage->height (); y++)
146 for (int x = 0; x < pImage->width (); x++)
148 QRgb pix = pImage->pixel (x, y);
149 pix = ::AdjustHSVInternal (pix, hue, saturation, value);
150 pImage->setPixel (x, y, pix);
154 else
156 for (int i = 0; i < pImage->numColors (); i++)
158 QRgb pix = pImage->color (i);
159 pix = ::AdjustHSVInternal (pix, hue, saturation, value);
160 pImage->setColor (i, pix);
165 // public static
166 kpImage kpEffectHSV::applyEffect (const kpImage &image,
167 double hue, double saturation, double value)
169 QImage qimage = kpPixmapFX::convertToQImage (image);
171 ::AdjustHSV (&qimage, hue, saturation, value);
173 QPixmap retPixmap = kpPixmapFX::convertToPixmap (qimage);
175 return retPixmap;