package cn.com.qmth.stmms.common.utils;

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

public class ValidateCodeUtil {

    public static Font mFont = new Font("Arial", Font.BOLD, 16); // 设置图形验证码中的字符串的字体的大小

    /**
     * 生成随机颜色
     * 
     * @param rfc
     *            红色值的范围下限
     * @param rbc
     *            红色值的范围上限
     * @param gfc
     *            绿色值的范围下限
     * @param gbc
     *            绿色值的范围上限
     * @param bfc
     *            蓝色值的范围下限
     * @param bbc
     *            蓝色值的范围上限
     * @return
     */
    public static Color getRandColor(int rfc, int rbc, int gfc, int gbc, int bfc, int bbc) {
        Random random = new Random();
        if (rfc > 255)
            rfc = 255;
        if (rbc > 255)
            rbc = 255;
        if (gfc > 255)
            gfc = 255;
        if (gbc > 255)
            gbc = 255;
        if (bfc > 255)
            bfc = 255;
        if (bbc > 255)
            bbc = 255;
        int r = rfc + random.nextInt(rbc - rfc);
        int g = gfc + random.nextInt(gbc - gfc);
        int b = bfc + random.nextInt(bbc - bfc);
        return new Color(r, g, b);
    }

    /**
     * 随机生成字符串，不包含I和l，0和O
     * 
     * @return
     */
    public static String getRandomChar() {
        int rand = (int) Math.round(Math.random() * 2);
        long itmp = 0;
        char ctmp = '\u0000';
        switch (rand) {
        case 1:
            itmp = Math.round(Math.random() * 25 + 65);
            ctmp = (char) itmp;
            if (ctmp == 'I' || ctmp == 'O')
                return getRandomChar();
            return String.valueOf(ctmp);
        case 2:
            itmp = Math.round(Math.random() * 25 + 97);
            ctmp = (char) itmp;
            if (ctmp == 'l')
                return getRandomChar();
            return String.valueOf(ctmp);
        default:
            itmp = Math.round(Math.random() * 8) + 1;
            return String.valueOf(itmp);
        }
    }
}
