info = { type = "image", title = "Kaleidoscope", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "Slowly turning mirrored kaleidoscope of beads", api = 33, } local STEP_HZ = 30 local FR = 0 local ltr = ({ sk = { ["Sectors"] = "Výseky", ["Palette"] = "Paleta", ["Ember"] = "Žeravé uhlíky", ["Jewel"] = "Drahokam", ["Aurora"] = "Polárna žiara", ["Speed"] = "Rýchlosť" }, cs = { ["Sectors"] = "Výseče", ["Palette"] = "Paleta", ["Ember"] = "Žhavé uhlíky", ["Jewel"] = "Drahokam", ["Aurora"] = "Polární záře", ["Speed"] = "Rychlost" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "sec", label = LT("Sectors"), type = "int", default = "s8", opts = { keys = { "s6", "s8", "s12" }, LT("6"), LT("8"), LT("12") } }, { key = "pal", label = LT("Palette"), type = "int", default = "ember", opts = { keys = { "ember", "jewel", "aurora" }, LT("Ember"), LT("Jewel"), LT("Aurora") } }, { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local blend_mask, round_rect = gfx.blend_mask, gfx.round_rect local TAU = 6.2831853 local LOOP = 2400 local CX, CY = 31.5, 31.5 local PALS = { ember = { bg = { 18, 6, 40 }, A = { 350, 62 }, B = { 165, 38 } }, jewel = { bg = { 8, 8, 40 }, A = { 250, 72 }, B = { 128, 52 } }, aurora = { bg = { 6, 22, 40 }, A = { 150, 62 }, B = { 285, 55 } }, } local SH = { { 3, 17, 0.40, 27, 4, 1, 0.50, 0.40, 1, 1, 0.00, 1, 0 }, { 2, 13, 0.70, 19, 5, 2, 0.55, 0.35, 1, 2, 0.25, 1, 0 }, { 1, 9, 0.90, 12, 4, 1, 0.35, 0.30, 2, 1, 0.50, 1, 0 }, { 3, 9, 0.95, 23, 5, 3, 0.30, 0.25, 1, 1, 0.75, 2, 1 }, { 1, 7, 0.95, 30, 2, 2, 0.60, 0.30, 2, 1, 0.10, 2, 2 }, { 2, 9, 0.85, 6, 2, 1, 0.50, 0.40, 3, 2, 0.40, 1, 0 }, { 1, 5, 1.00, 16, 7, 3, 0.50, 0.40, 2, 1, 0.90, 2, 1 }, { 3, 5, 1.00, 28, 3, 2, 0.70, 0.25, 3, 2, 0.60, 1, 0 }, } 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 MASKS = {} local function getmask(kind, S) local key = kind * 32 + S local m = MASKS[key] if m then return m end if kind == 2 and S < 7 then kind = 1 end local hh = (S - 1) // 2 local n = hh + 1 local R = S / 2 - 0.25 local Ri = R - 1.8 local T = {} for a = 0, hh do for b = 0, a do local d = sqrt(a * a + b * b) local c = R - d + 0.5 if c > 1 then c = 1 elseif c < 0 then c = 0 end local v if kind == 1 then v = c elseif kind == 2 then local ci = Ri - d + 0.5 if ci > 1 then ci = 1 elseif ci < 0 then ci = 0 end v = c * (1 - ci) else v = c * (0.45 + 0.55 * (1 - d / R)) end if v < 0 then v = 0 end v = floor(v * 255 + 0.5) T[a * n + b] = v T[b * n + a] = v end end local rows, row = {}, {} for dy = -hh, hh do local a = dy < 0 and -dy or dy for dx = -hh, hh do row[dx + hh + 1] = T[a * n + (dx < 0 and -dx or dx)] end rows[dy + hh + 1] = string.char(table.unpack(row, 1, S)) end m = table.concat(rows) MASKS[key] = m return m end local NPH = 3 local PM = {} local BUILD_MAX, BUILD = 3, 0 local function phmask(kind, S, qx, qy) local key = ((kind * 32 + S) * 4 + qx) * 4 + qy local m = PM[key] if m then return m end if BUILD <= 0 then return nil end BUILD = BUILD - 1 if kind == 2 and S < 7 then kind = 1 end local G = S + 2 local cxm, cym = (S + 1) / 2 + qx / NPH, (S + 1) / 2 + qy / NPH local R = S / 2 - 0.25 local Ri = R - 1.8 local rows, row = {}, {} for j = 0, G - 1 do for i = 0, G - 1 do local dx, dy = i - cxm, j - cym local d = sqrt(dx * dx + dy * dy) local c = R - d + 0.5 if c > 1 then c = 1 elseif c < 0 then c = 0 end local v if kind == 1 then v = c elseif kind == 2 then local ci = Ri - d + 0.5 if ci > 1 then ci = 1 elseif ci < 0 then ci = 0 end v = c * (1 - ci) else v = c * (0.45 + 0.55 * (1 - d / R)) end if v < 0 then v = 0 end row[i + 1] = floor(v * 255 + 0.5) end rows[j + 1] = string.char(table.unpack(row, 1, G)) end m = table.concat(rows) PM[key] = m return m end local function bead(kind, m, S, cx, cy, r, g, b, a) if S >= 13 then local o = (S - 1) * 0.5 blend_mask(m, r, g, b, floor(cx - o + 0.5), floor(cy - o + 0.5), S, S, S, S, a) return end local tx, ty = floor((cx - (S + 1) * 0.5) * NPH + 0.5), floor((cy - (S + 1) * 0.5) * NPH + 0.5) local ix, iy, qx, qy = tx // NPH, ty // NPH, tx % NPH, ty % NPH local G = S + 2 local pm = phmask(kind, S, qx, qy) if pm then blend_mask(pm, r, g, b, ix, iy, G, G, G, G, a); return end local o = (S - 1) * 0.5 blend_mask(getmask(kind, S), r, g, b, floor(cx - o + 0.5), floor(cy - o + 0.5), S, S, S, S, a) end local NAX, SPD = 4, 3 local PAL, READY = PALS.ember, false local CJ, SJ = {}, {} function on_settings() local s = cfg.sec or "s8" NAX = (s == "s6") and 3 or (s == "s12") and 6 or 4 PAL = PALS[cfg.pal or "ember"] or PALS.ember SPD = cfg.speed or 3 READY = true end function render() FR = frame * STEP_HZ // gfx.FPS if not READY then on_settings() end BUILD = BUILD_MAX local u = ((FR * SPD) % LOOP) / LOOP local bg = PAL.bg gfx.clear(bg[1], bg[2], bg[3]) local breath = 1 + 0.06 * sin(TAU * u * 2) local rot = (TAU / NAX) * u for j = 0, NAX - 1 do local an = TAU * j / NAX + rot CJ[j], SJ[j] = cos(an), sin(an) end local half = 3.14159265 / NAX for si = 1, #SH do local s = SH[si] local kind, S0, alpha, r0, dr, hr, f0, df, hf, grp, cph, chain, dir = s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9], s[10], s[11], s[12], s[13] local r = (r0 + dr * sin(TAU * (hr * u + si * 0.37))) * breath local f = f0 + df * sin(TAU * (hf * u + si * 0.21)) local ang = f * half local rng = (grp == 1) and PAL.A or PAL.B local hue = rng[1] + rng[2] * (0.5 + 0.5 * sin(TAU * (u + cph))) local cr, cg, cb = hsv(hue, 0.85, 1.0) for k = 0, chain - 1 do local S = S0 - 2 * k if S < 3 then S = 3 end local rk, ak = r, ang if dir == 1 then rk = r + k * S0 * 0.7 elseif dir == 2 then ak = ang + k * (S0 * 0.7) / r end local m = (S >= 13) and getmask(kind, S) or nil local lx, ly = rk * cos(ak), rk * sin(ak) for j = 0, NAX - 1 do local cj, sj = CJ[j], SJ[j] bead(kind, m, S, CX + lx * cj - ly * sj, CY + lx * sj + ly * cj, cr, cg, cb, alpha) bead(kind, m, S, CX + lx * cj + ly * sj, CY + lx * sj - ly * cj, cr, cg, cb, alpha) end end end local rng = PAL.A local hue = rng[1] + rng[2] * (0.5 + 0.5 * sin(TAU * u)) local r1, g1, b1 = hsv(hue, 0.6, 1.0) local r2, g2, b2 = hsv(hue, 0.9, 0.7) round_rect(28, 28, 8, 8, r1, g1, b1, r2, g2, b2, 3) end