Complementary colors
Pyth, 9 8 bytes
Thanks to @isaacg for -1 byte!
sXz.HM16
Subtracting a certain color's value from 255 is equivalent to subtracting each of its hexadecimal digits from 15. Say a number is 16a+b. Then the value of the number created by subtracting its digits from 15 is 16(15-a) + (15-b) = 255 - (16a+b).
sXz.HM16 implicit: z=input()
16
.HM map hex representation over range
.HM16 '0123456789abcdef'
z the input string
X Translate characters in x1 present in x2 to reversed x2
that is, '0' becomes f, '1' becomes 'e', and so on.
The initial '#' is unchanged.
s That produced a list, so join into a string by reducing +
Try it here. Test suite.
Retina, 13 10 bytes
T`w`G-A9-0
There are three parts to the code, separated by backticks (`
): T
specifies transliterate mode, which replaces each character in the second part with its corresponding character in the third part.
w
is the same as traditional regex's \w
, or _0-9A-Za-z
, which is expanded to _0123456789ABCDEFGH...
.
The second part is expanded to GFEDCBA9876543210
, thanks to Retina's nifty ability to expand in reverse order. Put these on top of each other, and we get:
_0123456789ABCDEFGH...
GFEDCBA987654321000...
^^^^^^^^^^^^^^^^
Note that the last character, 0
, is repeated to fit the length of the longer string, but we only care about the hexadecimal characters, shown by carets.
Thanks to Martin Büttner for suggesting this approach.
Try the test suite online.
JavaScript ES6, 61 bytes 66 68 48 53 64
Saves quite a few bytes thanks to @Cᴏɴᴏʀ O'Bʀɪᴇɴ, @NinjaBearMonkey, and @nderscore
s=>"#"+(1e5+(8**8+~('0x'+s.slice(1))).toString(16)).slice(-6)
Takes advantage of auto-type-casting. Fixing the zeros killed the byte count