1/*
2 * Copyright (C) 2006, 2007, 2008, 2010 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#include "FloatRect.h"
32#include <wtf/UnusedParam.h>
33
34namespace WebCore {
35
36Gradient::Gradient(const FloatPoint& p0, const FloatPoint& p1)
37    : m_radial(false)
38    , m_p0(p0)
39    , m_p1(p1)
40    , m_r0(0)
41    , m_r1(0)
42    , m_stopsSorted(false)
43    , m_lastStop(0)
44    , m_spreadMethod(SpreadMethodPad)
45{
46    platformInit();
47}
48
49Gradient::Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1)
50    : m_radial(true)
51    , m_p0(p0)
52    , m_p1(p1)
53    , m_r0(r0)
54    , m_r1(r1)
55    , m_stopsSorted(false)
56    , m_lastStop(0)
57    , m_spreadMethod(SpreadMethodPad)
58{
59    platformInit();
60}
61
62Gradient::~Gradient()
63{
64    platformDestroy();
65}
66
67void Gradient::adjustParametersForTiledDrawing(IntSize& size, FloatRect& srcRect)
68{
69    if (m_radial)
70        return;
71
72    if (srcRect.isEmpty())
73        return;
74
75    if (m_p0.x() == m_p1.x()) {
76        size.setWidth(1);
77        srcRect.setWidth(1);
78        srcRect.setX(0);
79        return;
80    }
81    if (m_p0.y() != m_p1.y())
82        return;
83
84    size.setHeight(1);
85    srcRect.setHeight(1);
86    srcRect.setY(0);
87}
88
89void Gradient::addColorStop(float value, const Color& color)
90{
91    float r;
92    float g;
93    float b;
94    float a;
95    color.getRGBA(r, g, b, a);
96    m_stops.append(ColorStop(value, r, g, b, a));
97
98    m_stopsSorted = false;
99
100    platformDestroy();
101}
102
103static inline bool compareStops(const Gradient::ColorStop& a, const Gradient::ColorStop& b)
104{
105    return a.stop < b.stop;
106}
107
108void Gradient::sortStopsIfNecessary()
109{
110    if (m_stopsSorted)
111        return;
112
113    if (m_stops.size())
114        std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
115    m_stopsSorted = true;
116}
117
118void Gradient::getColor(float value, float* r, float* g, float* b, float* a) const
119{
120    ASSERT(value >= 0);
121    ASSERT(value <= 1);
122
123    if (m_stops.isEmpty()) {
124        *r = 0;
125        *g = 0;
126        *b = 0;
127        *a = 0;
128        return;
129    }
130    if (!m_stopsSorted) {
131        if (m_stops.size())
132            std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
133        m_stopsSorted = true;
134    }
135    if (value <= 0 || value <= m_stops.first().stop) {
136        *r = m_stops.first().red;
137        *g = m_stops.first().green;
138        *b = m_stops.first().blue;
139        *a = m_stops.first().alpha;
140        return;
141    }
142    if (value >= 1 || value >= m_stops.last().stop) {
143        *r = m_stops.last().red;
144        *g = m_stops.last().green;
145        *b = m_stops.last().blue;
146        *a = m_stops.last().alpha;
147        return;
148    }
149
150    // Find stop before and stop after and interpolate.
151    int stop = findStop(value);
152    const ColorStop& lastStop = m_stops[stop];
153    const ColorStop& nextStop = m_stops[stop + 1];
154    float stopFraction = (value - lastStop.stop) / (nextStop.stop - lastStop.stop);
155    *r = lastStop.red + (nextStop.red - lastStop.red) * stopFraction;
156    *g = lastStop.green + (nextStop.green - lastStop.green) * stopFraction;
157    *b = lastStop.blue + (nextStop.blue - lastStop.blue) * stopFraction;
158    *a = lastStop.alpha + (nextStop.alpha - lastStop.alpha) * stopFraction;
159}
160
161int Gradient::findStop(float value) const
162{
163    ASSERT(value >= 0);
164    ASSERT(value <= 1);
165    ASSERT(m_stopsSorted);
166
167    int numStops = m_stops.size();
168    ASSERT(numStops >= 2);
169    ASSERT(m_lastStop < numStops - 1);
170
171    int i = m_lastStop;
172    if (value < m_stops[i].stop)
173        i = 1;
174    else
175        i = m_lastStop + 1;
176
177    for (; i < numStops - 1; ++i)
178        if (value < m_stops[i].stop)
179            break;
180
181    m_lastStop = i - 1;
182    return m_lastStop;
183}
184
185void Gradient::setSpreadMethod(GradientSpreadMethod spreadMethod)
186{
187    // FIXME: Should it become necessary, allow calls to this method after m_gradient has been set.
188    ASSERT(m_gradient == 0);
189    m_spreadMethod = spreadMethod;
190}
191
192void Gradient::setGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation)
193{
194    m_gradientSpaceTransformation = gradientSpaceTransformation;
195    setPlatformGradientSpaceTransform(gradientSpaceTransformation);
196}
197
198#if !(PLATFORM(SKIA) && !PLATFORM(ANDROID))
199void Gradient::setPlatformGradientSpaceTransform(const AffineTransform&)
200{
201}
202#endif
203
204
205} //namespace
206