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 rgb = StrUtil.split(colorName, ','); + if (3 == rgb.size()) { + final Integer r = Convert.toInt(rgb.get(0)); + final Integer g = Convert.toInt(rgb.get(1)); + final Integer b = Convert.toInt(rgb.get(2)); + if (false == ArrayUtil.hasNull(r, g, b)) { + return new Color(r, g, b); + } + } else { + return null; + } + } + return null; + } + + /** + * 获取一个RGB值对应的颜色 + * + * @param rgb RGB值 + * @return {@link Color} + * @since 4.1.14 + */ + public static Color getColor(int rgb) { + return new Color(rgb); + } + + /** + * 16进制的颜色值转换为Color对象,例如#fcf6d6 + * + * @param hex 16进制的颜色值,例如#fcf6d6 + * @return {@link Color} + * @since 4.1.14 + */ + public static Color hexToColor(String hex) { + return getColor(Integer.parseInt(StrUtil.removePrefix(hex, "#"), 16)); + } + + /** + * 叠加颜色 + * + * @param color1 颜色1 + * @param color2 颜色2 + * @return 叠加后的颜色 + */ + public static Color add(Color color1, Color color2) { + double r1 = color1.getRed(); + double g1 = color1.getGreen(); + double b1 = color1.getBlue(); + double a1 = color1.getAlpha(); + double r2 = color2.getRed(); + double g2 = color2.getGreen(); + double b2 = color2.getBlue(); + double a2 = color2.getAlpha(); + int r = (int) ((r1 * a1 / 255 + r2 * a2 / 255) / (a1 / 255 + a2 / 255)); + int g = (int) ((g1 * a1 / 255 + g2 * a2 / 255) / (a1 / 255 + a2 / 255)); + int b = (int) ((b1 * a1 / 255 + b2 * a2 / 255) / (a1 / 255 + a2 / 255)); + return new Color(r, g, b); + } + + /** + * 生成随机颜色 + * + * @return 随机颜色 + * @since 3.1.2 + */ + public static Color randomColor() { + return randomColor(null); + } + + /** + * 生成随机颜色 + * + * @param random 随机对象 {@link Random} + * @return 随机颜色 + * @since 3.1.2 + */ + public static Color randomColor(Random random) { + if (null == random) { + random = RandomUtil.getRandom(); + } + return new Color(random.nextInt(RGB_COLOR_BOUND), random.nextInt(RGB_COLOR_BOUND), random.nextInt(RGB_COLOR_BOUND)); + } + + /** + * 获取给定图片的主色调,背景填充用 + * + * @param image {@link BufferedImage} + * @param rgbFilters 过滤多种颜色 + * @return {@link String} #ffffff + * @since 5.6.7 + */ + public static String getMainColor(BufferedImage image, int[]... rgbFilters) { + int r, g, b; + Map countMap = new HashMap<>(); + int width = image.getWidth(); + int height = image.getHeight(); + int minx = image.getMinX(); + int miny = image.getMinY(); + for (int i = minx; i < width; i++) { + for (int j = miny; j < height; j++) { + int pixel = image.getRGB(i, j); + r = (pixel & 0xff0000) >> 16; + g = (pixel & 0xff00) >> 8; + b = (pixel & 0xff); + if (matchFilters(r, g, b, rgbFilters)) { + continue; + } + countMap.merge(r + "-" + g + "-" + b, 1L, Long::sum); + } + } + String maxColor = null; + long maxCount = 0; + for (Map.Entry entry : countMap.entrySet()) { + String key = entry.getKey(); + Long count = entry.getValue(); + if (count > maxCount) { + maxColor = key; + maxCount = count; + } + } + final String[] splitRgbStr = StrUtil.splitToArray(maxColor, '-'); + String rHex = Integer.toHexString(Integer.parseInt(splitRgbStr[0])); + String gHex = Integer.toHexString(Integer.parseInt(splitRgbStr[1])); + String bHex = Integer.toHexString(Integer.parseInt(splitRgbStr[2])); + rHex = rHex.length() == 1 ? "0" + rHex : rHex; + gHex = gHex.length() == 1 ? "0" + gHex : gHex; + bHex = bHex.length() == 1 ? "0" + bHex : bHex; + return "#" + rHex + gHex + bHex; + } + + /** + * 给定RGB是否匹配过滤器中任何一个RGB颜色 + * + * @param r R + * @param g G + * @param b B + * @param rgbFilters 颜色过滤器 + * @return 是否匹配 + */ + private static boolean matchFilters(int r, int g, int b, int[]... rgbFilters) { + if (rgbFilters != null && rgbFilters.length > 0) { + for (int[] rgbFilter : rgbFilters) { + if (r == rgbFilter[0] && g == rgbFilter[1] && b == rgbFilter[2]) { + return true; + } + } + } + return false; + } +} diff --git a/hutool-swing/src/main/java/cn/hutool/swing/img/GraphicsUtil.java b/hutool-swing/src/main/java/cn/hutool/swing/img/GraphicsUtil.java index 9dc2561fc..db099778e 100755 --- a/hutool-swing/src/main/java/cn/hutool/swing/img/GraphicsUtil.java +++ b/hutool-swing/src/main/java/cn/hutool/swing/img/GraphicsUtil.java @@ -115,7 +115,7 @@ public class GraphicsUtil { for (int i = 0; i < len; i++) { if (null == color) { // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。 - g.setColor(ImgUtil.randomColor()); + g.setColor(ColorUtil.randomColor()); } g.drawString(String.valueOf(str.charAt(i)), i * charWidth, midY); } diff --git a/hutool-swing/src/main/java/cn/hutool/swing/img/ImgUtil.java b/hutool-swing/src/main/java/cn/hutool/swing/img/ImgUtil.java index 08feaffb0..1be64b570 100755 --- a/hutool-swing/src/main/java/cn/hutool/swing/img/ImgUtil.java +++ b/hutool-swing/src/main/java/cn/hutool/swing/img/ImgUtil.java @@ -1,18 +1,15 @@ package cn.hutool.swing.img; import cn.hutool.core.codec.Base64; -import cn.hutool.core.convert.Convert; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.resource.Resource; import cn.hutool.core.lang.Assert; -import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.math.NumberUtil; -import cn.hutool.core.util.ObjUtil; -import cn.hutool.core.util.RandomUtil; -import cn.hutool.core.text.StrUtil; import cn.hutool.core.net.URLUtil; +import cn.hutool.core.text.StrUtil; +import cn.hutool.core.util.ObjUtil; import javax.imageio.IIOImage; import javax.imageio.ImageIO; @@ -54,11 +51,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; -import java.util.HashMap; import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Random; /** * 图片处理工具类:
@@ -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
-	 * 
- *

- * 方法来自: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 rgb = StrUtil.split(colorName, ','); - if (3 == rgb.size()) { - final Integer r = Convert.toInt(rgb.get(0)); - final Integer g = Convert.toInt(rgb.get(1)); - final Integer b = Convert.toInt(rgb.get(2)); - if (false == ArrayUtil.hasNull(r, g, b)) { - return new Color(r, g, b); - } - } else { - return null; - } - } - return null; - } - - /** - * 生成随机颜色 - * - * @return 随机颜色 - * @since 3.1.2 - */ - public static Color randomColor() { - return randomColor(null); - } - - /** - * 生成随机颜色 - * - * @param random 随机对象 {@link Random} - * @return 随机颜色 - * @since 3.1.2 - */ - public static Color randomColor(Random random) { - if (null == random) { - random = RandomUtil.getRandom(); - } - return new Color(random.nextInt(RGB_COLOR_BOUND), random.nextInt(RGB_COLOR_BOUND), random.nextInt(RGB_COLOR_BOUND)); - } - /** * 获得修正后的矩形坐标位置,变为以背景中心为基准坐标(即x,y == 0,0时,处于背景正中) * @@ -2053,73 +1855,6 @@ public class ImgUtil { ); } - /** - * 获取给定图片的主色调,背景填充用 - * - * @param image {@link BufferedImage} - * @param rgbFilters 过滤多种颜色 - * @return {@link String} #ffffff - * @since 5.6.7 - */ - public static String getMainColor(final BufferedImage image, final int[]... rgbFilters) { - int r, g, b; - final Map countMap = new HashMap<>(); - final int width = image.getWidth(); - final int height = image.getHeight(); - final int minx = image.getMinX(); - final int miny = image.getMinY(); - for (int i = minx; i < width; i++) { - for (int j = miny; j < height; j++) { - final int pixel = image.getRGB(i, j); - r = (pixel & 0xff0000) >> 16; - g = (pixel & 0xff00) >> 8; - b = (pixel & 0xff); - if (matchFilters(r, g, b, rgbFilters)) { - continue; - } - countMap.merge(r + "-" + g + "-" + b, 1L, Long::sum); - } - } - String maxColor = null; - long maxCount = 0; - for (final Map.Entry entry : countMap.entrySet()) { - final String key = entry.getKey(); - final Long count = entry.getValue(); - if (count > maxCount) { - maxColor = key; - maxCount = count; - } - } - final String[] splitRgbStr = StrUtil.splitToArray(maxColor, '-'); - String rHex = Integer.toHexString(Integer.parseInt(splitRgbStr[0])); - String gHex = Integer.toHexString(Integer.parseInt(splitRgbStr[1])); - String bHex = Integer.toHexString(Integer.parseInt(splitRgbStr[2])); - rHex = rHex.length() == 1 ? "0" + rHex : rHex; - gHex = gHex.length() == 1 ? "0" + gHex : gHex; - bHex = bHex.length() == 1 ? "0" + bHex : bHex; - return "#" + rHex + gHex + bHex; - } - - /** - * 给定RGB是否匹配过滤器中任何一个RGB颜色 - * - * @param r R - * @param g G - * @param b B - * @param rgbFilters 颜色过滤器 - * @return 是否匹配 - */ - private static boolean matchFilters(final int r, final int g, final int b, final int[]... rgbFilters) { - if (rgbFilters != null && rgbFilters.length > 0) { - for (final int[] rgbFilter : rgbFilters) { - if (r == rgbFilter[0] && g == rgbFilter[1] && b == rgbFilter[2]) { - return true; - } - } - } - return false; - } - // ------------------------------------------------------------------------------------------------------ 背景图换算 /** diff --git a/hutool-swing/src/test/java/cn/hutool/swing/img/ImgUtilTest.java b/hutool-swing/src/test/java/cn/hutool/swing/img/ImgUtilTest.java index 832fee333..18dc2a030 100755 --- a/hutool-swing/src/test/java/cn/hutool/swing/img/ImgUtilTest.java +++ b/hutool-swing/src/test/java/cn/hutool/swing/img/ImgUtilTest.java @@ -121,7 +121,7 @@ public class ImgUtilTest { @Test public void toHexTest(){ - final String s = ImgUtil.toHex(Color.RED); + final String s = ColorUtil.toHex(Color.RED); Assert.assertEquals("#FF0000", s); } @@ -143,7 +143,7 @@ public class ImgUtilTest { @Test public void getMainColor() throws MalformedURLException { final BufferedImage read = ImgUtil.read(new URL("https://pic2.zhimg.com/v2-94f5552f2b142ff575306850c5bab65d_b.png")); - final String mainColor = ImgUtil.getMainColor(read, new int[]{64,84,116}); + final String mainColor = ColorUtil.getMainColor(read, new int[]{64,84,116}); System.out.println(mainColor); }