You can use color codes in the Minecraft chat. I tried to create a parser in 140byt.es that converts a Minecraft chat line to HTML. I found two possible solutions, both 150 bytes. One with a lookup table and another with an algorithmic aproach (which I think is very cool).
Here are some things I learned while golfing this down:
- Do you know
String.slice()
is the newString.substring()
? It's shorter and more reliable. Be aware of the few different behaviors. - To convert a hexadecimal string to a number (“hex2dec”) you can use
parseInt('FF', 16)
oreval('0x' + 'FF')
or('0x' + 'FF') * 1
or'0x' + 'FF' | 0
. Take care of the operator precedence. - The eight main colors are ordered like they are binary numbers: 0002, 0012, 0102, 0112, 1002, 1012, 1102, 1112. Thats why I can do this: 616 → 1102 → convert to a binary and parse as a hexadecimal string → 11016 → multiply with A16 → AA016 → add 55516 → FF516.
- The most reliable solution would be to use
/&[\dA-F].*/i
in a loop. Solutions with[^&]
stop coloring on additional ampersands (I can live with this). Solutions with&.
either fail (not acceptable) or go black for invalid colors (may be acceptable on a bright background). String.fontcolor()
is a fascinating relic from the 90s. You should never use it. ;-)
Just for the record, here is a possible solution that relies on external CSS.
Its possible to make this even smaller by replacing
span
withc
and removing all"
and.toLowerCase()
(requires.ca,.cA
and so on in the CSS). But this wont make it a 140byt.es solution because of the external data.