如何获得十六进制颜色值而不是 RGB 值? [英] How to get hex color value rather than RGB value?

查看:36
本文介绍了如何获得十六进制颜色值而不是 RGB 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下 jQuery 将获得元素背景颜色的 RGB 值:

Using the following jQuery will get the RGB value of an element's background color:

$('#selector').css('backgroundColor');

有没有办法获得十六进制值而不是 RGB?

Is there a way to get the hex value rather than the RGB?

推荐答案

var hexDigits = new Array
        ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"); 

//Function to convert rgb color to hex format
function rgb2hex(rgb) {
 rgb = rgb.match(/^rgb((d+),s*(d+),s*(d+))$/);
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function hex(x) {
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
 }

(来源)

这篇关于如何获得十六进制颜色值而不是 RGB 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