ii-pix/ntsc_colours.py

53 lines
1.7 KiB
Python
Raw Normal View History

"""Precomputes all possible colours available via NTSC emulation."""
import numpy as np
from PIL import Image
import screen
def main():
2021-11-02 15:47:37 +00:00
s = screen.DHGRScreen(palette=None)
colours = {}
unique = set()
2021-07-19 11:55:50 +00:00
print("import numpy as np")
print()
2021-11-02 12:30:43 +00:00
print("# Indexed by (trailing 8-bit dot pattern, x % 4)")
print("SRGB = {")
# For each sequence of 8 pixels, compute the RGB colour of the right-most
# pixel, using NTSC emulation.
2021-07-19 11:55:50 +00:00
# Double Hi-Res has a timing shift that rotates the displayed bits one
# position with respect to NTSC phase.
ntsc_shift = 1
2021-11-02 12:30:43 +00:00
for j in range(ntsc_shift, ntsc_shift + 4):
2021-11-02 15:47:37 +00:00
bitmap = np.zeros((1, 11 + ntsc_shift), dtype=bool)
2021-07-19 11:55:50 +00:00
for bits in range(256):
2021-11-02 15:47:37 +00:00
bits8 = np.empty((8,), dtype=bool)
2021-07-19 11:55:50 +00:00
for i in range(8):
bits8[i] = bits & (1 << i)
2021-11-02 12:30:43 +00:00
bitmap[0, j:j + 8] = bits8
2021-07-19 11:55:50 +00:00
2021-11-02 12:30:43 +00:00
# bitmap_to_ntsc produces 3 output pixels for each DHGR input
2021-11-02 15:23:23 +00:00
ntsc = s.bitmap_to_image_ntsc(bitmap)
2021-11-02 12:30:43 +00:00
last_colour = ntsc[0, 3 * (j + 8) - 1, :]
colours[(bits, j - ntsc_shift)] = last_colour
2021-07-19 11:55:50 +00:00
unique.add(tuple(last_colour))
print(" (%d, %d): np.array((%d, %d, %d))," % (
2021-11-02 12:30:43 +00:00
bits, j - ntsc_shift, last_colour[0], last_colour[1],
last_colour[2]))
print("}")
print("# %d unique colours" % len(unique))
# Show spectrum of available colours sorted by HSV hue value
2021-11-02 12:30:43 +00:00
im = np.zeros((128 * 4, 256 * 16, 3), dtype=np.uint8)
2021-07-19 11:55:50 +00:00
for x, j in colours:
2021-11-02 12:30:43 +00:00
im[128 * j:128 * (j + 1), x * 16: (x + 1) * 16, :] = colours[x, j]
Image.fromarray(im).show()
if __name__ == "__main__":
main()