Ter*_*rry 34
我已经制作了一个快速的JSfiddle表单,允许您将8位十六进制代码转换为CSS rgba值;)
https://jsfiddle.net/teddyrised/g02s07n4/embedded/result/
基础相当简单 - 将您提供的字符串拆分为2位数的部分,并执行转换为Alpha通道的百分比率,以及RGB通道的小数位数.标记如下:
<form action="">
<select id="format">
<option value="rgba">RGBa: RRGGBBAA</option>
<option value="argb">aRGB: AARRGGBB</option>
</select>
<input type="text" id="hex" value="#949494E8" />
<button>Convert</button>
</form>
<p id="rgba"></p>
Run Code Online (Sandbox Code Playgroud)
逻辑:
// Remove hash
var hex = $('#hex').val().slice(1);
// Split to four channels
var c = hex.match(/.{1,2}/g);
// Function: to decimals (for RGB)
var d = function(v) {
return parseInt(v, 16);
};
// Function: to percentage (for alpha), to 3 decimals
var p = function(v) {
return parseFloat(parseInt((parseInt(v, 16)/255)*1000)/1000);
};
// Check format: if it's argb, pop the alpha value from the end and move it to front
var a, rgb=[];
if($('#format').val() === 'argb') {
c.push(c.shift());
}
// Convert array into rgba values
a = p(c[3]);
$.each(c.slice(0,3), function(i,v) {
rgb.push(d(v));
});
Run Code Online (Sandbox Code Playgroud)
转换的要点如下:
parseInt(hexValue, 16).这是一个小工具提示:
在这种情况下#DCDCDC8F,DCis alpha= 220,
十六进制到十进制[DC]:
然后220/255 = 0.86不透明度.

这些字节按照AABBGGRR的顺序存储在little-endian机器的内存中
请查看:http://www.statman.info/conversions/hexadecimal.html
| 归档时间: |
|
| 查看次数: |
16821 次 |
| 最近记录: |