2021-01-10 16:06:14 +00:00
|
|
|
import dither
|
|
|
|
import colour.difference
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
COLOURS = 256
|
|
|
|
|
|
|
|
|
2021-01-10 20:10:32 +00:00
|
|
|
def rgb_to_lab(rgb: np.ndarray):
|
|
|
|
srgb = np.clip(
|
|
|
|
dither.linear_to_srgb_array(rgb.astype(np.float32) / 255), 0.0,
|
|
|
|
1.0)
|
|
|
|
xyz = colour.sRGB_to_XYZ(srgb)
|
|
|
|
return colour.XYZ_to_Lab(xyz)
|
|
|
|
|
|
|
|
|
2021-01-10 16:06:14 +00:00
|
|
|
def nearest_colours():
|
|
|
|
diffs = np.empty((COLOURS ** 3, 16), dtype=np.float32)
|
|
|
|
all_rgb = np.array(tuple(np.ndindex(COLOURS, COLOURS, COLOURS)),
|
|
|
|
dtype=np.uint8)
|
2021-01-10 20:10:32 +00:00
|
|
|
all_lab = rgb_to_lab(all_rgb)
|
2021-01-10 16:06:14 +00:00
|
|
|
|
2021-01-10 20:10:32 +00:00
|
|
|
for i, palette_rgb in dither.RGB.items():
|
2021-01-10 16:06:14 +00:00
|
|
|
print(i)
|
2021-01-10 20:10:32 +00:00
|
|
|
palette_lab = rgb_to_lab(palette_rgb)
|
|
|
|
diffs[:, i] = colour.difference.delta_E_CIE2000(all_lab, palette_lab)
|
2021-01-10 16:06:14 +00:00
|
|
|
|
2021-01-10 20:10:32 +00:00
|
|
|
norm = np.max(diffs)
|
|
|
|
return (diffs / norm * 255).astype(np.uint8)
|
2021-01-10 16:06:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
n = nearest_colours()
|
2021-01-12 22:01:29 +00:00
|
|
|
out = np.memmap(filename="distances.npy", mode="w+", dtype=np.uint8,
|
|
|
|
shape=n.shape)
|
|
|
|
out[:] = n[:]
|