info = { type = "image", title = "Dot mandala", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "Rings of dots turning against each other", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Palette"] = "Paleta", ["Neon"] = "Neón", ["Ember"] = "Žeravé uhlíky", ["Ocean"] = "Oceán", ["Dots"] = "Bodky", ["Airy"] = "Vzdušná", ["Normal"] = "Normálne", ["Speed"] = "Rýchlosť" }, cs = { ["Palette"] = "Paleta", ["Ember"] = "Žhavé uhlíky", ["Ocean"] = "Oceán", ["Dots"] = "Tečky", ["Airy"] = "Vzdušná", ["Normal"] = "Normálně", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pal", label = T("Palette"), type = "int", default = "neon", opts = { keys = { "neon", "ember", "ocean" }, T("Neon"), T("Ember"), T("Ocean") } }, { key = "dense", label = T("Dots"), type = "int", default = "normal", opts = { keys = { "airy", "normal" }, T("Airy"), T("Normal") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local floor, ceil, sin, cos, sqrt = math.floor, math.ceil, math.sin, math.cos, math.sqrt local rect, round_rect = gfx.rect, gfx.round_rect local TAU = 6.2831853 local LOOP = 1800 local CX, CY = 31.5, 31.5 local K = 96 local PALS = { neon = { bg = { 6, 4, 28 }, s = 0.85, v = 1.0, hues = { 300, 230, 185, 130, 55, 350 } }, ember = { bg = { 22, 4, 16 }, s = 0.9, v = 1.0, hues = { 350, 20, 45, 20 } }, ocean = { bg = { 4, 10, 30 }, s = 0.8, v = 1.0, hues = { 170, 200, 230, 270, 230, 200 } }, } 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 PLATE_R = { 30.5, 21.5, 13, 0 } local PLATE = {} for p = 1, 3 do local R, Ri = PLATE_R[p], PLATE_R[p + 1] local x0, w, xb, wb = {}, {}, {}, {} for y = 0, 63 do local dy = y - CY local d2 = R * R - dy * dy x0[y], w[y], xb[y], wb[y] = 0, 0, 0, 0 if d2 > 0 then local half = sqrt(d2) local lo, hi = ceil(CX - half), floor(CX + half) local i2 = Ri * Ri - dy * dy if i2 > 0 then local ih = sqrt(i2) local ilo, ihi = ceil(CX - ih), floor(CX + ih) x0[y], w[y] = lo, ilo - lo xb[y], wb[y] = ihi + 1, hi - ihi else x0[y], w[y] = lo, hi - lo + 1 end end end local list, n = {}, 0 for seg = 1, 2 do local X, Wd = (seg == 1) and x0 or xb, (seg == 1) and w or wb local y = 0 while y <= 63 do if Wd[y] > 0 then local y2 = y while y2 < 63 and Wd[y2 + 1] == Wd[y] and X[y2 + 1] == X[y] do y2 = y2 + 1 end list[n + 1], list[n + 2], list[n + 3], list[n + 4] = X[y], y, Wd[y], y2 - y + 1 n = n + 4 y = y2 + 1 else y = y + 1 end end end PLATE[p] = list end local PR, PG, PB = {}, {}, {} local BG, SPD, SP = { 6, 4, 28 }, 3, 4.4 local RR, RM, RC, RD, RS, NR = {}, {}, {}, {}, {}, 0 local READY = false function on_settings() local P = PALS[cfg.pal or "neon"] or PALS.neon BG = P.bg local hs, n = P.hues, #P.hues for k = 0, K - 1 do local t = k / K * n local i0 = floor(t) local f = t - i0 local h0, h1 = hs[i0 % n + 1], hs[(i0 + 1) % n + 1] local dh = ((h1 - h0 + 540) % 360) - 180 PR[k + 1], PG[k + 1], PB[k + 1] = hsv(h0 + dh * f, P.s, P.v) end SPD = cfg.speed or 3 SP = (cfg.dense == "airy") and 6.0 or 4.6 NR = 0 local R = SP * 0.85 while R < 31.4 do NR = NR + 1 local m = 2 * floor(3.14159 * R / SP + 0.5) if m < 6 then m = 6 end RR[NR], RM[NR] = R, m RC[NR] = floor(m * (0.2 + 0.05 * (NR % 3)) + 0.5) RD[NR] = (NR % 2 == 0) and 1 or -1 RS[NR] = NR % 3 R = R + SP end READY = true end local NPH = 6 local DM = {} for qx = 0, NPH - 1 do for qy = 0, NPH - 1 do local fx, fy = qx / NPH, qy / NPH local function c(w) w = w * 2.4; if w > 1 then w = 1 end; return floor(w * 255 + 0.5) end DM[qx * NPH + qy] = string.char(c((1 - fx) * (1 - fy)), c(fx * (1 - fy)), c((1 - fx) * fy), c(fx * fy)) end end local blend_mask = gfx.blend_mask local function dot(x, y, r, g, b, a) local tx, ty = floor(x * NPH + 0.5), floor(y * NPH + 0.5) blend_mask(DM[(tx % NPH) * NPH + ty % NPH], r, g, b, tx // NPH, ty // NPH, 2, 2, 2, 2, a) end function render() FR = frame * STEP_HZ // gfx.FPS if not READY then on_settings() end local u = ((FR * SPD) % LOOP) / LOOP local br, bg_, bb = BG[1], BG[2], BG[3] gfx.clear(br, bg_, bb) for p = 1, 3 do local k = floor(((u - (p == 1 and 5 or p == 2 and 3 or 1) * 0.07) % 1) * K) % K + 1 local r = br + (PR[k] - br) * 0.13 local g = bg_ + (PG[k] - bg_) * 0.13 local b = bb + (PB[k] - bb) * 0.13 local list = PLATE[p] r, g, b = floor(r), floor(g), floor(b) for q = 1, #list, 4 do rect(list[q], list[q + 1], list[q + 2], list[q + 3], r, g, b) end end for i = 1, NR do local k = floor(((u - i * 0.07) % 1) * K) % K + 1 local r, g, b = PR[k], PG[k], PB[k] local R, m, style = RR[i], RM[i], RS[i] local step = TAU / m local th = RD[i] * step * RC[i] * u + i * 0.5 local c, s = cos(th), sin(th) local cs, sn = cos(step), sin(step) for d = 1, m do local a = 1.0 if style == 1 and d % 2 == 0 then a = 0.5 end if style ~= 2 then dot(CX + R * c, CY + R * s, r, g, b, a) end if style == 2 then dot(CX + (R - 0.9) * c, CY + (R - 0.9) * s, r, g, b, 0.9) dot(CX + (R + 0.9) * c, CY + (R + 0.9) * s, r, g, b, 0.9) end c, s = c * cs - s * sn, c * sn + s * cs end end local k = floor((u % 1) * K) % K + 1 local r, g, b = PR[k], PG[k], PB[k] round_rect(30, 30, 4, 4, r, g, b, r * 6 // 10, g * 6 // 10, b * 6 // 10, 1) end