"""A seamless red marble picture (PNG), standing in for the mockup's SVG feTurbulence filter. ThorVG has no SVG filters, so this module draws the noise itself. It is the only module that uses numpy. Recipe (like feTurbulence type="turbulence" -> greyscale -> table colour ramp): 1. Periodic gradient (Perlin) noise: the random lattice wraps, so the picture tiles with no seam. 2. Turbulence: add the absolute value of six octaves, each twice as fine and half as strong. 3. Colour: the mockup's seven-step red / green / blue tables turn the value into veined dark red. """ import struct import zlib import numpy TILE_SIZE = 256 BASE_CELLS = (5, 7) # lattice cells across x and y for the first octave (mockup: 0.018 x 0.026 per px) OCTAVES = 6 SEED = 4 def _fade(values): return values * values * values * (values * (values * 6 - 15) + 10) def periodic_noise(size, cells_x, cells_y, random): """Perlin noise on a size x size grid that repeats exactly every `size` pixels.""" angles = random.uniform(0, 2 * numpy.pi, (cells_y, cells_x)) gradient_x, gradient_y = numpy.cos(angles), numpy.sin(angles) pixel = numpy.arange(size) + 0.5 cell_u, cell_v = pixel * cells_x / size, pixel * cells_y / size column, row = numpy.floor(cell_u).astype(int), numpy.floor(cell_v).astype(int) frac_u, frac_v = cell_u - column, cell_v - row frac_u, frac_v = frac_u[numpy.newaxis, :], frac_v[:, numpy.newaxis] columns = (column, (column + 1) % cells_x) rows = (row, (row + 1) % cells_y) def corner(step_y, step_x): gradient_index = (rows[step_y][:, numpy.newaxis], columns[step_x][numpy.newaxis, :]) return (gradient_x[gradient_index] * (frac_u - step_x) + gradient_y[gradient_index] * (frac_v - step_y)) blend_u, blend_v = _fade(frac_u), _fade(frac_v) top = corner(0, 0) + blend_u * (corner(0, 1) - corner(0, 0)) bottom = corner(1, 0) + blend_u * (corner(1, 1) - corner(1, 0)) return top + blend_v * (bottom - top) def turbulence(size=TILE_SIZE, seed=SEED): random = numpy.random.default_rng(seed) total = numpy.zeros((size, size)) for octave in range(OCTAVES): scale = 2 ** octave total += numpy.abs(periodic_noise(size, BASE_CELLS[0] * scale, BASE_CELLS[1] * scale, random)) / scale return total def colour_ramp(values, red, green, blue): """feComponentTransfer type="table": straight lines between evenly spaced table values.""" stops = numpy.linspace(0, 1, len(red)) channels = [numpy.interp(values, stops, table) for table in (red, green, blue)] return numpy.clip(numpy.stack(channels, axis=-1) * 255 + 0.5, 0, 255).astype(numpy.uint8) def png_bytes(rgb): """Encode an (height, width, 3) uint8 array as an RGB PNG (stdlib zlib only).""" height, width, _ = rgb.shape raw = b"".join(b"\x00" + rgb[row].tobytes() for row in range(height)) def chunk(kind, data): return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF) header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(raw, 9)) + chunk(b"IEND", b"")) def make_marble_png(pattern): """Return PNG bytes for the theme's marble tile.""" values = turbulence() values = values / max(values.max(), 1e-6) return png_bytes(colour_ramp(values, pattern["red"], pattern["green"], pattern["blue"]))