繁体   English   中英

如何从另一个中减去颜色

[英]How to subtract a color from another

是否可以从另一个颜色中减去一种颜色?

示例(如果我错了,请纠正我)

如果我从白色中减去红色绿色 ,我希望结果是蓝色的

var white = 0xFFFFFF,
    red = 0xFF0000,
    result = white - red;
console.log(result); //65535 <-- what is that ? can it be converted to 0x00FFFF ?

[更新]

感谢Rocket的回答 ,结果我需要一个function()来将我的结果转换为实际颜色。

这是最后的工作示例:

var toColor = function ( d ) {
       var c = Number(d).toString(16);
       return "#" + ( "000000".substr( 0, 6 - c.length ) + c.toUpperCase() );
    },
    white = 0xFFFFFF,
    red = 0xFF0000,
    green = 0x00FF00,
    result = toColor( white - red - green );

console.log( result ); // logs the expected result: "#0000FF"

你的white-red工作正常,只是JavaScript将值表示为基数为10的值。 您需要将它们转换回基数16(十六进制)。 看看这个答案 ,将值转换回十六进制。

var white = 0xFFFFFF, // Stored as 16777215
    red = 0xFF0000, // Stored as 16711680
    result = white - red; // 16777215 - 16711680 = 65535
console.log(result); // It's stored as base 10, so it prints 65535
var resultHex = result.toString(16); // 'ffff', converted back to hex

我只是假设你正在使用RGB,因为有很多其他方法可以混合颜色。 您必须将颜色表示为3 *个不同的部分,RG和B.

//            R  G  B 
var white = [ 1, 1, 1]; 
var red =   [ 0, 0, 0 ];
var result = [ white[0] - red[0], white[1] - red[1], white[2] - red[2] ;

要减去颜色,您必须减去颜色的每个分量,并可能稍后将其转换回十六进制。

*您可能希望稍后添加Alpha

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM