views:

232

answers:

2

how can i create an opposite of a hexadecimal color? for example, i'd like to convert 0x000000 (black) into 0xFFFFFF (white), or 0xFF0000 (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.

+6  A: 

Just do 0xFFFFFF - COLOR.

Spidey
now i'm glad i didn't create a loop function. such an obvious solution you've pointed out. how embarrassing! :)
TheDarkInI1978
+4  A: 

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');
Graphain