我假设您正在谈论对特定颜色值的单个红色、绿色和蓝色通道(我认为这就是您所说的“颜色”)执行某种数学运算。我只能猜测您想要的输入和输出值,但这是一个开始的示例,操作 RGB 空间中的值*。假设您的输入是一个十六进制数字:
var color = 0xFFD700, // "gold"
// separate the channels using bitmasks
redValue = color & 0xFF0000, // redValue is 0xFF0000
greenValue = color & 0x00FF00, // greenValue is 0x00D700
blueValue = color & 0x0000FF; // blueValue is 0x000000
// now we can manipulate each color channel independently
var lessRed = redValue - 0x010000,
moreBlue = blueValue + 0x000001,
newColor = lessRed + greenValue + moreBlue; // newColor is 0xFED701
因此,一种通过改变红色通道产生所有颜色数组的方法,保持绿色和蓝色不变:
var colors = [],
startColor = 0x00D700,
endColor = 0xFFD700,
incr = 0x010000;
while (startColor <= endColor)
{
colors.push(startColor);
startColor = startColor + incr;
}
// print the hex values
var i, len = colors.length, out = [];
for (var i=0; i<len; i++)
{
out.push('0x' + colors[i].toString(16))
}
console.log(out.join('\n'))
如果你的输入是一个字符串,你只需要先把它转换成一个数字。
var input = 'FFD700',
hexValue = parseInt(input, 16);
console.log(hexValue.toString(10)); // prints: 16766720
console.log(hexValue.toString(16)); // prints: FFD700
哦,不需要 jQuery!
* 根据this answer,RGB 空间可能不是最好的色彩空间,但根据您的问题,我认为是。