info = { type = "image", title = "Colour blocks", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "Moving colour blocks split by black lines", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Layout"] = "Rozloženie", ["Palette"] = "Paleta", ["Vivid"] = "Sýta", ["Pastel"] = "Pastelová", ["Warm"] = "Teplá", ["Cool"] = "Chladná", ["Speed"] = "Rýchlosť" }, cs = { ["Layout"] = "Rozložení", ["Palette"] = "Paleta", ["Vivid"] = "Sytá", ["Pastel"] = "Pastelová", ["Warm"] = "Teplá", ["Cool"] = "Chladná", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "layout", label = T("Layout"), type = "int", min = 1, max = 8, default = 1 }, { key = "detail", label = T("Detail"), type = "int", min = 3, max = 6, default = 5 }, { key = "pal", label = T("Palette"), type = "int", default = "vivid", opts = { keys = { "vivid", "pastel", "warm", "cool" }, T("Vivid"), T("Pastel"), T("Warm"), T("Cool") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local floor, ceil, sin = math.floor, math.ceil, math.sin local rect, blend_rect = gfx.rect, gfx.blend_rect local hash32 = math.hash32 local TAU = 6.2831853 local LOOP = 5400 local EPOCHS = 5 local FADE = 0.12 local PALS = { vivid = { { 5, .95, 1 }, { 28, .95, 1 }, { 52, .95, 1 }, { 95, .9, .95 }, { 140, .9, .9 }, { 185, .9, 1 }, { 225, .9, 1 }, { 270, .85, 1 }, { 315, .9, 1 } }, pastel = { { 350, .42, 1 }, { 20, .45, 1 }, { 48, .45, 1 }, { 110, .4, 1 }, { 170, .4, 1 }, { 205, .42, 1 }, { 265, .4, 1 }, { 305, .4, 1 } }, warm = { { 355, .9, 1 }, { 12, .95, 1 }, { 28, .95, 1 }, { 42, .9, 1 }, { 52, .85, 1 }, { 335, .8, .95 }, { 18, .9, .7 } }, cool = { { 150, .85, .9 }, { 175, .9, 1 }, { 195, .9, 1 }, { 215, .85, 1 }, { 240, .8, 1 }, { 265, .75, 1 }, { 290, .75, 1 }, { 160, .5, 1 } }, } local function hsv(h, s, v) h = (h % 360) / 60 local i = floor(h) local f = h - i local p, q, t = v * (1 - s), v * (1 - s * f), v * (1 - s * (1 - f)) local r, g, b if i == 0 then r, g, b = v, t, p elseif i == 1 then r, g, b = q, v, p elseif i == 2 then r, g, b = p, v, t elseif i == 3 then r, g, b = p, q, v elseif i == 4 then r, g, b = t, p, v else r, g, b = v, p, q end return floor(r * 255 + 0.5), floor(g * 255 + 0.5), floor(b * 255 + 0.5) end local function h3(a, b, c) return hash32(a * 7919 + b * 104729 + c * 15485863) & 0x7FFF end local NV, NR, NA, NF, NP, NC1, NC2, LEAF = {}, {}, {}, {}, {}, {}, {}, {} local NNODE, NLEAF, OFFS = 0, 0, {} local PAL, SPD = PALS.vivid, 3 local READY = false local function build(x0, y0, x1, y1, depth, seed, maxd) NNODE = NNODE + 1 local id = NNODE local w, h = x1 - x0, y1 - y0 local vert if w > h * 1.25 then vert = true elseif h > w * 1.25 then vert = false else vert = (h3(seed, id, 1) % 2 == 0) end local L = vert and w or h local stop = depth >= maxd or L < 18 or (depth >= 2 and h3(seed, id, 2) % 100 < 8 * depth) if stop then NLEAF = NLEAF + 1 LEAF[id] = NLEAF OFFS[NLEAF] = h3(seed, id, 3) / 0x8000 return id end local ratio = 0.38 + 0.24 * (h3(seed, id, 4) / 0x7FFF) NV[id], NR[id] = vert, ratio NA[id] = 0.08 * L NF[id] = 1 + h3(seed, id, 5) % 2 NP[id] = h3(seed, id, 6) / 0x8000 if vert then local x = x0 + w * ratio NC1[id] = build(x0, y0, x, y1, depth + 1, seed, maxd) NC2[id] = build(x, y0, x1, y1, depth + 1, seed, maxd) else local y = y0 + h * ratio NC1[id] = build(x0, y0, x1, y, depth + 1, seed, maxd) NC2[id] = build(x0, y, x1, y1, depth + 1, seed, maxd) end return id end local SEED = 1 function on_settings() PAL = PALS[cfg.pal or "vivid"] or PALS.vivid SPD = cfg.speed or 3 SEED = cfg.layout or 1 NNODE, NLEAF = 0, 0 for i = 1, 200 do LEAF[i] = nil; NV[i] = nil end build(0, 0, 64, 64, 0, SEED, cfg.detail or 5) READY = true end local function pick(l, e) local P = #PAL local c = h3(SEED * 97 + l, e, 7) % P + 1 local pv = h3(SEED * 97 + l, (e - 1) % EPOCHS, 7) % P + 1 if c == pv then c = c % P + 1 end return c end local function leafcolor(l, u) local pe = (u + OFFS[l]) * EPOCHS local k = floor(pe) local fr = pe - k local e = k % EPOCHS local c = PAL[pick(l, e)] if fr >= FADE then return hsv(c[1], c[2], c[3]) end local t = fr / FADE t = t * t * (3 - 2 * t) local p = PAL[pick(l, (e - 1) % EPOCHS)] local dh = ((c[1] - p[1] + 540) % 360) - 180 return hsv(p[1] + dh * t, p[2] + (c[2] - p[2]) * t, p[3] + (c[3] - p[3]) * t) end local function drawleaf(l, x0, y0, x1, y1, u) local r, g, b = leafcolor(l, u) local ex0, ey0, ex1, ey1 = x0 + 1, y0 + 1, x1 - 1, y1 - 1 local ax, ay, bx, by = ceil(ex0), ceil(ey0), floor(ex1), floor(ey1) if bx <= ax or by <= ay then return end rect(ax, ay, bx - ax, by - ay, r, g, b) local c = ax - ex0 if c > 0.04 then blend_rect(ax - 1, ay, 1, by - ay, r, g, b, c) end c = ex1 - bx if c > 0.04 then blend_rect(bx, ay, 1, by - ay, r, g, b, c) end c = ay - ey0 if c > 0.04 then blend_rect(ax, ay - 1, bx - ax, 1, r, g, b, c) end c = ey1 - by if c > 0.04 then blend_rect(ax, by, bx - ax, 1, r, g, b, c) end end local function walk(i, x0, y0, x1, y1, u) local l = LEAF[i] if l then drawleaf(l, x0, y0, x1, y1, u) return end local s = NA[i] * sin(TAU * (NF[i] * u + NP[i])) if NV[i] then local x = x0 + (x1 - x0) * NR[i] + s walk(NC1[i], x0, y0, x, y1, u) walk(NC2[i], x, y0, x1, y1, u) else local y = y0 + (y1 - y0) * NR[i] + s walk(NC1[i], x0, y0, x1, y, u) walk(NC2[i], x0, y, x1, y1, u) end end function render() FR = frame * STEP_HZ // gfx.FPS if not READY then on_settings() end local u = ((FR * SPD) % LOOP) / LOOP gfx.clear(0, 0, 0) walk(1, 0, 0, 64, 64, u) end