ColorUtil工具类:

本文介绍了一个Java实用类ColorsUtil,该类定义了一系列的颜色常量,包括各种颜色及其不同透明度和色调的变化。这些颜色常量使用ARGB格式表示,适用于图形界面开发中颜色配置的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ColorsUtil {
 
    private ColorsUtil() {
        throw new Error("Do not need instantiate!");
    }
 
    /**
     * 白色
     */
    public static final int WHITE = 0xffffffff;
 
    /**
     * 白色 - 半透明
     */
    public static final int WHITE_TRANSLUCENT = 0x80ffffff;
 
    /**
     * 黑色
     */
    public static final int BLACK = 0xff000000;
 
    /**
     * 黑色 - 半透明
     */
    public static final int BLACK_TRANSLUCENT = 0x80000000;
 
    /**
     * 透明
     */
    public static final int TRANSPARENT = 0x00000000;
 
    /**
     * 红色
     */
    public static final int RED = 0xffff0000;
 
    /**
     * 红色 - 半透明
     */
    public static final int RED_TRANSLUCENT = 0x80ff0000;
 
    /**
     * 红色 - 深的
     */
    public static final int RED_DARK = 0xff8b0000;
 
    /**
     * 红色 - 深的 - 半透明
     */
    public static final int RED_DARK_TRANSLUCENT = 0x808b0000;
 
    /**
     * 绿色
     */
    public static final int GREEN = 0xff00ff00;
 
    /**
     * 绿色 - 半透明
     */
    public static final int GREEN_TRANSLUCENT = 0x8000ff00;
 
    /**
     * 绿色 - 深的
     */
    public static final int GREEN_DARK = 0xff003300;
 
    /**
     * 绿色 - 深的 - 半透明
     */
    public static final int GREEN_DARK_TRANSLUCENT = 0x80003300;
 
    /**
     * 绿色 - 浅的
     */
    public static final int GREEN_LIGHT = 0xffccffcc;
 
    /**
     * 绿色 - 浅的 - 半透明
     */
    public static final int GREEN_LIGHT_TRANSLUCENT = 0x80ccffcc;
 
    /**
     * 蓝色
     */
    public static final int BLUE = 0xff0000ff;
 
    /**
     * 蓝色 - 半透明
     */
    public static final int BLUE_TRANSLUCENT = 0x800000ff;
 
    /**
     * 蓝色 - 深的
     */
    public static final int BLUE_DARK = 0xff00008b;
 
    /**
     * 蓝色 - 深的 - 半透明
     */
    public static final int BLUE_DARK_TRANSLUCENT = 0x8000008b;
 
    /**
     * 蓝色 - 浅的
     */
    public static final int BLUE_LIGHT = 0xff36a5E3;
 
    /**
     * 蓝色 - 浅的 - 半透明
     */
    public static final int BLUE_LIGHT_TRANSLUCENT = 0x8036a5E3;
 
    /**
     * 天蓝
     */
    public static final int SKYBLUE = 0xff87ceeb;
 
    /**
     * 天蓝 - 半透明
     */
    public static final int SKYBLUE_TRANSLUCENT = 0x8087ceeb;
 
    /**
     * 天蓝 - 深的
     */
    public static final int SKYBLUE_DARK = 0xff00bfff;
 
    /**
     * 天蓝 - 深的 - 半透明
     */
    public static final int SKYBLUE_DARK_TRANSLUCENT = 0x8000bfff;
 
    /**
     * 天蓝 - 浅的
     */
    public static final int SKYBLUE_LIGHT = 0xff87cefa;
 
    /**
     * 天蓝 - 浅的 - 半透明
     */
    public static final int SKYBLUE_LIGHT_TRANSLUCENT = 0x8087cefa;
 
    /**
     * 灰色
     */
    public static final int GRAY = 0xff969696;
 
    /**
     * 灰色 - 半透明
     */
    public static final int GRAY_TRANSLUCENT = 0x80969696;
 
    /**
     * 灰色 - 深的
     */
    public static final int GRAY_DARK = 0xffa9a9a9;
 
    /**
     * 灰色 - 深的 - 半透明
     */
    public static final int GRAY_DARK_TRANSLUCENT = 0x80a9a9a9;
 
    /**
     * 灰色 - 暗的
     */
    public static final int GRAY_DIM = 0xff696969;
 
    /**
     * 灰色 - 暗的 - 半透明
     */
    public static final int GRAY_DIM_TRANSLUCENT = 0x80696969;
 
    /**
     * 灰色 - 浅的
     */
    public static final int GRAY_LIGHT = 0xffd3d3d3;
 
    /**
     * 灰色 - 浅的 - 半透明
     */
    public static final int GRAY_LIGHT_TRANSLUCENT = 0x80d3d3d3;
 
    /**
     * 橙色
     */
    public static final int ORANGE = 0xffffa500;
 
    /**
     * 橙色 - 半透明
     */
    public static final int ORANGE_TRANSLUCENT = 0x80ffa500;
 
    /**
     * 橙色 - 深的
     */
    public static final int ORANGE_DARK = 0xffff8800;
 
    /**
     * 橙色 - 深的 - 半透明
     */
    public static final int ORANGE_DARK_TRANSLUCENT = 0x80ff8800;
 
    /**
     * 橙色 - 浅的
     */
    public static final int ORANGE_LIGHT = 0xffffbb33;
 
    /**
     * 橙色 - 浅的 - 半透明
     */
    public static final int ORANGE_LIGHT_TRANSLUCENT = 0x80ffbb33;
 
    /**
     * 金色
     */
    public static final int GOLD = 0xffffd700;
 
    /**
     * 金色 - 半透明
     */
    public static final int GOLD_TRANSLUCENT = 0x80ffd700;
 
    /**
     * 粉色
     */
    public static final int PINK = 0xffffc0cb;
 
    /**
     * 粉色 - 半透明
     */
    public static final int PINK_TRANSLUCENT = 0x80ffc0cb;
 
    /**
     * 紫红色
     */
    public static final int FUCHSIA = 0xffff00ff;
 
    /**
     * 紫红色 - 半透明
     */
    public static final int FUCHSIA_TRANSLUCENT = 0x80ff00ff;
 
    /**
     * 灰白色
     */
    public static final int GRAYWHITE = 0xfff2f2f2;
 
    /**
     * 灰白色 - 半透明
     */
    public static final int GRAYWHITE_TRANSLUCENT = 0x80f2f2f2;
 
    /**
     * 紫色
     */
    public static final int PURPLE = 0xff800080;
 
    /**
     * 紫色 - 半透明
     */
    public static final int PURPLE_TRANSLUCENT = 0x80800080;
 
    /**
     * 青色
     */
    public static final int CYAN = 0xff00ffff;
 
    /**
     * 青色 - 半透明
     */
    public static final int CYAN_TRANSLUCENT = 0x8000ffff;
 
    /**
     * 青色 - 深的
     */
    public static final int CYAN_DARK = 0xff008b8b;
 
    /**
     * 青色 - 深的 - 半透明
     */
    public static final int CYAN_DARK_TRANSLUCENT = 0x80008b8b;
 
    /**
     * 黄色
     */
    public static final int YELLOW = 0xffffff00;
 
    /**
     * 黄色 - 半透明
     */
    public static final int YELLOW_TRANSLUCENT = 0x80ffff00;
 
    /**
     * 黄色 - 浅的
     */
    public static final int YELLOW_LIGHT = 0xffffffe0;
 
    /**
     * 黄色 - 浅的 - 半透明
     */
    public static final int YELLOW_LIGHT_TRANSLUCENT = 0x80ffffe0;
 
    /**
     * 巧克力色
     */
    public static final int CHOCOLATE = 0xffd2691e;
 
    /**
     * 巧克力色 - 半透明
     */
    public static final int CHOCOLATE_TRANSLUCENT = 0x80d2691e;
 
    /**
     * 番茄色
     */
    public static final int TOMATO = 0xffff6347;
 
    /**
     * 番茄色 - 半透明
     */
    public static final int TOMATO_TRANSLUCENT = 0x80ff6347;
 
    /**
     * 橙红色
     */
    public static final int ORANGERED = 0xffff4500;
 
    /**
     * 橙红色 - 半透明
     */
    public static final int ORANGERED_TRANSLUCENT = 0x80ff4500;
 
    /**
     * 银白色
     */
    public static final int SILVER = 0xffc0c0c0;
 
    /**
     * 银白色 - 半透明
     */
    public static final int SILVER_TRANSLUCENT = 0x80c0c0c0;
 
    /**
     * 高光
     */
    public static final int HIGHLIGHT = 0x33ffffff;
 
    /**
     * 低光
     */
    public static final int LOWLIGHT = 0x33000000;
}

 

