[英]How can i retrieve individual color components(red,blue,green) from the rgb format using javascript?
$('#selector').css('background-color'); $( '#选择器')的CSS( '背景颜色');
This returns the background color as rgb(xxx,yyy,zzz) 这将返回背景颜色为rgb(xxx,yyy,zzz)
How to get individual color components(red, green, blue) in DECIMAL from this string? 如何从这个字符串中获取DECIMAL中的单个颜色组件(红色,绿色,蓝色)?
The solution: 解决方案:
function icolor(rgb){
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
var red=parseInt(rgb[1]);
var green=parseInt(rgb[2]);
var blue=parseInt(rgb[3]);
} }
What about getting all numbers, converting them to Hex: 如何获取所有数字,将它们转换为Hex:
function convert(value){
return(value.match(/\d+/g)).map(function(o){
var val=(o*1).toString(16);
val=(val.length>1)?val:"0"+val;
return val;
}).join("")
}
Here is the fiddle to play with. 这是小提琴。
with my favourite color: BADA55 ;) 用我最喜欢的颜色:BADA55;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.