1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.gui.splash;
22
23import java.awt.*;
24
25/**
26 * This VariableColor varies linearly with respect to its Timing.
27 *
28 * @author Eric Lafortune
29 */
30public class LinearColor implements VariableColor
31{
32    private final Color  fromValue;
33    private final Color  toValue;
34    private final Timing timing;
35
36    private double cachedTiming = -1.0;
37    private Color  cachedColor;
38
39
40    /**
41     * Creates a new LinearColor.
42     * @param fromValue the value that corresponds to a timing of 0.
43     * @param toValue   the value that corresponds to a timing of 1.
44     * @param timing    the applied timing.
45     */
46    public LinearColor(Color fromValue, Color toValue, Timing timing)
47    {
48        this.fromValue = fromValue;
49        this.toValue   = toValue;
50        this.timing    = timing;
51    }
52
53
54    // Implementation for VariableColor.
55
56    public Color getColor(long time)
57    {
58        double t = timing.getTiming(time);
59        if (t != cachedTiming)
60        {
61            cachedTiming = t;
62            cachedColor =
63                t == 0.0 ? fromValue :
64                t == 1.0 ? toValue   :
65                           new Color((int)(fromValue.getRed()   + t * (toValue.getRed()   - fromValue.getRed())),
66                                     (int)(fromValue.getGreen() + t * (toValue.getGreen() - fromValue.getGreen())),
67                                     (int)(fromValue.getBlue()  + t * (toValue.getBlue()  - fromValue.getBlue())));
68        }
69
70        return cachedColor;
71    }
72}
73