嘿,我想知道是否有人知道如何根据以十六进制输入的单一颜色生成一些单色的颜色到javascript函数中。
答案 0 :(得分:3)
如果您想将颜色转换为某种程度的灰度,请尝试计算发光,如下所示:
function toGrayScale(color) /* color is an integer */ {
// Extract the red, green, and blue portions of the color
var red = (color >> 16) & 0xff;
var green = (color >> 8) & 0xff;
var blue = color & 0xff;
// Calculate the luminescence
var luminescence = red * 0.3 + green * 0.59 + blue * 0.11;
var lumInt = Math.floor(luminescence);
// Combine into a grayscale color
return (lumInt << 16) + (lumInt << 8) + lumInt;
}
答案 1 :(得分:2)
你想要做的是计算每个颜色分量相对于平均发光的相对值,然后将每个颜色分量乘以一对偏移以产生不同的亮度......
这是伪代码,我相信你可以弄清楚细节:
function getColors(baseline) {
var offsets = [ 0x33, 0x66, 0x99, 0xCC ];
// Use Jake's suggestion on computing luminescence...
var lum = getLuminescence(baseline);
var redCoefficient = baseline[red] / lum;
var greenCoefficient = baseline[green] / lum;
var blueCoefficient = baseline[blue] / lum;
var output = [];
for (offsetInd in offsets) {
var offset = offsets[offsetInd];
output.push(new Color(offset * redCoefficient,
offset * greenCofficient, offset * blueCoefficient));
}
return ouptut;
}
答案 2 :(得分:1)
尝试:
// color looks like 0xRRGGBB
function(color){
R = color.substr(2, 2)
G = color.substr(4, 2)
B = color.substr(6, 2)
return "0x" + R + R + R
// or return ["0x"+R+R+R, "0x"+G+G+G, "0x"+B+B+B]
}
顺便说一下:“单色”是矛盾的,不是吗? ; - )