ColorUtil是一个用于处理颜色的工具类,它提供了一些常见的颜色操作方法,包括颜色转换、颜色混合、颜色亮度调整等等。 以下是ColorUtil工具类的示例代码: ```java public class ColorUtil { /** * 将RGB颜色值转换为16进制表示的字符串 * * @param color RGB颜色值 * @return 16进制表示的字符串 */ public static String toHex(int color) { String hex = Integer.toHexString(color); if (hex.length() == 8) { hex = hex.substring(2); } return "#" + hex.toUpperCase(Locale.getDefault()); } /** * 将16进制表示的颜色值转换为RGB颜色值 * * @param hex 16进制表示的颜色值 * @return RGB颜色值 */ public static int toRgb(String hex) { if (hex.startsWith("#")) { hex = hex.substring(1); } if (hex.length() == 3) { hex = hex.substring(0, 1) + hex.substring(0, 1) + hex.substring(1, 2) + hex.substring(1, 2) + hex.substring(2, 3) + hex.substring(2, 3); } if (hex.length() != 6) { throw new IllegalArgumentException("Invalid hex color: " + hex); } return Color.parseColor("#" + hex.toUpperCase(Locale.getDefault())); } /** * 将两种颜色按照指定的比例混合 * * @param color1 第一种颜色 * @param color2 第二种颜色 * @param ratio 混合比例,取值范围[0, 1] * @return 混合后的颜色 */ public static int blend(int color1, int color2, float ratio) { float r = (Color.red(color1) * ratio) + (Color.red(color2) * (1 - ratio)); float g = (Color.green(color1) * ratio) + (Color.green(color2) * (1 - ratio)); float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * (1 - ratio)); return Color.rgb((int) r, (int) g, (int) b); } /** * 调整颜色的亮度 * * @param color 要调整的颜色 * @param factor 亮度调整因子,取值范围[-1, 1] * @return 调整后的颜色 */ public static int adjustBrightness(int color, float factor) { float[] hsv = new float[3]; Color.colorToHSV(color, hsv); hsv[2] *= (1 + factor); return Color.HSVToColor(hsv); } } ``` ColorUtil工具类包含了以下方法: - **toHex(int color)**:将RGB颜色值转换为16进制表示的字符串。 - **toRgb(String hex)**:将16进制表示的颜色值转换为RGB颜色值。 - **blend(int color1, int color2, float ratio)**:将两种颜色按照指定的比例混合。 - **adjustBrightness(int color, float factor)**:调整颜色的亮度。 这些方法都是静态方法,可以直接通过类名调用。要使用ColorUtil工具类,只需要将它的代码复制到自己的项目中即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值