All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tinygroup.imagecreator.impl.RandomUtil Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * --------------------------------------------------------------------------
 *  版权 (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  本开源软件遵循 GPL 3.0 协议;
 *  如果您不遵循此协议,则不被允许使用此文件。
 *  你可以从下面的地址获取完整的协议文本
 *
 *       http://www.gnu.org/licenses/gpl.html
 */
package org.tinygroup.imagecreator.impl;

import java.awt.Color;
import java.util.Date;
import java.util.Random;

/**
 * 随机数工具
 * 
 * @author luoguo
 * 
 */
final class RandomUtil {
	private static final int MAX_PRIMARY_COLOR = 256;

	private RandomUtil() {
	}

	// 随机数发生器
	private static Random rnd = new Random(new Date().getTime());

	/**
	 * 随机获取颜色对象
	 */
	private static Color getRandomColor(Color base, int range) {

		int red = (base.getRed() + rnd.nextInt(range)) % MAX_PRIMARY_COLOR;
		int green = (base.getGreen() + rnd.nextInt(range)) % MAX_PRIMARY_COLOR;
		int blue = (base.getBlue() + rnd.nextInt(range)) % MAX_PRIMARY_COLOR;

		return new Color(red, green, blue);
	}

	/**
	 * 只返回一个整数的随机数
	 * 
	 * @param intValue
	 * @return
	 */
	public static int getInt(int intValue) {
		return rnd.nextInt(intValue);
	}

	/**
	 * 返回指定浮点数周围的随机数
	 * 
	 * @param baseValue
	 *            基本值
	 * @param range
	 *            波动范围
	 * @return
	 */
	public static double getDouble(double baseValue, int range) {
		if (range == 0) {
			return baseValue;
		}
		return baseValue + rnd.nextInt(range) - range / 2;
	}

	/**
	 * 返回随机颜色
	 * 
	 * @param fontColor
	 *            基本颜色,如果为空,则产生完全随机的颜色
	 * @param colorRange
	 *            颜色范围,为0时,不做随机转换
	 * @return
	 */
	public static Color getColor(Color fontColor, int colorRange) {
		if (fontColor == null) {// 其于某一颜色产生其周边颜色
			return getRandomColor(Color.WHITE, colorRange);
		} else {
			if (colorRange == 0) {// 没有波动范围,则不作随机转换
				return fontColor;
			} else {
				return new Color(getInt(colorRange), getInt(colorRange),
						getInt(colorRange));
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy