Java获取颜色方法封装

年爸 1年前 ⋅ 1475 阅读

说明:在Java开发中偶尔需要获取颜色来使用,我们的项目就是如此,今天简单整理封装了一下,给大家做个参考!

package com.xxx.project.core.utils.color;

/**
 * 颜色处理类
 *
 * @author 年爸
 * @QQ     526704425
 * @version 1.0
 * @date    2019-03-27
 */
public class Color {

    /**
     * 获取16进制颜色值
     * @param index 获取索引
     * @param filter 是否开启相同淡色过滤
     * @return 16进制颜色值
     */
    public static String getHex(int index, boolean filter){
        // 返回颜色值,默认为粉红色
        String color = "#FF69B4";
        // 验证是否开启过滤
        if(filter){ // 开启过滤
            // 验证索引与集合长度对比
            if(index < hexColorList2.length){ // 正常范围
                color = hexColorList2[index];
            }else { // 范围越界
                // 取余数验证
                int i = index % hexColorList2.length;
                color = hexColorList2[i];
            }
        }else { // 未开启
            // 验证索引与集合长度对比
            if(index < hexColorList.length){ // 正常范围
                color = hexColorList[index];
            }else { // 范围越界
                // 取余数验证
                int i = index % hexColorList.length;
                color = hexColorList[i];
            }
        }
        return color;
    }

    /**
     * 获取红绿兰颜色表示值
     * @param index 获取索引
     * @param filter 是否开启相同淡色过滤
     * @return 红绿兰颜色表示值
     */
    public static String getRgb(int index, boolean filter){
        // 返回颜色值,默认为粉红色
        String color = "255,105,180";
        // 验证是否开启过滤
        if(filter){ // 开启过滤
            // 验证索引与集合长度对比
            if(index < rgbColorList2.length){ // 正常范围
                color = rgbColorList2[index];
            }else { // 范围越界
                // 取余数验证
                int i = index % rgbColorList2.length;
                color = rgbColorList2[i];
            }
        }else { // 未开启
            // 验证索引与集合长度对比
            if(index < rgbColorList.length){ // 正常范围
                color = rgbColorList[index];
            }else { // 范围越界
                // 取余数验证
                int i = index % rgbColorList.length;
                color = rgbColorList[i];
            }
        }
        return color;
    }

