ColorGenerator.java
001 /*
002  *  ColorGenerator.java
003  *
004  *  Copyright (c) 1995-2012, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Valentin Tablan, 11/07/2000
013  *
014  *  $Id: ColorGenerator.java 17873 2014-04-18 11:14:43Z markagreenwood $
015  */
016 package gate.swing;
017 
018 import java.awt.Color;
019 import java.util.LinkedList;
020 
021 /**
022  * This class is used to generate random colours that are evenly distributed in
023  * the colours space.
024  *
025  */
026 public class ColorGenerator {
027 
028   /**
029    * Creates a new ColorGenerator
030    *
031    */
032   public ColorGenerator() {
033     for(int i = 0; i < 8; i++)availableSpacesList[inew LinkedList<ColorSpace>();
034     ColorSpace usedCS = new ColorSpace(0,0,0,255);
035     availableSpacesList[0].addLast(new ColorSpace(usedCS.baseR +
036                                                usedCS.radius/2,
037                                                usedCS.baseG,
038                                                usedCS.baseB,
039                                                usedCS.radius/2));
040     availableSpacesList[1].addLast(new ColorSpace(usedCS.baseR,
041                                                usedCS.baseG + usedCS.radius/2,
042                                                usedCS.baseB,
043                                                usedCS.radius/2));
044     availableSpacesList[2].addLast(new ColorSpace(usedCS.baseR +
045                                                usedCS.radius/2,
046                                                usedCS.baseG + usedCS.radius/2,
047                                                usedCS.baseB,
048                                                usedCS.radius/2));
049 
050     availableSpacesList[3].addLast(new ColorSpace(usedCS.baseR,
051                                                usedCS.baseG,
052                                                usedCS.baseB + usedCS.radius/2,
053                                                usedCS.radius/2));
054     availableSpacesList[4].addLast(new ColorSpace(usedCS.baseR +
055                                                usedCS.radius/2,
056                                                usedCS.baseG,
057                                                usedCS.baseB + usedCS.radius/2,
058                                                usedCS.radius/2));
059     availableSpacesList[5].addLast(new ColorSpace(usedCS.baseR,
060                                                usedCS.baseG + usedCS.radius/2,
061                                                usedCS.baseB + usedCS.radius/2,
062                                                usedCS.radius/2));
063   /*
064     availableSpacesList[6].addLast(new ColorSpace(usedCS.baseR +
065                                                usedCS.radius/2,
066                                                usedCS.baseG + usedCS.radius/2,
067                                                usedCS.baseB + usedCS.radius/2,
068                                                usedCS.radius/2));
069 
070   */
071   //    Color foo = getNextColor();
072   }
073 
074   /**
075    * Gets the next random colour
076    *
077    */
078   public Color getNextColor(){
079     ColorSpace usedCS;
080     listToRead = listToRead % 8;
081 
082     if(availableSpacesList[listToRead].isEmpty()){
083       usedCS = usedSpacesList.removeFirst();
084       availableSpacesList[listToRead].addLast(new ColorSpace(usedCS.baseR,
085                                                  usedCS.baseG,
086                                                  usedCS.baseB,
087                                                  usedCS.radius/2));
088       availableSpacesList[listToRead].addLast(new ColorSpace(
089                                                  usedCS.baseR + usedCS.radius/2,
090                                                  usedCS.baseG,
091                                                  usedCS.baseB,
092                                                  usedCS.radius/2));
093       availableSpacesList[listToRead].addLast(new ColorSpace(usedCS.baseR,
094                                                  usedCS.baseG + usedCS.radius/2,
095                                                  usedCS.baseB,
096                                                  usedCS.radius/2));
097       availableSpacesList[listToRead].addLast(new ColorSpace(
098                                                  usedCS.baseR + usedCS.radius/2,
099                                                  usedCS.baseG + usedCS.radius/2,
100                                                  usedCS.baseB,
101                                                  usedCS.radius/2));
102 
103       availableSpacesList[listToRead].addLast(new ColorSpace(usedCS.baseR,
104                                                  usedCS.baseG,
105                                                  usedCS.baseB + usedCS.radius/2,
106                                                  usedCS.radius/2));
107       availableSpacesList[listToRead].addLast(new ColorSpace(
108                                                  usedCS.baseR + usedCS.radius/2,
109                                                  usedCS.baseG,
110                                                  usedCS.baseB + usedCS.radius/2,
111                                                  usedCS.radius/2));
112       availableSpacesList[listToRead].addLast(new ColorSpace(usedCS.baseR,
113                                                  usedCS.baseG + usedCS.radius/2,
114                                                  usedCS.baseB + usedCS.radius/2,
115                                                  usedCS.radius/2));
116       availableSpacesList[listToRead].addLast(new ColorSpace(
117                                                  usedCS.baseR + usedCS.radius/2,
118                                                  usedCS.baseG + usedCS.radius/2,
119                                                  usedCS.baseB + usedCS.radius/2,
120                                                  usedCS.radius/2));
121 
122     }
123     usedCS = availableSpacesList[listToRead].removeFirst();
124     Color res = new Color(usedCS.baseR + usedCS.radius/2,
125                           usedCS.baseG + usedCS.radius/2,
126                           usedCS.baseB + usedCS.radius/2);
127     usedSpacesList.addLast(usedCS);
128     listToRead++;
129     res = res.brighter();
130     return res;
131   }
132 
133   /**
134    * Represents a colur space. A colour space is a cube in a tridimiensional
135    * space (where the axes represent red/green/blue values) defined by a point
136    * and a radius(the length of the edge).
137    */
138   class ColorSpace{
139     /**
140      * Creates a new ColorSpace
141      *
142      @param r
143      @param g
144      @param b
145      @param radius
146      */
147     ColorSpace(int r, int g, int b, int radius){
148       baseR = r;
149       baseG = g;
150       baseB = b;
151       this.radius = radius;
152     }
153 
154     /**      *
155      */
156     int baseR, baseG, baseB;
157     /**      */
158     int radius;
159   }
160 
161   /**    */
162   @SuppressWarnings({"unchecked","rawtypes"})
163   LinkedList<ColorSpace>[] availableSpacesList = new LinkedList[8];
164 
165   /**    */
166   LinkedList<ColorSpace> usedSpacesList = new LinkedList<ColorSpace>();
167 
168   /**    */
169   int listToRead = 0;
170 
171 // ColorGenerator