简体   繁体   English

如何使用javascript从rgb格式检索单个颜色组件(红色,蓝色,绿色)?

[英]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.

相关问题 如何从 rgb/rgba 字符串中获取红色绿色和蓝色值? - How can i get the red green and blue values from an rgb/rgba string? 如何在 RGB (javascript) 中生成所有蓝色? - how to generate all blue color in RGB (javascript)? JS:从RGB蓝色到RGB红色 - JS : from RGB Blue to RGB Red 如何填充画布中不同颜色的所有圆的样式:红色,黄色,蓝色和绿色? - How to fillStyle all circles in different color in canvas: red, yellow, blue and green? 我在 JS 中使用 function 将颜色更改为红色,然后再更改为绿色 onclick - I'm using a function in JS that changes the color to red and then to green onclick 你怎么能知道一种颜色是红色还是蓝色......有色(Javascript) - How would u be able to tell if a color is red or blue ... tinted (Javascript) 需要在我的页面上从绿色到红色单击后更改单个开/关按钮的颜色 - Need to change the color of individual on/off buttons on my page after being clicked from green to red 如果移动验证状态发生变化,如何将颜色从红色变为绿色? - How to change color from red to green if mobile verification status changes? 如何在“表百”中用蓝色为奇数编号,用红色为偶数编号 - how can I color the odd numbers with blue and even numbers with red in a “Table hundred” 如何从rgb颜色的字符串中获取颜色值? - How I can get the color value from string with rgb color?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM