obo
2 years ago
2 changed files with 39 additions and 77 deletions
@ -1,67 +0,0 @@
|
||||
package com.fr.design.utils; |
||||
|
||||
import javax.swing.Icon; |
||||
import javax.swing.ImageIcon; |
||||
import java.awt.Color; |
||||
import java.awt.Graphics2D; |
||||
import java.awt.Image; |
||||
import java.awt.image.BufferedImage; |
||||
|
||||
|
||||
/** |
||||
* 对Icon进行颜色处理的工具类 |
||||
* |
||||
* @author obo |
||||
* @since 11.0 |
||||
* Created on 2023/4/3 |
||||
*/ |
||||
public final class IconColorUtils { |
||||
|
||||
/** |
||||
* 转色处理并返回处理后的Icon |
||||
* |
||||
* @param icon 需要转色处理的icon |
||||
* @param color 需要转的颜色,可能要和视觉沟通 |
||||
* @return 转色后的icon |
||||
*/ |
||||
public static Icon convert(Icon icon, Color color) { |
||||
BufferedImage image = iconToImage(icon); |
||||
Image convertedImage = createConvertedImage(image, color); |
||||
return new ImageIcon(convertedImage); |
||||
} |
||||
|
||||
/** |
||||
* 获取icon中的image对象,虽然设计器中未来大部分图标会读取为SVGIcon类型,其中直接包含image对象 |
||||
* 但存在某些特殊情况,例如绘制带警告图标的数据集时,无法直接获取image对象,因此统一使用此方法获取 |
||||
* |
||||
* @param icon 需要获取image的icon |
||||
* @return 获取icon对应的image |
||||
*/ |
||||
private static BufferedImage iconToImage(Icon icon) { |
||||
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); |
||||
Graphics2D g2d = image.createGraphics(); |
||||
icon.paintIcon(null, g2d, 0, 0); |
||||
g2d.dispose(); |
||||
return image; |
||||
} |
||||
|
||||
/** |
||||
* 转色 |
||||
* |
||||
* @param image 需要转色的image |
||||
* @param color 需要转的颜色,可能要和视觉沟通 |
||||
* @return 转色后的image |
||||
*/ |
||||
private static Image createConvertedImage(BufferedImage image, Color color) { |
||||
int colorRgb = (color.getRGB() & 0x00FFFFFF); |
||||
for (int x = 0; x < image.getWidth(); x++) { |
||||
for (int y = 0; y < image.getHeight(); y++) { |
||||
int originalArgb = image.getRGB(x, y); |
||||
int alpha = (originalArgb >> 24) & 0xFF; |
||||
int newArgb = (alpha << 24) | colorRgb; |
||||
image.setRGB(x, y, newArgb); |
||||
} |
||||
} |
||||
return image; |
||||
} |
||||
} |
Loading…
Reference in new issue