十六进制颜色值与rgba格式的互相转换

package com.utils.color;
/**
 *项目名称:Java_Utils
 *类名称:ColorUtil
 *类描述: 颜色格式转换类
 *创建人:林剑斌
 *创建时间:2017-09-17上午12:43:51
 *@version 1.0
 */
public class ColorUtil {


/**
* <p>功能描述: 十六进制的颜色值转换成rgb格式</p>
* <p>输入参数: </p>
* <p>返回结果: </p>
* <p>时间:2017-09-17上午12:44:39</p>  
* @author 林剑斌
* @version 1.0
*/
public static String getRGBA(String color,String transparency){//transparency 代表透明度
int uColor=Integer.parseInt(color,16);
int r = (uColor >> 16) & 0xFF;
int g = (uColor >> 8) & 0xFF;
int b = (uColor) & 0xFF;
String rgba="rgba("+r+","+g+","+b+","+transparency+")";
return rgba;
}

/**
* <p>功能描述: rgb格式的颜色变为十六进制颜色代码</p>
* <p>输入参数:1、 color的值样式为rgba(245,39,60,0.7) a为透明度</p>
* <p>2、color的值样式为rgba(245,39,60)</p>
* <p>返回结果: </p>
* <p>时间:2017-09-17上午12:50:40</p>  
* @author 林剑斌
* @version 1.0
*/
public static String getColor16Code(String color){

color=color.substring(5, color.length()-1);//
String[] uColor=color.split(",");
int r=Integer.parseInt(uColor[0]);
int g=Integer.parseInt(uColor[1]);
int b=Integer.parseInt(uColor[2]);
Double a=1.0;
if(color.split(",").length==4){
a=Double.parseDouble(uColor[3]);
}
String result=Integer.toHexString(r<<16|g<<8|b).toString();
return result;
}


}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值