    // HEX颜色集合定义(较全)
    private static String[] hexColorList = new String[]{
        "#FFB6C1",       //浅粉色
        "#FFC0CB",		 //粉红
        "#DC143C",		 //猩红
        "#FFF0F5",		 //脸红的淡紫色
        "#DB7093",		 //苍白的紫罗兰红色
        "#FF69B4",		 //热情的粉红
        "#FF1493",		 //深粉色
        "#C71585",		 //适中的紫罗兰红色
        "#DA70D6",		 //兰花的紫色
        "#D8BFD8",		 //蓟
        "#DDA0DD",		 //李子
        "#EE82EE",		 //紫罗兰
        "#FF00FF",		 //洋红
        "#FF00FF",		 //灯笼海棠(紫红色)
        "#8B008B",		 //深洋红色
        "#800080",		 //紫色
        "#BA55D3",		 //适中的兰花紫
        "#9400D3",		 //深紫罗兰色
        "#9932CC",		 //深兰花紫
        "#4B0082",		 //靛青
        "#8A2BE2",		 //深紫罗兰的蓝色
        "#9370DB",		 //适中的紫色
        "#7B68EE",		 //适中的板岩暗蓝灰色
        "#6A5ACD",		 //板岩暗蓝灰色
        "#483D8B",		 //深岩暗蓝灰色
        "#E6E6FA",		 //薰衣草花的淡紫色
        "#F8F8FF",		 //幽灵的白色
        "#0000FF",		 //纯蓝
        "#0000CD",		 //适中的蓝色
        "#191970",		 //午夜的蓝色
        "#00008B",		 //深蓝色
        "#000080",		 //海军蓝
        "#4169E1",		 //皇军蓝
        "#6495ED",		 //矢车菊的蓝色
        "#B0C4DE",		 //淡钢蓝
        "#778899",		 //浅石板灰
        "#708090",		 //石板灰
        "#1E90FF",		 //道奇蓝
        "#F0F8FF",		 //爱丽丝蓝
        "#4682B4",		 //钢蓝
        "#87CEFA",		 //淡蓝色
        "#87CEEB",		 //天蓝色
        "#00BFFF",		 //深天蓝
        "#ADD8E6",		 //淡蓝
        "#B0E0E6",		 //火药蓝
        "#5F9EA0",		 //军校蓝
        "#F0FFFF",		 //蔚蓝色
        "#E1FFFF",		 //淡青色
        "#AFEEEE",		 //苍白的绿宝石
        "#00FFFF",		 //青色
        "#00FFFF",		 //水绿色
        "#00CED1",		 //深绿宝石
        "#2F4F4F",		 //深石板灰
        "#008B8B",		 //深青色
        "#008080",		 //水鸭色
        "#48D1CC",		 //适中的绿宝石
        "#20B2AA",		 //浅海洋绿
        "#40E0D0",		 //绿宝石
        "#7FFFAA",		 //绿玉碧绿色
        "#00FA9A",		 //适中的碧绿色
        "#F5FFFA",		 //适中的春天的绿色
        "#00FF7F",		 //薄荷奶油
        "#3CB371",		 //春天的绿色
        "#2E8B57",		 //海洋绿
        "#F0FFF0",		 //蜂蜜
        "#90EE90",		 //淡绿色
        "#98FB98",		 //苍白的绿色
        "#8FBC8F",		 //深海洋绿
        "#32CD32",		 //酸橙绿
        "#00FF00",		 //酸橙色
        "#228B22",		 //森林绿
        "#008000",		 //纯绿
        "#006400",		 //深绿色
        "#7FFF00",		 //查特酒绿
        "#7CFC00",		 //草坪绿
        "#ADFF2F",		 //绿黄色
        "#556B2F",		 //橄榄土褐色
        "#6B8E23",		 //米色(浅褐色)
        "#FAFAD2",		 //浅秋麒麟黄
        "#FFFFF0",		 //象牙
        "#FFFFE0",		 //浅黄色
        "#FFFF00",		 //纯黄
        "#808000",		 //橄榄
        "#BDB76B",		 //深卡其布
        "#FFFACD",		 //柠檬薄纱
        "#EEE8AA",		 //灰秋麒麟
        "#F0E68C",		 //卡其布
        "#FFD700",		 //金
        "#FFF8DC",		 //玉米色
        "#DAA520",		 //秋麒麟
        "#FFFAF0",		 //花的白色
        "#FDF5E6",		 //老饰带
        "#F5DEB3",		 //小麦色
        "#FFE4B5",		 //鹿皮鞋
        "#FFA500",		 //橙色
        "#FFEFD5",		 //番木瓜
        "#FFEBCD",		 //漂白的杏仁
        "#FFDEAD",		 //Navajo白
        "#FAEBD7",		 //古代的白色
        "#D2B48C",		 //晒黑
        "#DEB887",		 //结实的树
        "#FFE4C4",		 //(浓汤)乳脂,番茄等
        "#FF8C00",		 //深橙色
        "#FAF0E6",		 //亚麻布
        "#CD853F",		 //秘鲁
        "#FFDAB9",		 //桃色
        "#F4A460",		 //沙棕色
        "#D2691E",		 //巧克力
        "#8B4513",		 //马鞍棕色
        "#FFF5EE",		 //海贝壳
        "#A0522D",		 //黄土赭色
        "#FFA07A",		 //浅鲜肉(鲑鱼)色
        "#FF7F50",		 //珊瑚
        "#FF4500",		 //橙红色
        "#E9967A",		 //深鲜肉(鲑鱼)色
        "#FF6347",		 //番茄
        "#FFE4E1",		 //薄雾玫瑰
        "#FA8072",		 //鲜肉(鲑鱼)色
        "#FFFAFA",		 //雪
        "#F08080",		 //淡珊瑚色
        "#BC8F8F",		 //玫瑰棕色
        "#CD5C5C",		 //印度红
        "#FF0000",		 //纯红
        "#A52A2A",		 //棕色
        "#B22222",		 //耐火砖
        "#8B0000",		 //深红色
        "#800000",		 //栗色
        "#FFFFFF",		 //纯白
        "#F5F5F5",		 //白烟
        "#DCDCDC",		 //Gainsboro
        "#D3D3D3",		 //浅灰色
        "#C0C0C0",		 //银白色
        "#A9A9A9",		 //深灰色
        "#808080",		 //灰色
        "#696969",		 //暗淡的灰色
        "#000000",		 //纯黑
    };

