If you need to code-golf a set of predefined RGB color palettes, how would you do it?
Problem: Each palette has a variable amount of RGB colors. The program output should closely match the input palettes, in string hex code format (with or without #
prefix) so that it can be fed into Canvas2D APIs. Solution must be valid JavaScript code.
Proposed Solution: Turn each palette into a base64 encoded string. During decoding, use atob
to convert the Base64 palette to a set of hex codes.
Other Solutions? Maybe with RGB565 or RGB444? Please comment if you have any other solutions. :)
TODO idea: use rgb444 or rgb565 to further reduce the base64 payload, and/or use a custom ASCII encoder such as a variant of uuencoding.
The difficulty with 444 is decoding as it may require a sort of bit stream reader. Below is an option with rgb565 using Uint16Array to wrap a single color (2 bytes) into a single 16bit decimal.