开发者

Formula For calculating opposite ("Difference") for hexadecimal color

How can I create the opposite of a hexadecimal color? For example, I'd like to convert 0x000000 (black) into 0xFFFFFF (white), or 0xFF开发者_如何学编程0000 (red) into 0x00FFFF (cyan). Those are rather basic colors, while variants of colors can have more complex hexadecimal values, such as 0x21B813 (greenish).

Are bitwise operators required for this? Maybe a loop of each digit to calculate it's mirror from 0 to 15, or 0 to F (0 become F, 6 becomes 9, etc.)

I'm using ActionScript, so I'm almost certain that this would be done the same way in Java.


As Spidey says just use 0xFFFFFF - COLOR.

In ActionScript you would do something like:

public static function pad(str:String, minLength:uint, pad:String):String { 
    while (str.length < minLength) str = pad + str; 
    return str; 
} 

var color:Number=0x002233;
var hexColorStr:String = "#" + pad((0xFFFFFF-color).toString(16), 6, "0");

In Java:

int color = 0x002233;
String hex = String.format("06X", (0xFFFFFF - color)); 

In C#:

int color = 0x002233;
string hex = (0xFFFFFF - color).ToString("X").PadLeft(6, '0');


Just do 0xFFFFFF - COLOR.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