View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.captcha.util;
20  
21  import java.awt.Color;
22  
23  /**
24   * This class is responsible for generating CAPTCHA random color...
25   * 
26   * @since 1.1.7
27   */
28  public class ColorGenerator
29  {
30  
31      private final static int COLOR_DEGREES = 255;
32      private final static int COLOR_GENERATOR_DELTA = 127;
33  
34      /**
35       * This method is used for generating a random color.
36       * @param startFrom -> the color that we should be away from.
37       * @return the new color.
38       */
39      public static Color generateRandomColor(Color startFrom)
40      {
41  
42          /* if the startingFrom color is null, then generate a new random color. */
43          if (startFrom == null)
44          {
45              return new Color((int) (Math.random() * COLOR_DEGREES), (int) (Math
46                      .random() * COLOR_DEGREES),
47                      (int) (Math.random() * COLOR_DEGREES));
48          }
49  
50          /* try to avoid the startFrom color. */
51          int startingRed = (startFrom.getRed() >= 128) ? 0 : 128;
52          int startingGreen = (startFrom.getGreen() >= 128) ? 0 : 128;
53          int startingBlue = (startFrom.getBlue() >= 128) ? 0 : 128;
54  
55          // generate the new random colors.  
56          int newRandomRed = (int) (Math.random() * (startingRed + COLOR_GENERATOR_DELTA));
57          int newRandomGreen = (int) (Math.random() * (startingGreen + COLOR_GENERATOR_DELTA));
58          int newRandomBlue = (int) (Math.random() * (startingBlue + COLOR_GENERATOR_DELTA));
59  
60          /* 
61           * If the newly generated color is less than the starting color 
62           * then add the starting color to it. 
63           */
64          if (newRandomRed < startingRed)
65          {
66              newRandomRed += startingRed;
67          }
68  
69          if (newRandomGreen < startingGreen)
70          {
71              newRandomGreen += startingGreen;
72          }
73  
74          if (newRandomBlue < startingBlue)
75          {
76              newRandomBlue += startingBlue;
77          }
78  
79          return new Color(newRandomRed, newRandomGreen, newRandomBlue);
80      }
81  }