HEX colors
Given a library to manipulate PNG files, like imageio, pillow, ..., write a function called to_png
that takes a string and a file name, which produces an image corresponding to the string in the given file.
The string format being:
- A color is defined by its lowercased hexadecimal representation as in HTML, so red is FF0000.
- One given color in the string (a pixel, as in "picture element")) gives a box of 16×16 pixels in the output PNG image.
- A newline in the input string means a new line of 16×16 boxes in the output image.
- All lines can differ in length, if so, shorter lines are completed by transparent boxes of 16×16 pixels so the output image is rectangular.
For example:
FRENCH_FLAG="002654ffffffce1126\n002654ffffffce1126"
to_png(FRENCH_FLAG, "french_flag.png")
Level two:
Add a color_map
parameter, taking a dict of color maps, like {"B": "002654", "W": "ffffff", "R": "ce1126"}
.
Then you can produce images using the color map instead of the actual colors, like:
to_png("RWB\nRWB", "french_flag.png", color_map={"B": "002654", "W": "ffffff", "R": "ce1126"})
While parsing the string, the rule is simple:
- If a pixel character starts with a digit or a lowercase it's an hexadecimal color (6 chars).
- Else it's from the cmap.
The cmap can contain multi-chars colors, like Red
, Blue0
, Blue1
, ...
The cmap cannot contain ambiguous aliases, like "Blue" and "Blue1", because "Bleu1" starts with "Blue", encontering "Blue" would lead to the ambiguous situation "should we stop or continue eating chars?".
As the output of the correction bot is Markdown, it should be possible to produce inline images in base64.