1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.captcha.util;
20
21 import java.awt.Color;
22
23
24
25
26
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
36
37
38
39 public static Color generateRandomColor(Color startFrom)
40 {
41
42
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
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
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
62
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 }