You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.3 KiB
89 lines
2.3 KiB
package com.fr.widgettheme.theme.panel; |
|
|
|
import javax.swing.ImageIcon; |
|
import java.awt.Color; |
|
import java.awt.Graphics; |
|
import java.awt.image.BufferedImage; |
|
import java.awt.image.WritableRaster; |
|
|
|
/** |
|
* 控件主题Image处理工具类 |
|
* |
|
* @author obo |
|
* @since 11.0 |
|
* Created on 2023/11/13 |
|
*/ |
|
public class ImageUtils { |
|
|
|
/** |
|
* 根据主题色处理image |
|
* |
|
* @param image 图像 |
|
* @param color 主题色 |
|
* @return 处理结果 |
|
*/ |
|
public static BufferedImage colorImage(BufferedImage image, Color color) { |
|
int width = image.getWidth(); |
|
int height = image.getHeight(); |
|
WritableRaster raster = image.getRaster(); |
|
|
|
for (int xx = 0; xx < width; xx++) { |
|
for (int yy = 0; yy < height; yy++) { |
|
int[] pixels = raster.getPixel(xx, yy, (int[]) null); |
|
setRedPixels(pixels, color); |
|
setGreenPixels(pixels, color); |
|
setBluePixels(pixels, color); |
|
raster.setPixel(xx, yy, pixels); |
|
} |
|
} |
|
return image; |
|
} |
|
|
|
/** |
|
* 处理红像素 |
|
* |
|
* @param pixels 像素数组 |
|
* @param color 颜色 |
|
*/ |
|
private static void setRedPixels(int[] pixels, Color color) { |
|
pixels[0] = pixels[0] > 0 && pixels[0] < 255 ? color.getRed() : 255; |
|
} |
|
|
|
/** |
|
* 处理绿像素 |
|
* |
|
* @param pixels 像素数组 |
|
* @param color 颜色 |
|
*/ |
|
private static void setGreenPixels(int[] pixels, Color color) { |
|
pixels[1] = pixels[1] > 0 && pixels[1] < 255 ? color.getGreen() : 255; |
|
} |
|
|
|
/** |
|
* 处理蓝像素 |
|
* |
|
* @param pixels 像素数组 |
|
* @param color 颜色 |
|
*/ |
|
private static void setBluePixels(int[] pixels, Color color) { |
|
pixels[2] = pixels[2] > 0 && pixels[2] < 255 ? color.getBlue() : 255; |
|
} |
|
|
|
/** |
|
* ImageIcon转换为BufferImage |
|
* |
|
* @param icon imageIcon |
|
* @return BufferedImage |
|
*/ |
|
public static BufferedImage imageIconToBufferedImage(ImageIcon icon) { |
|
BufferedImage bi = new BufferedImage( |
|
icon.getIconWidth(), |
|
icon.getIconHeight(), |
|
BufferedImage.TYPE_INT_ARGB); |
|
Graphics g = bi.createGraphics(); |
|
icon.paintIcon(null, g, 0, 0); |
|
g.dispose(); |
|
return bi; |
|
} |
|
|
|
}
|
|
|