    // HEX颜色集合定义(少:去掉了淡色)
    private static String[] hexColorList2 = new String[]{
            "#DC143C",       //猩红
            "#FF1493",       //深粉红
            "#C71585",       //中紫罗兰红
            "#FF00FF",       //洋红(玫瑰红)
            "#8B008B",       //深洋红
            "#9400D3",       //暗紫罗兰
            "#4B0082",       //靛青/紫兰色
            "#483D8B",       //暗灰蓝色(暗板岩蓝)
            "#0000FF",       //纯蓝
            "#000080",       //海军蓝
            "#2F4F4F",       //暗瓦灰色(暗石板灰)
            "#008080",       //水鸭色
            "#2E8B57",       //海洋绿
            "#006400",       //暗绿色
            "#556B2F",       //暗橄榄绿
            "#B8860B",       //暗金菊黄
            "#FF8C00",       //深橙色
            "#8B4513",       //重褐色(马鞍棕色)
            "#FF4500",       //橙红色
            "#FF0000",       //纯红
            "#696969",       //暗淡的灰色
            "#800000",       //栗色
            "#A52A2A",       //棕色
            "#000000",       //纯黑
            "#A0522D",       //黄土赭色
            "#808000",       //橄榄
            "#008B8B",       //暗青色
            "#20B2AA",       //浅海洋绿
            "#1E90FF",       //闪兰色(道奇蓝)
            "#4682B4",       //钢蓝/铁青
    };

    // rgb颜色集合定义(较全)
    private static String[] rgbColorList = new String[]{
        "255,182,193",       //浅粉色
        "255,192,203",		 //粉红
        "220,20,60",		 //猩红
        "255,240,245",		 //脸红的淡紫色
        "219,112,147",		 //苍白的紫罗兰红色
        "255,105,180",		 //热情的粉红
        "255,20,147",		 //深粉色
        "199,21,133",		 //适中的紫罗兰红色
        "218,112,214",		 //兰花的紫色
        "216,191,216",		 //蓟
        "221,160,221",		 //李子
        "238,130,238",		 //紫罗兰
        "255,0,255",		 //洋红
        "255,0,255",		 //灯笼海棠(紫红色)
        "139,0,139",		 //深洋红色
        "128,0,128",		 //紫色
        "186,85,211",		 //适中的兰花紫
        "148,0,211",		 //深紫罗兰色
        "153,50,204",		 //深兰花紫
        "75,0,130",		     //靛青
        "138,43,226",		 //深紫罗兰的蓝色
        "147,112,219",		 //适中的紫色
        "123,104,238",		 //适中的板岩暗蓝灰色
        "106,90,205",		 //板岩暗蓝灰色
        "72,61,139",		 //深岩暗蓝灰色
        "230,230,250",		 //薰衣草花的淡紫色
        "248,248,255",		 //幽灵的白色
        "0,0,255",		     //纯蓝
        "0,0,205",		     //适中的蓝色
        "25,25,112",		 //午夜的蓝色
        "0,0,139",		     //深蓝色
        "0,0,128",		     //海军蓝
        "65,105,225",		 //皇军蓝
        "100,149,237",		 //矢车菊的蓝色
        "176,196,222",		 //淡钢蓝
        "119,136,153",		 //浅石板灰
        "112,128,144",		 //石板灰
        "30,144,255",		 //道奇蓝
        "240,248,255",		 //爱丽丝蓝
        "70,130,180",		 //钢蓝
        "135,206,250",		 //淡蓝色
        "135,206,235",		 //天蓝色
        "0,191,255",		 //深天蓝
        "173,216,230",		 //淡蓝
        "176,224,230",		 //火药蓝
        "95,158,160",		 //军校蓝
        "240,255,255",		 //蔚蓝色
        "225,255,255",		 //淡青色
        "175,238,238",		 //苍白的绿宝石
        "0,255,255",		 //青色
        "0,255,255",		 //水绿色
        "0,206,209",		 //深绿宝石
        "47,79,79",		     //深石板灰
        "0,139,139",		 //深青色
        "0,128,128",		 //水鸭色
        "72,209,204",		 //适中的绿宝石
        "32,178,170",		 //浅海洋绿
        "64,224,208",		 //绿宝石
        "127,255,170",		 //绿玉碧绿色
        "0,250,154",		 //适中的碧绿色
        "245,255,250",		 //适中的春天的绿色
        "0,255,127",		 //薄荷奶油
        "60,179,113",		 //春天的绿色
        "46,139,87",		 //海洋绿
        "240,255,240",		 //蜂蜜
        "144,238,144",		 //淡绿色
        "152,251,152",		 //苍白的绿色
        "143,188,143",		 //深海洋绿
        "50,205,50",		 //酸橙绿
        "0,255,0",		     //酸橙色
        "34,139,34",		 //森林绿
        "0,128,0",		     //纯绿
        "0,100,0",		     //深绿色
        "127,255,0",		 //查特酒绿
        "124,252,0",		 //草坪绿
        "173,255,47",		 //绿黄色
        "85,107,47",		 //橄榄土褐色
        "107,142,35",		 //米色(浅褐色)
        "250,250,210",		 //浅秋麒麟黄
        "255,255,240",		 //象牙
        "255,255,224",		 //浅黄色
        "255,255,0",		 //纯黄
        "128,128,0",		 //橄榄
        "189,183,107",		 //深卡其布
        "255,250,205",		 //柠檬薄纱
        "238,232,170",		 //灰秋麒麟
        "240,230,140",		 //卡其布
        "255,215,0",		 //金
        "255,248,220",		 //玉米色
        "218,165,32",		 //秋麒麟
        "255,250,240",		 //花的白色
        "253,245,230",		 //老饰带
        "245,222,179",		 //小麦色
        "255,228,181",		 //鹿皮鞋
        "255,165,0",		 //橙色
        "255,239,213",		 //番木瓜
        "255,235,205",		 //漂白的杏仁
        "255,222,173",		 //Navajo白
        "250,235,215",		 //古代的白色
        "210,180,140",		 //晒黑
        "222,184,135",		 //结实的树
        "255,228,196",		 //(浓汤)乳脂,番茄等
        "255,140,0",		 //深橙色
        "250,240,230",		 //亚麻布
        "205,133,63",		 //秘鲁
        "255,218,185",		 //桃色
        "244,164,96",		 //沙棕色
        "210,105,30",		 //巧克力
        "139,69,19",		 //马鞍棕色
        "255,245,238",		 //海贝壳
        "160,82,45",		 //黄土赭色
        "255,160,122",		 //浅鲜肉(鲑鱼)色
        "255,127,80",		 //珊瑚
        "255,69,0",		     //橙红色
        "233,150,122",		 //深鲜肉(鲑鱼)色
        "255,99,71",		 //番茄
        "255,228,225",		 //薄雾玫瑰
        "250,128,114",		 //鲜肉(鲑鱼)色
        "255,250,250",		 //雪
        "240,128,128",		 //淡珊瑚色
        "188,143,143",		 //玫瑰棕色
        "205,92,92",		 //印度红
        "255,0,0",		     //纯红
        "165,42,42",		 //棕色
        "178,34,34",		 //耐火砖
        "139,0,0",		     //深红色
        "128,0,0",		     //栗色
        "255,255,255",		 //纯白
        "245,245,245",		 //白烟
        "220,220,220",		 //Gainsboro
        "211,211,211",		 //浅灰色
        "192,192,192",		 //银白色
        "169,169,169",		 //深灰色
        "128,128,128",		 //灰色
        "105,105,105",		 //暗淡的灰色
        "0,0,0",		     //纯黑
    };

