本文介绍了在JSColor中,如何获取颜色的十六进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用JSColor,用户选择颜色后,如何获取十六进制"?
Using JSColor, after the user picks a color, how do I get the "hex"?
$("input#colorpicker").css('background-color') => this returns background-color: rgb(107, 132, 255);
但不是十六进制.
推荐答案
我假设jQuery.css返回设置的值.尝试使用以下功能将RGB转换为十六进制:
I assume that jQuery.css returns the value that was set.Try the following function to convert RGB to HEX:
function colorToHex(color) {
if (color.substr(0, 1) === '#') {
return color;
}
var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
var red = parseInt(digits[2]);
var green = parseInt(digits[3]);
var blue = parseInt(digits[4]);
var rgb = blue | (green << 8) | (red << 16);
return digits[1] + '#' + rgb.toString(16);
};
colorToHex('rgb(120, 120, 240)')
这篇关于在JSColor中,如何获取颜色的十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!