Palette.java revision 13978:1993af50385d
1/* 2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package com.sun.swingset3.demos.spinner; 24 25/** 26 * @author Mikhail Lapshin 27 */ 28import java.awt.*; 29 30public class Palette { 31 32 private final int minColor; 33 private final int colorRange; 34 private Color[] colors; 35 private int[] rgbColors; 36 private final int rSteps; 37 private final int gSteps; 38 private final int bSteps; 39 private int totalRange; 40 private int rRange; 41 private int gRange; 42 private int bRange; 43 private final double rStart; 44 private final double gStart; 45 private final double bStart; 46 47 public Palette(int totalRange, int minColor, int maxColor, double rStart, 48 double gStart, double bStart, int rSteps, int gSteps, int bSteps) { 49 this.minColor = minColor; 50 this.colorRange = maxColor - minColor; 51 this.rStart = rStart; 52 this.gStart = gStart; 53 this.bStart = bStart; 54 this.rSteps = rSteps; 55 this.gSteps = gSteps; 56 this.bSteps = bSteps; 57 setSize(totalRange); 58 } 59 60 public void setSize(int newSize) { 61 totalRange = newSize; 62 rRange = totalRange / rSteps; 63 gRange = totalRange / gSteps; 64 bRange = totalRange / bSteps; 65 fillColorTable(); 66 } 67 68 private void fillColorTable() { 69 colors = new Color[totalRange]; 70 rgbColors = new int[totalRange]; 71 for (int i = 0; i < totalRange; i++) { 72 double cosR = Math.cos(i * 2 * Math.PI / rRange + rStart); 73 double cosG = Math.cos(i * 2 * Math.PI / gRange + gStart); 74 double cosB = Math.cos(i * 2 * Math.PI / bRange + bStart); 75 Color color = new Color( 76 (int) ((cosR * colorRange) + colorRange) / 2 + minColor, 77 (int) ((cosG * colorRange) + colorRange) / 2 + minColor, 78 (int) ((cosB * colorRange) + colorRange) / 2 + minColor); 79 colors[i] = color; 80 rgbColors[i] = color.getRGB(); 81 } 82 } 83 84 public Color getColor(int index) { 85 return colors[index]; 86 } 87 88 public int getRgbColor(int index) { 89 return rgbColors[index]; 90 } 91 92 public int getSize() { 93 return totalRange; 94 } 95} 96