1   package simworldobjects;
2   /*
3    * LightSensorColorLibrary.java
4    *
5    * Created on July 22, 2003, 3:59 PM
6    */
7   
8   import java.awt.*;
9   import java.util.*;
10  import main.*;
11  
12  /**
13   *
14   * @author  Simon Zienkiewicz
15   */
16  public class LightSensorColorLibrary {
17      
18      private String[] availableColors = {"Black","Blue","Cyan","Gray","Green","Magenta","Orange","Pink","Red","White","Yellow"};
19      private Color desiredColor;
20      private static boolean[] desiredColors;
21      private static int[] lowerRange;
22      private static int[] upperRange;
23      private static int[] lowerError;
24      private static int[] upperError;
25      private static Random factor;
26      
27      /** Creates a new instance of LightSensorColorLibrary */
28      public LightSensorColorLibrary(boolean[] d, int[] lr, int[] ur, int[] le, int[] ue){
29          desiredColors = d;
30          lowerRange = lr;
31          upperRange = ur;
32          lowerError = le;
33          upperError = ue;
34          factor = new Random();
35      }
36      
37      public static int getValue(Color color){
38      
39          int index = getColorIndex(color);
40          if(index != 9999){
41              if(desiredColors[index]){
42                  int min = lowerRange[index]-lowerError[index];
43                  int max = upperRange[index]+upperError[index];
44                  int difference = max - min;
45                  
46                  return (min + (int)(factor.nextInt(difference+1)));
47              }
48              else return 911;
49          }
50          
51          return 911;
52      }
53      
54      private static int getColorIndex(Color c){
55         
56          if(c.getRGB()==(Color.black.getRGB()))return 0;
57          else if(c.getRGB()==(Color.blue.getRGB()))return 1;
58          else if(c.getRGB()==(Color.cyan.getRGB()))return 2;
59          else if(c.getRGB()==(Color.gray.getRGB()))return 3;
60          else if(c.getRGB()==(Color.green.getRGB()))return 4;
61          else if(c.getRGB()==(Color.magenta.getRGB()))return 5;
62          else if(c.getRGB()==(Color.orange.getRGB()))return 6;
63          else if(c.getRGB()==(Color.pink.getRGB()))return 7;
64          else if(c.getRGB()==(Color.red.getRGB()))return 8;
65          else if(c.getRGB()==(Color.white.getRGB()))return 9;
66          else if(c.getRGB()==(Color.yellow.getRGB()))return 10;
67          else return 9999;
68          
69      }
70      
71      public void updateDesiredColors(boolean[] d, int[] lr, int[] ur, int[] le, int[] ue){
72          desiredColors = d;
73          lowerRange = lr;
74          upperRange = ur;
75          lowerError = le;
76          upperError = ue;
77      }
78      
79      public boolean[] getColorInfo(){
80          return desiredColors;
81      }
82      
83      public int[] getLowerRangeInfo(){
84          return lowerRange;
85      }
86      
87      public int[] getUpperRangeInfo(){
88          return upperRange;
89      }
90      
91      public int[] getLowerErrorInfo(){
92          return lowerError;
93      }
94      
95      public int[] getUpperErrorInfo(){
96          return upperError;
97      }
98      
99      
100     
101     
102     
103 }
104