校验颜色工具类
public class ColorUtil {
private static Integer min = 0;
private static Integer max = 16777215;
public static String judgeHexColor(String color) {
String error = null;
if (StringUtils.isBlank(color)) {
return error = "颜色不能为空";
}
color = color.trim();
if (!color.startsWith("#")) {
return error = "颜色不合法";
}
color = color.substring(1);
if (StringUtils.isBlank(color)) {
return error = "颜色不合法";
}
color = color.trim();
if (color.length() != 6) {
return error = "颜色数值长度不合法";
}
try {
int i = Integer.parseInt(color, 16);
if (i < min || i > max) {
return error = "颜色不合法";
}
} catch (Exception e) {
return error = "颜色不合法";
}
return error;
}
}