各种颜色值的表示方法以及它们之间如何相互转换

1. 十六进制:

 #ff4751

2. int 常量型:

 private static final int color_text = 0xff4751;

3. 通过xml文件获取id:

  <color name="test_color">#ff4751</color>  
  ContextCompat.getColor(this,R.color.test_color);

4. RGB表示:

2557181

5. 0-1浮点表示:

1.00.27843140.31764707

十进制转十六进制

private String change10To16(int color) {
        StringBuffer stringBuffer = new StringBuffer();

        int red = (color & 0xff0000) >> 16;
        int green = (color & 0x00ff00) >> 8;
        int blue = (color & 0x0000ff);

        String str_red = Integer.toHexString(red);
        str_red = str_red.length() < 2 ? ("0" + str_red) : str_red;

        String str_green = Integer.toHexString(green);
        str_green = str_green.length() < 2 ? ("0" + str_green) : str_green;

        String str_blue = Integer.toHexString(blue);
        str_blue = str_blue.length() < 2 ? ("0" + str_blue) : str_blue;

        stringBuffer.append("#");
        stringBuffer.append(str_red);
        stringBuffer.append(str_green);
        stringBuffer.append(str_blue);

        return stringBuffer.toString();
    }

十进制转RGB

    private float[] change10ToRGB(int color) {

        int red = (color & 0xff0000) >> 16;
        int green = (color & 0x00ff00) >> 8;
        int blue = (color & 0x0000ff);

        float colors[] = new float[3];
        colors[0] = red;
        colors[1] = green;
        colors[2] = blue;

        return colors;
    }

十进制转浮点

    private float[] change10ToFloat(int color) {

        int red = (color & 0xff0000) >> 16;
        int green = (color & 0x00ff00) >> 8;
        int blue = (color & 0x0000ff);

        float colors[] = new float[3];
        colors[0] = red/255f;
        colors[1] = green/255f;
        colors[2] = blue/255f;

        return colors;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤独的冥王星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值