View Javadoc

1   /*
2    * Copyright 2011 Király Attila
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.github.cage.image;
17  
18  import java.awt.Color;
19  import java.util.Random;
20  
21  import com.github.cage.IGenerator;
22  
23  /**
24   * Random RGB {@link Color} object generator. The returned {@link Color}-s are
25   * not too bright so they look good on white background. This class is thread
26   * safe.
27   * 
28   * @author akiraly
29   */
30  public class RgbColorGenerator implements IGenerator<Color> {
31  	private final Random rnd;
32  
33  	/**
34  	 * Constructor.
35  	 * 
36  	 * @param rnd
37  	 *            random generator to be used, can be null
38  	 */
39  	public RgbColorGenerator(Random rnd) {
40  		this.rnd = rnd != null ? rnd : new Random();
41  	}
42  
43  	public Color next() {
44  		int[] c = new int[3];
45  
46  		int i = rnd.nextInt(c.length);
47  
48  		for (int fi = 0; fi < c.length; fi++) {
49  			if (fi == i) {
50  				c[fi] = rnd.nextInt(71);
51  			} else {
52  				c[fi] = rnd.nextInt(256);
53  			}
54  		}
55  
56  		return new Color(c[0], c[1], c[2]);
57  	}
58  
59  }