package com.version2;

/**
 * @author xu
 * @create 2019-07-12 7:55
 */
/**
 * 将rgb值装换为16进制值,
 * 将16进制转为rgb
 */
public class ColorToHex {

    public static String toHex(int r, int g, int b) {
        return "#" + toBrowserHexValue(r) + toBrowserHexValue(g)
                + toBrowserHexValue(b);
    }

    private static String toBrowserHexValue(int rgb) {
        StringBuilder sb = new StringBuilder(
                Integer.toHexString(rgb & 0xff));
        //为什么要和0xff做&运算?保持二进制补码的一致性
        while (sb.length() < 2) {
            sb.append("0");
        }
        return sb.toString().toUpperCase();
    }

    public static String toInt(String hex){
        //要头不要尾
        String r = rgbToNumber(hex.substring(0,2));
        String g = rgbToNumber(hex.substring(2,4));
        String b = rgbToNumber(hex.substring(4,6));

        return "["+r+","+g+","+b+"]";
    }
    private static String rgbToNumber(String str){
        return ""+Integer.parseUnsignedInt(str,16);
    }

    public static void main(String[] args) {
        System.out.println(toHex(255, 0, 0));
        System.out.println(Integer.parseUnsignedInt("a8",16));
    }
}

最近下载更多
CxlyboSoft  LV6 2020年2月16日
moyec50  LV1 2019年7月30日
hsl2019  LV3 2019年7月25日
旧梦圆  LV1 2019年7月24日
Demons_Robin  LV7 2019年7月23日
谭鬼鬼  LV48 2019年7月21日
最代码官方  LV167 2019年7月15日
最近浏览更多
jkjfdgbkl  LV2 2023年11月2日
暂无贡献等级
猪皮怪 2022年6月12日
暂无贡献等级
wubinbin  LV11 2022年2月13日
shisun  LV3 2021年7月5日
大连有个马猴  LV4 2020年12月19日
jpfjpfjpf  LV1 2020年12月12日
段朝洪  LV15 2020年11月21日
jeed141305  LV1 2020年7月4日
Andy0211 2020年7月1日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友