info = { type = "image", title = "Morph ABC", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "Pixel letters morph through the alphabet", api = 33, } local STEP_HZ = 30 local FR = 0 local ltr = ({ sk = { ["Letter hold"] = "Držanie písmen", ["Dot grid"] = "Mriežka bodov" }, cs = { ["Letter hold"] = "Držení písmen", ["Dot grid"] = "Mřížka bodů" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "hold", label = LT("Letter hold"), type = "int", min = 1, max = 6, unit = "s", default = 2 }, { key = "dots", label = LT("Dot grid"), type = "bool", default = true }, } local rect, blend_rect = gfx.rect, gfx.blend_rect local floor = math.floor local W, H, FPS = gfx.W, gfx.H, gfx.FPS local P, B = 8, 7 local OX, OY = 12, 4 local F57 = { font = "5x7" } local BGC, DOT = { 58, 60, 76 }, { 92, 96, 120 } local MOVE, SPREAD, DUR = 26, 10, 16 local GLYPH = {} local SW, SH = 16, 24 local ORIGIN = nil local function raw(i) gfx.clear(0, 0, 0) gfx.text(4, 8, string.char(64 + i), 255, 255, 255, F57) local s = gfx.snapshot(0, 0, SW, SH) local c = {} for y = 0, SH - 1 do for x = 0, SW - 1 do if s:byte((y * SW + x) * 3 + 1) > 127 then c[#c + 1] = x; c[#c + 1] = y end end end return c end local function lo(c, k) local m = 999 for j = k, #c, 2 do if c[j] < m then m = c[j] end end return m end local function glyph(i) if GLYPH[i] then return GLYPH[i] end if not ORIGIN then ORIGIN = { lo(raw(13), 1), lo(raw(8), 2) } end local c, g = raw(i), {} for j = 1, #c, 2 do local x, y = c[j] - ORIGIN[1], c[j + 1] - ORIGIN[2] if x >= 0 and x < 5 and y >= 0 and y < 7 then g[#g + 1] = y * 5 + x end end table.sort(g) GLYPH[i] = g return g end local HUE = {} for i = 1, 26 do HUE[i] = (i - 1) * 47 % 360 end local function hsv(h) h = h % 360 local i = floor(h / 60) local f = h / 60 - i local v, p = 255, 46 local q, u = floor(255 - 209 * f), floor(46 + 209 * f) if i == 0 then return v, u, p elseif i == 1 then return q, v, p elseif i == 2 then return p, v, u elseif i == 3 then return p, q, v elseif i == 4 then return u, p, v end return v, p, q end local function block(x, y, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy rect(ix, iy, B, B, r, g, b) if fx > 0.04 then blend_rect(ix + B, iy, 1, B, r, g, b, fx) end if fy > 0.04 then blend_rect(ix, iy + B, B, 1, r, g, b, fy) end rect(ix, iy, B, 1, r + (255 - r) * 45 // 100, g + (255 - g) * 45 // 100, b + (255 - b) * 45 // 100) end local BG = nil local function build_bg() rect(0, 0, W, H, BGC[1], BGC[2], BGC[3]) if cfg.dots ~= false then for cy = 0, 6 do for cx = 0, 4 do rect(OX + cx * P + 2, OY + cy * P + 2, 3, 3, DOT[1], DOT[2], DOT[3]) end end end BG = gfx.snapshot(0, 0, W, H) end function on_settings() BG = nil end function render() FR = frame * STEP_HZ // gfx.FPS local hold = (cfg.hold or 2) * STEP_HZ local L = hold + MOVE local n = FR // L local f = FR - n * L local a = n % 26 + 1 local z = a % 26 + 1 local S, T = glyph(a), glyph(z) if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) if f < hold then local r, g, b = hsv(HUE[a]) for k = 1, #S do local c = S[k] block(OX + (c % 5) * P, OY + (c // 5) * P, r, g, b) end return end local tm = f - hold local n1, n2 = #S, #T local m = n1 > n2 and n1 or n2 local h1 = HUE[a] local dh = (HUE[z] - h1 + 540) % 360 - 180 for i = 0, m - 1 do local s, d = S[i * n1 // m + 1], T[i * n2 // m + 1] local u = (tm - (i * 7) % SPREAD) / DUR if u < 0 then u = 0 elseif u > 1 then u = 1 end local e = u * u * (3 - 2 * u) local sx, sy, dx, dy = s % 5, s // 5, d % 5, d // 5 local r, g, b = hsv(h1 + dh * e) block(OX + (sx + (dx - sx) * e) * P, OY + (sy + (dy - sy) * e) * P, r, g, b) end end