diff --git a/hutool-swing/src/main/java/cn/hutool/swing/captcha/CircleCaptcha.java b/hutool-swing/src/main/java/cn/hutool/swing/captcha/CircleCaptcha.java index 19a2c357b..2450b4bc9 100755 --- a/hutool-swing/src/main/java/cn/hutool/swing/captcha/CircleCaptcha.java +++ b/hutool-swing/src/main/java/cn/hutool/swing/captcha/CircleCaptcha.java @@ -1,9 +1,9 @@ package cn.hutool.swing.captcha; -import cn.hutool.swing.img.GraphicsUtil; -import cn.hutool.swing.img.ImgUtil; import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.RandomUtil; +import cn.hutool.swing.img.ColorUtil; +import cn.hutool.swing.img.GraphicsUtil; import java.awt.Color; import java.awt.Graphics2D; @@ -57,7 +57,7 @@ public class CircleCaptcha extends AbstractCaptcha { @Override public Image createImage(final String code) { final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - final Graphics2D g = ImgUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE)); + final Graphics2D g = GraphicsUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE)); // 随机画干扰圈圈 drawInterfere(g); @@ -92,7 +92,7 @@ public class CircleCaptcha extends AbstractCaptcha { final ThreadLocalRandom random = RandomUtil.getRandom(); for (int i = 0; i < this.interfereCount; i++) { - g.setColor(ImgUtil.randomColor(random)); + g.setColor(ColorUtil.randomColor(random)); g.drawOval(random.nextInt(width), random.nextInt(height), random.nextInt(height >> 1), random.nextInt(height >> 1)); } } diff --git a/hutool-swing/src/main/java/cn/hutool/swing/captcha/LineCaptcha.java b/hutool-swing/src/main/java/cn/hutool/swing/captcha/LineCaptcha.java index e3f95e725..e9298a2f7 100755 --- a/hutool-swing/src/main/java/cn/hutool/swing/captcha/LineCaptcha.java +++ b/hutool-swing/src/main/java/cn/hutool/swing/captcha/LineCaptcha.java @@ -2,8 +2,8 @@ package cn.hutool.swing.captcha; import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.RandomUtil; +import cn.hutool.swing.img.ColorUtil; import cn.hutool.swing.img.GraphicsUtil; -import cn.hutool.swing.img.ImgUtil; import java.awt.Color; import java.awt.Graphics; @@ -88,7 +88,7 @@ public class LineCaptcha extends AbstractCaptcha { final int ys = random.nextInt(height); final int xe = xs + random.nextInt(width / 8); final int ye = ys + random.nextInt(height / 8); - g.setColor(ImgUtil.randomColor(random)); + g.setColor(ColorUtil.randomColor(random)); g.drawLine(xs, ys, xe, ye); } } diff --git a/hutool-swing/src/main/java/cn/hutool/swing/captcha/ShearCaptcha.java b/hutool-swing/src/main/java/cn/hutool/swing/captcha/ShearCaptcha.java index bd72e738a..893902c6a 100755 --- a/hutool-swing/src/main/java/cn/hutool/swing/captcha/ShearCaptcha.java +++ b/hutool-swing/src/main/java/cn/hutool/swing/captcha/ShearCaptcha.java @@ -1,9 +1,9 @@ package cn.hutool.swing.captcha; -import cn.hutool.swing.img.GraphicsUtil; -import cn.hutool.swing.img.ImgUtil; import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.RandomUtil; +import cn.hutool.swing.img.ColorUtil; +import cn.hutool.swing.img.GraphicsUtil; import java.awt.Color; import java.awt.Graphics; @@ -65,7 +65,7 @@ public class ShearCaptcha extends AbstractCaptcha { // 扭曲 shear(g, this.width, this.height, ObjUtil.defaultIfNull(this.background, Color.WHITE)); // 画干扰线 - drawInterfere(g, 0, RandomUtil.randomInt(this.height) + 1, this.width, RandomUtil.randomInt(this.height) + 1, this.interfereCount, ImgUtil.randomColor()); + drawInterfere(g, 0, RandomUtil.randomInt(this.height) + 1, this.width, RandomUtil.randomInt(this.height) + 1, this.interfereCount, ColorUtil.randomColor()); return image; } diff --git a/hutool-swing/src/main/java/cn/hutool/swing/img/BackgroundRemoval.java b/hutool-swing/src/main/java/cn/hutool/swing/img/BackgroundRemoval.java index 310a597a0..7b11ed332 100755 --- a/hutool-swing/src/main/java/cn/hutool/swing/img/BackgroundRemoval.java +++ b/hutool-swing/src/main/java/cn/hutool/swing/img/BackgroundRemoval.java @@ -132,7 +132,7 @@ public class BackgroundRemoval { for (int x = image.getMinX(); x < image.getWidth(); x++) { // 获取像素的16进制 int rgb = image.getRGB(x, y); - final String hex = ImgUtil.toHex((rgb & 0xff0000) >> 16, (rgb & 0xff00) >> 8, (rgb & 0xff)); + final String hex = ColorUtil.toHex((rgb & 0xff0000) >> 16, (rgb & 0xff00) >> 8, (rgb & 0xff)); final boolean isTrue = ArrayUtil.contains(removeRgb, hex) || areColorsWithinTolerance(hexToRgb(mainColor), new Color(Integer.parseInt(hex.substring(1), 16)), tolerance); if (isTrue) { @@ -181,28 +181,28 @@ public class BackgroundRemoval { final int height = image.getHeight() - 1; // 左上 final int leftUpPixel = image.getRGB(1, 1); - final String leftUp = ImgUtil.toHex((leftUpPixel & 0xff0000) >> 16, (leftUpPixel & 0xff00) >> 8, (leftUpPixel & 0xff)); + final String leftUp = ColorUtil.toHex((leftUpPixel & 0xff0000) >> 16, (leftUpPixel & 0xff00) >> 8, (leftUpPixel & 0xff)); // 上中 final int upMiddlePixel = image.getRGB(width / 2, 1); - final String upMiddle = ImgUtil.toHex((upMiddlePixel & 0xff0000) >> 16, (upMiddlePixel & 0xff00) >> 8, (upMiddlePixel & 0xff)); + final String upMiddle = ColorUtil.toHex((upMiddlePixel & 0xff0000) >> 16, (upMiddlePixel & 0xff00) >> 8, (upMiddlePixel & 0xff)); // 右上 final int rightUpPixel = image.getRGB(width, 1); - final String rightUp = ImgUtil.toHex((rightUpPixel & 0xff0000) >> 16, (rightUpPixel & 0xff00) >> 8, (rightUpPixel & 0xff)); + final String rightUp = ColorUtil.toHex((rightUpPixel & 0xff0000) >> 16, (rightUpPixel & 0xff00) >> 8, (rightUpPixel & 0xff)); // 右中 final int rightMiddlePixel = image.getRGB(width, height / 2); - final String rightMiddle = ImgUtil.toHex((rightMiddlePixel & 0xff0000) >> 16, (rightMiddlePixel & 0xff00) >> 8, (rightMiddlePixel & 0xff)); + final String rightMiddle = ColorUtil.toHex((rightMiddlePixel & 0xff0000) >> 16, (rightMiddlePixel & 0xff00) >> 8, (rightMiddlePixel & 0xff)); // 右下 final int lowerRightPixel = image.getRGB(width, height); - final String lowerRight = ImgUtil.toHex((lowerRightPixel & 0xff0000) >> 16, (lowerRightPixel & 0xff00) >> 8, (lowerRightPixel & 0xff)); + final String lowerRight = ColorUtil.toHex((lowerRightPixel & 0xff0000) >> 16, (lowerRightPixel & 0xff00) >> 8, (lowerRightPixel & 0xff)); // 下中 final int lowerMiddlePixel = image.getRGB(width / 2, height); - final String lowerMiddle = ImgUtil.toHex((lowerMiddlePixel & 0xff0000) >> 16, (lowerMiddlePixel & 0xff00) >> 8, (lowerMiddlePixel & 0xff)); + final String lowerMiddle = ColorUtil.toHex((lowerMiddlePixel & 0xff0000) >> 16, (lowerMiddlePixel & 0xff00) >> 8, (lowerMiddlePixel & 0xff)); // 左下 final int leftLowerPixel = image.getRGB(1, height); - final String leftLower = ImgUtil.toHex((leftLowerPixel & 0xff0000) >> 16, (leftLowerPixel & 0xff00) >> 8, (leftLowerPixel & 0xff)); + final String leftLower = ColorUtil.toHex((leftLowerPixel & 0xff0000) >> 16, (leftLowerPixel & 0xff00) >> 8, (leftLowerPixel & 0xff)); // 左中 final int leftMiddlePixel = image.getRGB(1, height / 2); - final String leftMiddle = ImgUtil.toHex((leftMiddlePixel & 0xff0000) >> 16, (leftMiddlePixel & 0xff00) >> 8, (leftMiddlePixel & 0xff)); + final String leftMiddle = ColorUtil.toHex((leftMiddlePixel & 0xff0000) >> 16, (leftMiddlePixel & 0xff00) >> 8, (leftMiddlePixel & 0xff)); // 需要删除的RGB元素 return new String[]{leftUp, upMiddle, rightUp, rightMiddle, lowerRight, lowerMiddle, leftLower, leftMiddle}; } @@ -323,7 +323,7 @@ public class BackgroundRemoval { // rgb 的数量只有3个 final int rgbLength = 3; if (strings.length == rgbLength) { - return ImgUtil.toHex(Integer.parseInt(strings[0]), Integer.parseInt(strings[1]), + return ColorUtil.toHex(Integer.parseInt(strings[0]), Integer.parseInt(strings[1]), Integer.parseInt(strings[2])); } return StrUtil.EMPTY; diff --git a/hutool-swing/src/main/java/cn/hutool/swing/img/ColorUtil.java b/hutool-swing/src/main/java/cn/hutool/swing/img/ColorUtil.java new file mode 100644 index 000000000..fc9244924 --- /dev/null +++ b/hutool-swing/src/main/java/cn/hutool/swing/img/ColorUtil.java @@ -0,0 +1,263 @@ +package cn.hutool.swing.img; + +import cn.hutool.core.convert.Convert; +import cn.hutool.core.text.StrUtil; +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.RandomUtil; + +import java.awt.Color; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; + +/** + * 颜色工具类 + * + * @since 5.8.7 + */ +public class ColorUtil { + + /** + * RGB颜色范围上限 + */ + private static final int RGB_COLOR_BOUND = 256; + + /** + * Color对象转16进制表示,例如#fcf6d6 + * + * @param color {@link Color} + * @return 16进制的颜色值,例如#fcf6d6 + * @since 4.1.14 + */ + public static String toHex(Color color) { + return toHex(color.getRed(), color.getGreen(), color.getBlue()); + } + + /** + * RGB颜色值转换成十六进制颜色码 + * + * @param r 红(R) + * @param g 绿(G) + * @param b 蓝(B) + * @return 返回字符串形式的 十六进制颜色码 如 + */ + public static String toHex(int r, int g, int b) { + // rgb 小于 255 + if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) { + throw new IllegalArgumentException("RGB must be 0~255!"); + } + return String.format("#%02X%02X%02X", r, g, b); + } + + /** + * 将颜色值转换成具体的颜色类型 汇集了常用的颜色集,支持以下几种形式: + * + *
+ * 1. 颜色的英文名(大小写皆可) + * 2. 16进制表示,例如:#fcf6d6或者$fcf6d6 + * 3. RGB形式,例如:13,148,252 + *+ *
+ * 方法来自:com.lnwazg.kit
+ *
+ * @param colorName 颜色的英文名,16进制表示或RGB表示
+ * @return {@link Color}
+ * @since 4.1.14
+ */
+ public static Color getColor(String colorName) {
+ if (StrUtil.isBlank(colorName)) {
+ return null;
+ }
+ colorName = colorName.toUpperCase();
+
+ if ("BLACK".equals(colorName)) {
+ return Color.BLACK;
+ } else if ("WHITE".equals(colorName)) {
+ return Color.WHITE;
+ } else if ("LIGHTGRAY".equals(colorName) || "LIGHT_GRAY".equals(colorName)) {
+ return Color.LIGHT_GRAY;
+ } else if ("GRAY".equals(colorName)) {
+ return Color.GRAY;
+ } else if ("DARKGRAY".equals(colorName) || "DARK_GRAY".equals(colorName)) {
+ return Color.DARK_GRAY;
+ } else if ("RED".equals(colorName)) {
+ return Color.RED;
+ } else if ("PINK".equals(colorName)) {
+ return Color.PINK;
+ } else if ("ORANGE".equals(colorName)) {
+ return Color.ORANGE;
+ } else if ("YELLOW".equals(colorName)) {
+ return Color.YELLOW;
+ } else if ("GREEN".equals(colorName)) {
+ return Color.GREEN;
+ } else if ("MAGENTA".equals(colorName)) {
+ return Color.MAGENTA;
+ } else if ("CYAN".equals(colorName)) {
+ return Color.CYAN;
+ } else if ("BLUE".equals(colorName)) {
+ return Color.BLUE;
+ } else if ("DARKGOLD".equals(colorName)) {
+ // 暗金色
+ return hexToColor("#9e7e67");
+ } else if ("LIGHTGOLD".equals(colorName)) {
+ // 亮金色
+ return hexToColor("#ac9c85");
+ } else if (StrUtil.startWith(colorName, '#')) {
+ return hexToColor(colorName);
+ } else if (StrUtil.startWith(colorName, '$')) {
+ // 由于#在URL传输中无法传输,因此用$代替#
+ return hexToColor("#" + colorName.substring(1));
+ } else {
+ // rgb值
+ final List
- * 方法来自:com.lnwazg.kit
- *
- * @param colorName 颜色的英文名,16进制表示或RGB表示
- * @return {@link Color}
- * @since 4.1.14
- */
- public static Color getColor(String colorName) {
- if (StrUtil.isBlank(colorName)) {
- return null;
- }
- colorName = colorName.toUpperCase();
-
- if ("BLACK".equals(colorName)) {
- return Color.BLACK;
- } else if ("WHITE".equals(colorName)) {
- return Color.WHITE;
- } else if ("LIGHTGRAY".equals(colorName) || "LIGHT_GRAY".equals(colorName)) {
- return Color.LIGHT_GRAY;
- } else if ("GRAY".equals(colorName)) {
- return Color.GRAY;
- } else if ("DARKGRAY".equals(colorName) || "DARK_GRAY".equals(colorName)) {
- return Color.DARK_GRAY;
- } else if ("RED".equals(colorName)) {
- return Color.RED;
- } else if ("PINK".equals(colorName)) {
- return Color.PINK;
- } else if ("ORANGE".equals(colorName)) {
- return Color.ORANGE;
- } else if ("YELLOW".equals(colorName)) {
- return Color.YELLOW;
- } else if ("GREEN".equals(colorName)) {
- return Color.GREEN;
- } else if ("MAGENTA".equals(colorName)) {
- return Color.MAGENTA;
- } else if ("CYAN".equals(colorName)) {
- return Color.CYAN;
- } else if ("BLUE".equals(colorName)) {
- return Color.BLUE;
- } else if ("DARKGOLD".equals(colorName)) {
- // 暗金色
- return hexToColor("#9e7e67");
- } else if ("LIGHTGOLD".equals(colorName)) {
- // 亮金色
- return hexToColor("#ac9c85");
- } else if (StrUtil.startWith(colorName, '#')) {
- return hexToColor(colorName);
- } else if (StrUtil.startWith(colorName, '$')) {
- // 由于#在URL传输中无法传输,因此用$代替#
- return hexToColor("#" + colorName.substring(1));
- } else {
- // rgb值
- final List
@@ -76,12 +69,6 @@ public class ImgUtil {
public static final String IMAGE_TYPE_PNG = "png";// 可移植网络图形
public static final String IMAGE_TYPE_PSD = "psd";// Photoshop的专用格式Photoshop
- /**
- * RGB颜色范围上限
- */
- private static final int RGB_COLOR_BOUND = 256;
-
-
// ---------------------------------------------------------------------------------------------------------------------- scale
/**
@@ -1437,43 +1424,6 @@ public class ImgUtil {
false));
}
- /**
- * 根据文件创建字体
- * 首先尝试创建{@link Font#TRUETYPE_FONT}字体,此类字体无效则创建{@link Font#TYPE1_FONT}
- *
- * @param fontFile 字体文件
- * @return {@link Font}
- * @since 3.0.9
- */
- public static Font createFont(final File fontFile) {
- return FontUtil.createFont(fontFile);
- }
-
- /**
- * 根据文件创建字体
- * 首先尝试创建{@link Font#TRUETYPE_FONT}字体,此类字体无效则创建{@link Font#TYPE1_FONT}
- *
- * @param fontStream 字体流
- * @return {@link Font}
- * @since 3.0.9
- */
- public static Font createFont(final InputStream fontStream) {
- return FontUtil.createFont(fontStream);
- }
-
- /**
- * 创建{@link Graphics2D}
- *
- * @param image {@link BufferedImage}
- * @param color {@link Color}背景颜色以及当前画笔颜色
- * @return {@link Graphics2D}
- * @see GraphicsUtil#createGraphics(BufferedImage, Color)
- * @since 3.2.3
- */
- public static Graphics2D createGraphics(final BufferedImage image, final Color color) {
- return GraphicsUtil.createGraphics(image, color);
- }
-
/**
* 写出图像为JPG格式
*
@@ -1889,154 +1839,6 @@ public class ImgUtil {
// -------------------------------------------------------------------------------------------------------------------- Color
- /**
- * Color对象转16进制表示,例如#fcf6d6
- *
- * @param color {@link Color}
- * @return 16进制的颜色值,例如#fcf6d6
- * @since 4.1.14
- */
- public static String toHex(final Color color) {
- return toHex(color.getRed(), color.getGreen(), color.getBlue());
- }
-
- /**
- * RGB颜色值转换成十六进制颜色码
- *
- * @param r 红(R)
- * @param g 绿(G)
- * @param b 蓝(B)
- * @return 返回字符串形式的 十六进制颜色码 如
- */
- public static String toHex(final int r, final int g, final int b) {
- // rgb 小于 255
- if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
- throw new IllegalArgumentException("RGB must be 0~255!");
- }
- return String.format("#%02X%02X%02X", r, g, b);
- }
-
- /**
- * 16进制的颜色值转换为Color对象,例如#fcf6d6
- *
- * @param hex 16进制的颜色值,例如#fcf6d6
- * @return {@link Color}
- * @since 4.1.14
- */
- public static Color hexToColor(final String hex) {
- return getColor(Integer.parseInt(StrUtil.removePrefix(hex, "#"), 16));
- }
-
- /**
- * 获取一个RGB值对应的颜色
- *
- * @param rgb RGB值
- * @return {@link Color}
- * @since 4.1.14
- */
- public static Color getColor(final int rgb) {
- return new Color(rgb);
- }
-
- /**
- * 将颜色值转换成具体的颜色类型 汇集了常用的颜色集,支持以下几种形式:
- *
- *
- * 1. 颜色的英文名(大小写皆可)
- * 2. 16进制表示,例如:#fcf6d6或者$fcf6d6
- * 3. RGB形式,例如:13,148,252
- *
- *