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.category;
034
035import java.awt.Color;
036import java.io.Serializable;
037import java.util.Arrays;
038
039import org.jfree.chart3d.Colors;
040import org.jfree.chart3d.internal.Args;
041
042/**
043 * A standard implementation of the {@link CategoryColorSource} 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 StandardCategoryColorSource implements CategoryColorSource, 
051        Serializable {
052
053    /** The sequence of color objects to use for each series. */
054    private Color[] colors;
055    
056    /**
057     * Creates a new instance with default colors.
058     */
059    public StandardCategoryColorSource() {
060        this(Colors.getDefaultColors());
061    }
062    
063    /**
064     * Creates a new instance with the supplied sequence of colors.  The
065     * supplied array must have at least one entry, and all entries must be
066     * non-{@code null}.
067     * 
068     * @param colors  the colors ({@code null} not permitted). 
069     */
070    public StandardCategoryColorSource(Color... colors) {
071        Args.nullNotPermitted(colors, "colors");
072        if (colors.length == 0) {
073            throw new IllegalArgumentException(
074                    "Zero length array not permitted.");
075        }
076        for (Color c: colors) {
077            if (c == null) { 
078                throw new IllegalArgumentException(
079                        "Null array entries not permitted.");
080            }
081        }
082        this.colors = colors.clone();
083    }
084    
085    /**
086     * Returns the color to use for the specified item.
087     * 
088     * @param series  the series index.
089     * @param row  the row index.
090     * @param column  the column index.
091     * 
092     * @return The color (never {@code null}). 
093     */
094    @Override
095    public Color getColor(int series, int row, int column) {
096        return this.colors[series % this.colors.length];
097    }
098
099    /**
100     * Returns the color to use in the legend for the specified series.
101     * 
102     * @param series  the series index.
103     * 
104     * @return The color (never {@code null}).
105     */
106    @Override
107    public Color getLegendColor(int series) {
108        return this.colors[series % this.colors.length];
109    }
110    
111    /**
112     * Restyles the source using the specified colors.  Refer to the 
113     * implementing class to confirm the precise behaviour (typically all 
114     * existing color settings are cleared and the specified colors are 
115     * installed as the new defaults).
116     * 
117     * @param colors  the colors.
118     * 
119     * @since 1.2
120     */
121    @Override
122    public void style(Color... colors) {
123        Args.nullNotPermitted(colors, "colors");
124        if (colors.length == 0) {
125            throw new IllegalArgumentException(
126                    "Zero length array not permitted.");
127        }
128        for (Color c: colors) {
129            if (c == null) { 
130                throw new IllegalArgumentException(
131                        "Null array entries not permitted.");
132            }
133        }
134        this.colors = colors.clone();
135    }
136    
137    /**
138     * Tests this color source for equality with an arbitrary object.
139     * 
140     * @param obj  the object ({@code null} permitted).
141     * 
142     * @return A boolean. 
143     */
144    @Override
145    public boolean equals(Object obj) {
146        if (obj == this) {
147            return true;
148        }
149        if (!(obj instanceof StandardCategoryColorSource)) {
150            return false;
151        }
152        StandardCategoryColorSource that 
153                = (StandardCategoryColorSource) obj;
154        if (!Arrays.equals(this.colors, that.colors)) {
155            return false;
156        }
157        return true;
158    }
159
160    @Override
161    public int hashCode() {
162        int hash = 7;
163        hash = 97 * hash + Arrays.deepHashCode(this.colors);
164        return hash;
165    }
166
167}