Mapping to the PICO-8 palette, perceptually

https://news.ycombinator.com/rss Hits: 4
Summary

# The PICO-8 palette palette_srgb_u8 = np.array([ (0x00, 0x00, 0x00), (0x1d, 0x2b, 0x53), (0x7e, 0x25, 0x53), (0x00, 0x87, 0x51), (0xab, 0x52, 0x36), (0x5f, 0x57, 0x4f), (0xc2, 0xc3, 0xc7), (0xff, 0xf1, 0xe8), (0xff, 0x00, 0x4d), (0xff, 0xa3, 0x00), (0xff, 0xec, 0x27), (0x00, 0xe4, 0x36), (0x29, 0xad, 0xff), (0x83, 0x76, 0x9c), (0xff, 0x77, 0xa8), (0xff, 0xcc, 0xaa), # (0x29, 0x18, 0x14), # the secret palette begins here :) # (0x11, 0x1d, 0x35), # (0x42, 0x21, 0x36), # (0x12, 0x53, 0x59), # (0x74, 0x2f, 0x29), # (0x49, 0x33, 0x3b), # (0xa2, 0x88, 0x79), # (0xf3, 0xef, 0x7d), # (0xbe, 0x12, 0x50), # (0xff, 0x6c, 0x24), # (0xa8, 0xe7, 0x2e), # (0x00, 0xb5, 0x43), # (0x06, 0x5a, 0xb5), # (0x75, 0x46, 0x65), # (0xff, 0x6e, 0x59), # (0xff, 0x9d, 0x81), ], dtype=np.uint8) def euclidean_distance(a, b): delta = a - b return np.sqrt(np.sum(delta**2, axis=-1)) def weighted_rgb_distance(a, b): # Weighting and internal gamma chosen to match libimagequant # See https://github.com/ImageOptim/libimagequant/blob/6aad8f20b28185823813b8bd6823171711480dca/src/pal.rs#L12C1-L19C38 # Convert from sRGB to internal 1.754 gamma, giving more weight to bright colors. # Equal to 0.57/0.4545 = 0.57 / (1/2.2) # NOTE: Does not match libimagequant's behavior exactly, just my best attempt power = 2.2/1.754 channel_weights = np.array([[[0.5, 1.00, 0.45]]]) aw = a * channel_weights bw = b * channel_weights delta = aw**power - bw**power return np.sqrt(np.sum(delta**2, axis=-1)) def hyab_distance(a, b): delta = a - b dL = np.sum(np.abs(delta[..., 0:1]), axis=-1) dab = np.sqrt(np.sum(delta[..., 1:3]**2, axis=-1)) return dL + dab def L_weighted_hyab_distance(a, b): delta = a - b dL = np.sum(np.abs(delta[..., 0:1]), axis=-1) dab = np.sqrt(np.sum(delta[..., 1:3]**2, axis=-1)) return 3 * dL + dab def L_15_weighted_hyab_distance(a, b): delta = a - b dL = np.sum(np.abs(delta[..., 0:1]), axis=-1) dab = np.sqrt(np.sum(delta[..., 1:3]**2, axis=-1)) return 1.5 * dL + dab def L_distance(a, b): delta = a - b dL = n...

First seen: 2025-09-11 13:15

Last seen: 2025-09-11 16:16