| ColorGenerator |

    1   /*
    2    *  ColorGenerator.java
    3    *
    4    *  Copyright (c) 1998-2001, The University of Sheffield.
    5    *
    6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
    7    *  software, licenced under the GNU Library General Public License,
    8    *  Version 2, June 1991 (in the distribution as file licence.html,
    9    *  and also available at http://gate.ac.uk/gate/licence.html).
    10   *
    11   *  Valentin Tablan, 11/07/2000
    12   *
    13   *  $Id: ColorGenerator.java,v 1.3 2001/10/30 19:12:28 valyt Exp $
    14   */
    15  package gate.swing;
    16
    17  import java.awt.*;
    18  import java.util.*;
    19
    20  /**
    21   * This class is used to generate random colours that are evenly distributed in
    22   * the colours space.
    23   *
    24   */
    25  public class ColorGenerator {
    26
    27    /** Debug flag
    28     */
    29    private static final boolean DEBUG = false;
    30
    31    /**
    32     * Creates a new ColorGenerator
    33     *
    34     */
    35    public ColorGenerator() {
    36      for(int i = 0; i < 8; i++)availableSpacesList[i] = new LinkedList();
    37      ColorSpace usedCS = new ColorSpace(0,0,0,255);
    38      availableSpacesList[0].addLast(new ColorSpace(usedCS.baseR +
    39                                                 usedCS.radius/2,
    40                                                 usedCS.baseG,
    41                                                 usedCS.baseB,
    42                                                 usedCS.radius/2));
    43      availableSpacesList[1].addLast(new ColorSpace(usedCS.baseR,
    44                                                 usedCS.baseG + usedCS.radius/2,
    45                                                 usedCS.baseB,
    46                                                 usedCS.radius/2));
    47      availableSpacesList[2].addLast(new ColorSpace(usedCS.baseR +
    48                                                 usedCS.radius/2,
    49                                                 usedCS.baseG + usedCS.radius/2,
    50                                                 usedCS.baseB,
    51                                                 usedCS.radius/2));
    52
    53      availableSpacesList[3].addLast(new ColorSpace(usedCS.baseR,
    54                                                 usedCS.baseG,
    55                                                 usedCS.baseB + usedCS.radius/2,
    56                                                 usedCS.radius/2));
    57      availableSpacesList[4].addLast(new ColorSpace(usedCS.baseR +
    58                                                 usedCS.radius/2,
    59                                                 usedCS.baseG,
    60                                                 usedCS.baseB + usedCS.radius/2,
    61                                                 usedCS.radius/2));
    62      availableSpacesList[5].addLast(new ColorSpace(usedCS.baseR,
    63                                                 usedCS.baseG + usedCS.radius/2,
    64                                                 usedCS.baseB + usedCS.radius/2,
    65                                                 usedCS.radius/2));
    66    /*
    67      availableSpacesList[6].addLast(new ColorSpace(usedCS.baseR +
    68                                                 usedCS.radius/2,
    69                                                 usedCS.baseG + usedCS.radius/2,
    70                                                 usedCS.baseB + usedCS.radius/2,
    71                                                 usedCS.radius/2));
    72
    73    */
    74    //    Color foo = getNextColor();
    75    }
    76
    77    /**
    78     * Gets the next random colour
    79     *
    80     */
    81    public Color getNextColor(){
    82      ColorSpace usedCS;
    83      listToRead = listToRead % 8;
    84
    85      if(availableSpacesList[listToRead].isEmpty()){
    86        usedCS = (ColorSpace)usedSpacesList.removeFirst();
    87        availableSpacesList[listToRead].addLast(new ColorSpace(usedCS.baseR,
    88                                                   usedCS.baseG,
    89                                                   usedCS.baseB,
    90                                                   usedCS.radius/2));
    91        availableSpacesList[listToRead].addLast(new ColorSpace(
    92                                                   usedCS.baseR + usedCS.radius/2,
    93                                                   usedCS.baseG,
    94                                                   usedCS.baseB,
    95                                                   usedCS.radius/2));
    96        availableSpacesList[listToRead].addLast(new ColorSpace(usedCS.baseR,
    97                                                   usedCS.baseG + usedCS.radius/2,
    98                                                   usedCS.baseB,
    99                                                   usedCS.radius/2));
    100       availableSpacesList[listToRead].addLast(new ColorSpace(
    101                                                  usedCS.baseR + usedCS.radius/2,
    102                                                  usedCS.baseG + usedCS.radius/2,
    103                                                  usedCS.baseB,
    104                                                  usedCS.radius/2));
    105
    106       availableSpacesList[listToRead].addLast(new ColorSpace(usedCS.baseR,
    107                                                  usedCS.baseG,
    108                                                  usedCS.baseB + usedCS.radius/2,
    109                                                  usedCS.radius/2));
    110       availableSpacesList[listToRead].addLast(new ColorSpace(
    111                                                  usedCS.baseR + usedCS.radius/2,
    112                                                  usedCS.baseG,
    113                                                  usedCS.baseB + usedCS.radius/2,
    114                                                  usedCS.radius/2));
    115       availableSpacesList[listToRead].addLast(new ColorSpace(usedCS.baseR,
    116                                                  usedCS.baseG + usedCS.radius/2,
    117                                                  usedCS.baseB + usedCS.radius/2,
    118                                                  usedCS.radius/2));
    119       availableSpacesList[listToRead].addLast(new ColorSpace(
    120                                                  usedCS.baseR + usedCS.radius/2,
    121                                                  usedCS.baseG + usedCS.radius/2,
    122                                                  usedCS.baseB + usedCS.radius/2,
    123                                                  usedCS.radius/2));
    124
    125     }
    126     usedCS = (ColorSpace)availableSpacesList[listToRead].removeFirst();
    127     Color res = new Color(usedCS.baseR + usedCS.radius/2,
    128                           usedCS.baseG + usedCS.radius/2,
    129                           usedCS.baseB + usedCS.radius/2);
    130     usedSpacesList.addLast(usedCS);
    131     listToRead++;
    132     res = res.brighter();
    133     return res;
    134   }
    135
    136   /**
    137    * Represents a colur space. A colour space is a cube in a tridimiensional
    138    * space (where the axes represent red/green/blue values) defined by a point
    139    * and a radius(the length of the edge).
    140    */
    141   class ColorSpace{
    142     /**
    143      * Creates a new ColorSpace
    144      *
    145      * @param r
    146      * @param g
    147      * @param b
    148      * @param radius
    149      */
    150     ColorSpace(int r, int g, int b, int radius){
    151       baseR = r;
    152       baseG = g;
    153       baseB = b;
    154       this.radius = radius;
    155     }
    156
    157     /**      *
    158      */
    159     int baseR, baseG, baseB;
    160     /**      */
    161     int radius;
    162   }
    163
    164   /**    */
    165   LinkedList[] availableSpacesList = new LinkedList[8];
    166
    167   /**    */
    168   LinkedList usedSpacesList = new LinkedList();
    169
    170   /**    */
    171   int listToRead = 0;
    172
    173 } // ColorGenerator
    174

| ColorGenerator |