diff --git a/designer-base/src/main/java/com/fr/widgettheme/theme/panel/ImageUtils.java b/designer-base/src/main/java/com/fr/widgettheme/theme/panel/ImageUtils.java index 63da90e6f7..1ef7275e46 100644 --- a/designer-base/src/main/java/com/fr/widgettheme/theme/panel/ImageUtils.java +++ b/designer-base/src/main/java/com/fr/widgettheme/theme/panel/ImageUtils.java @@ -46,7 +46,7 @@ public class ImageUtils { * @param color 颜色 */ private static void setRedPixels(int[] pixels, Color color) { - pixels[0] = pixels[0] > 0 && pixels[0] < 255 ? color.getRed() : 255; + pixels[0] = pixels[0] >= 0 && pixels[0] <= 255 ? color.getRed() : 255; } /** @@ -56,7 +56,7 @@ public class ImageUtils { * @param color 颜色 */ private static void setGreenPixels(int[] pixels, Color color) { - pixels[1] = pixels[1] > 0 && pixels[1] < 255 ? color.getGreen() : 255; + pixels[1] = pixels[1] >= 0 && pixels[1] <= 255 ? color.getGreen() : 255; } /** @@ -66,7 +66,7 @@ public class ImageUtils { * @param color 颜色 */ private static void setBluePixels(int[] pixels, Color color) { - pixels[2] = pixels[2] > 0 && pixels[2] < 255 ? color.getBlue() : 255; + pixels[2] = pixels[2] >= 0 && pixels[2] <= 255 ? color.getBlue() : 255; } /** diff --git a/designer-base/src/test/java/com/fr/widgettheme/theme/panel/ImageUtilsTest.java b/designer-base/src/test/java/com/fr/widgettheme/theme/panel/ImageUtilsTest.java new file mode 100644 index 0000000000..a1d2c875a4 --- /dev/null +++ b/designer-base/src/test/java/com/fr/widgettheme/theme/panel/ImageUtilsTest.java @@ -0,0 +1,93 @@ +package com.fr.widgettheme.theme.panel; + +import org.junit.Test; + +import javax.swing.ImageIcon; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + + +/** + * @author Leo.Qin + * @since 11.0 + * Created on 2025/5/22 + */ +public class ImageUtilsTest { + + @Test + public void testColorImage() { + // 创建一个测试用的BufferedImage + BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB); + Color testColor = new Color(123, 45, 67); + + // 执行测试方法 + BufferedImage result = ImageUtils.colorImage(image, testColor); + + // 验证结果 + assertNotNull(result); + assertEquals(10, result.getWidth()); + assertEquals(10, result.getHeight()); + + // 检查图像中的像素颜色是否符合预期 + int[] pixel = new int[4]; + result.getRaster().getPixel(5, 5, pixel); + assertEquals(testColor.getRed(), pixel[0]); + assertEquals(testColor.getGreen(), pixel[1]); + assertEquals(testColor.getBlue(), pixel[2]); + } + + @Test + public void testImageIconToBufferedImage() { + // 创建一个简单的测试ImageIcon + BufferedImage srcImage = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB); + ImageIcon icon = new ImageIcon(srcImage); + + // 执行测试方法 + BufferedImage result = ImageUtils.imageIconToBufferedImage(icon); + + // 验证结果 + assertNotNull(result); + assertEquals(icon.getIconWidth(), result.getWidth()); + assertEquals(icon.getIconHeight(), result.getHeight()); + assertEquals(BufferedImage.TYPE_INT_ARGB, result.getType()); + } + + @Test + public void testPixelProcessing() { + // 测试边界情况下的像素处理 + BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = image.createGraphics(); + + // 设置像素值为边界值 + g2d.setColor(new Color(0, 0, 0, 255)); + g2d.fillRect(0, 0, 1, 1); + + g2d.setColor(new Color(255, 255, 255, 255)); + g2d.fillRect(1, 1, 1, 1); + + g2d.dispose(); + + // 使用自定义颜色处理图像 + Color customColor = new Color(100, 150, 200); + BufferedImage result = ImageUtils.colorImage(image, customColor); + + // 验证处理后的像素值 + int[] pixel1 = new int[4]; + int[] pixel2 = new int[4]; + result.getRaster().getPixel(0, 0, pixel1); + result.getRaster().getPixel(1, 1, pixel2); + + // 两个像素点应该都被处理成自定义颜色 + assertEquals(customColor.getRed(), pixel1[0]); + assertEquals(customColor.getGreen(), pixel1[1]); + assertEquals(customColor.getBlue(), pixel1[2]); + + assertEquals(customColor.getRed(), pixel2[0]); + assertEquals(customColor.getGreen(), pixel2[1]); + assertEquals(customColor.getBlue(), pixel2[2]); + } +} \ No newline at end of file