001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
006 * 
007 * https://github.com/jfree/orson-charts
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * Orson Charts home page:
028 * 
029 * http://www.object-refinery.com/orsoncharts/index.html
030 * 
031 */
032
033package org.jfree.chart3d.renderer.xyz;
034
035import java.awt.Color;
036import java.io.Serializable;
037
038import org.jfree.chart3d.Colors;
039import org.jfree.chart3d.internal.Args;
040import org.jfree.chart3d.internal.ObjectUtils;
041
042/**
043 * A standard implementation of the {@link XYZColorSource} interface.
044 * <br><br>
045 * NOTE: This class is serializable, but the serialization format is subject 
046 * to change in future releases and should not be relied upon for persisting 
047 * instances of this class.
048 */
049@SuppressWarnings("serial")
050public class StandardXYZColorSource implements XYZColorSource, Serializable {
051
052    /** The sequence of color objects to use for each series. */
053    private Color[] colors;
054    
055    /**
056     * Creates a new instance with default colors.
057     */
058    public StandardXYZColorSource() {
059        this(Colors.getDefaultColors());    
060    }
061    
062    /**
063     * Creates a new instance with the supplied sequence of colors.  The
064     * supplied array must have at least one entry, and all entries must be
065     * non-{@code null}.
066     * 
067     * @param colors  the colors ({@code null} not permitted). 
068     */
069    public StandardXYZColorSource(Color... colors) {
070        Args.nullNotPermitted(colors, "colors");
071        if (colors.length == 0) {
072            throw new IllegalArgumentException(
073                    "Zero length array not permitted.");
074        }
075        for (Color c: colors) {
076            if (c == null) { 
077                throw new IllegalArgumentException(
078                        "Null array entries not permitted.");
079            }
080        }
081        this.colors = colors;
082    }
083    
084    /**
085     * Returns the color to use for the specified item.
086     * 
087     * @param series  the series index.
088     * @param item  the item index.
089     * 
090     * @return The color (never {@code null}). 
091     */
092    @Override
093    public Color getColor(int series, int item) {
094        return this.colors[series % this.colors.length];
095    }
096
097    /**
098     * Returns the color to use in the legend for the specified series.
099     * 
100     * @param series  the series index.
101     * 
102     * @return The color (never {@code null}).
103     */
104    @Override
105    public Color getLegendColor(int series) {
106        return this.colors[series % this.colors.length];
107    }
108    
109    /**
110     * Restyles the source using the specified colors.  Refer to the 
111     * implementing class to confirm the precise behaviour (typically all 
112     * existing color settings are cleared and the specified colors are 
113     * installed as the new defaults).
114     * 
115     * @param colors  the colors.
116     * 
117     * @since 1.2
118     */
119    @Override
120    public void style(Color... colors) {
121        Args.nullNotPermitted(colors, "colors");
122        if (colors.length == 0) {
123            throw new IllegalArgumentException(
124                    "Zero length array not permitted.");
125        }
126        for (Color c: colors) {
127            if (c == null) { 
128                throw new IllegalArgumentException(
129                        "Null array entries not permitted.");
130            }
131        }
132        this.colors = colors.clone();
133    }
134
135    /**
136     * Tests this paint source for equality with an arbitrary object.
137     * 
138     * @param obj  the object ({@code null} permitted).
139     * 
140     * @return A boolean. 
141     */
142    @Override
143    public boolean equals(Object obj) {
144        if (obj == this) {
145            return true;
146        }
147        if (!(obj instanceof StandardXYZColorSource)) {
148            return false;
149        }
150        StandardXYZColorSource that = (StandardXYZColorSource) obj;
151        if (this.colors.length != that.colors.length) {
152            return false;
153        }
154        for (int i = 0; i < this.colors.length; i++) {
155            if (!ObjectUtils.equalsPaint(this.colors[i], that.colors[i])) {
156                return false;
157            }
158        }
159        return true;
160    }
161    
162}