• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "config.h"
28 #include "Gradient.h"
29 
30 #include "Color.h"
31 
32 namespace WebCore {
33 
Gradient(const FloatPoint & p0,const FloatPoint & p1)34 Gradient::Gradient(const FloatPoint& p0, const FloatPoint& p1)
35     : m_radial(false)
36     , m_p0(p0)
37     , m_p1(p1)
38     , m_r0(0)
39     , m_r1(0)
40     , m_stopsSorted(false)
41     , m_lastStop(0)
42     , m_spreadMethod(SpreadMethodPad)
43 {
44     platformInit();
45 }
46 
Gradient(const FloatPoint & p0,float r0,const FloatPoint & p1,float r1)47 Gradient::Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1)
48     : m_radial(true)
49     , m_p0(p0)
50     , m_p1(p1)
51     , m_r0(r0)
52     , m_r1(r1)
53     , m_stopsSorted(false)
54     , m_lastStop(0)
55     , m_spreadMethod(SpreadMethodPad)
56 {
57     platformInit();
58 }
59 
~Gradient()60 Gradient::~Gradient()
61 {
62     platformDestroy();
63 }
64 
addColorStop(float value,const Color & color)65 void Gradient::addColorStop(float value, const Color& color)
66 {
67     float r;
68     float g;
69     float b;
70     float a;
71     color.getRGBA(r, g, b, a);
72     m_stops.append(ColorStop(value, r, g, b, a));
73 
74     m_stopsSorted = false;
75 
76     platformDestroy();
77 }
78 
compareStops(const Gradient::ColorStop & a,const Gradient::ColorStop & b)79 static inline bool compareStops(const Gradient::ColorStop& a, const Gradient::ColorStop& b)
80 {
81     return a.stop < b.stop;
82 }
83 
getColor(float value,float * r,float * g,float * b,float * a) const84 void Gradient::getColor(float value, float* r, float* g, float* b, float* a) const
85 {
86     ASSERT(value >= 0);
87     ASSERT(value <= 1);
88 
89     if (m_stops.isEmpty()) {
90         *r = 0;
91         *g = 0;
92         *b = 0;
93         *a = 0;
94         return;
95     }
96     if (!m_stopsSorted) {
97         if (m_stops.size())
98             std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
99         m_stopsSorted = true;
100     }
101     if (value <= 0 || value <= m_stops.first().stop) {
102         *r = m_stops.first().red;
103         *g = m_stops.first().green;
104         *b = m_stops.first().blue;
105         *a = m_stops.first().alpha;
106         return;
107     }
108     if (value >= 1 || value >= m_stops.last().stop) {
109         *r = m_stops.last().red;
110         *g = m_stops.last().green;
111         *b = m_stops.last().blue;
112         *a = m_stops.last().alpha;
113         return;
114     }
115 
116     // Find stop before and stop after and interpolate.
117     int stop = findStop(value);
118     const ColorStop& lastStop = m_stops[stop];
119     const ColorStop& nextStop = m_stops[stop + 1];
120     float stopFraction = (value - lastStop.stop) / (nextStop.stop - lastStop.stop);
121     *r = lastStop.red + (nextStop.red - lastStop.red) * stopFraction;
122     *g = lastStop.green + (nextStop.green - lastStop.green) * stopFraction;
123     *b = lastStop.blue + (nextStop.blue - lastStop.blue) * stopFraction;
124     *a = lastStop.alpha + (nextStop.alpha - lastStop.alpha) * stopFraction;
125 }
126 
findStop(float value) const127 int Gradient::findStop(float value) const
128 {
129     ASSERT(value >= 0);
130     ASSERT(value <= 1);
131     ASSERT(m_stopsSorted);
132 
133     int numStops = m_stops.size();
134     ASSERT(numStops >= 2);
135     ASSERT(m_lastStop < numStops - 1);
136 
137     int i = m_lastStop;
138     if (value < m_stops[i].stop)
139         i = 1;
140     else
141         i = m_lastStop + 1;
142 
143     for (; i < numStops - 1; ++i)
144         if (value < m_stops[i].stop)
145             break;
146 
147     m_lastStop = i - 1;
148     return m_lastStop;
149 }
150 
setSpreadMethod(GradientSpreadMethod spreadMethod)151 void Gradient::setSpreadMethod(GradientSpreadMethod spreadMethod)
152 {
153     // FIXME: Should it become necessary, allow calls to this method after m_gradient has been set.
154     ASSERT(m_gradient == 0);
155     m_spreadMethod = spreadMethod;
156 }
157 
setGradientSpaceTransform(const TransformationMatrix & gradientSpaceTransformation)158 void Gradient::setGradientSpaceTransform(const TransformationMatrix& gradientSpaceTransformation)
159 {
160     m_gradientSpaceTransformation = gradientSpaceTransformation;
161     setPlatformGradientSpaceTransform(gradientSpaceTransformation);
162 }
163 
164 #if !PLATFORM(SKIA)
setPlatformGradientSpaceTransform(const TransformationMatrix &)165 void Gradient::setPlatformGradientSpaceTransform(const TransformationMatrix&)
166 {
167 }
168 #endif
169 
170 
171 } //namespace
172