    // rgb颜色集合定义(少:去掉了淡色)
    private static String[] rgbColorList2 = new String[]{
        "220,20,60",         //猩红
        "255,105,180",       //热情的粉红
        "218,112,214",       //兰花的紫色
        "75,0,130",          //靛青
        "0,0,255",           //纯蓝
        "112,128,144",       //石板灰
        "72,209,204",        //适中的绿宝石
        "240,255,240",       //蜂蜜
        "124,252,0",         //草坪绿
        "85,107,47",         //橄榄土褐色
        "250,250,210",       //浅秋麒麟黄
        "255,255,0",         //纯黄
        "255,215,0",         //金
        "255,165,0",         //橙色
        "255,239,213",       //番木瓜
        "210,180,140",       //晒黑
        "255,140,0",         //深橙色
        "255,218,185",       //桃色
        "210,105,30",        //巧克力
        "160,82,45",         //黄土赭色
        "255,69,0",          //橙红色
        "255,228,225",       //薄雾玫瑰
        "188,143,143",       //玫瑰棕色
        "255,0,0",	         //纯红
        "165,42,42",	     //棕色
        "128,0,0",	         //栗色
        "128,128,128",	     //灰色
        "0,0,0",	         //纯黑
        "176,224,230",	     //火药蓝
        "0,255,0",	         //酸橙色
    };
}

全部评论: 0

    我有话说: