info = { type = "image", title = "DNA helix", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "A double helix slowly turning in the dark", api = 33, } local ltr = ({ sk = { ["Strand 1"] = "Prameň 1", ["Strand 2"] = "Prameň 2", ["Speed"] = "Rýchlosť", ["Turns"] = "Otáčky" }, cs = { ["Strand 1"] = "Pramen 1", ["Strand 2"] = "Pramen 2", ["Speed"] = "Rychlost", ["Turns"] = "Otáčky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "ca", label = LT("Strand 1"), type = "color", default = "#28DCFF" }, { key = "cb", label = LT("Strand 2"), type = "color", default = "#FF9628" }, { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 10, default = 4 }, { key = "turns", label = LT("Turns"), type = "int", min = 1, max = 4, default = 2 }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, cos, min, max = math.floor, math.sin, math.cos, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local CY = H // 2 local AMP = H * 0.25 local RUNG_STEP = 5 local PAIRS = { { { 255, 220, 60 }, { 100, 230, 110 } }, { { 100, 230, 110 }, { 255, 220, 60 } }, { { 255, 90, 160 }, { 150, 110, 255 } }, { { 150, 110, 255 }, { 255, 90, 160 } }, } local RX, RP = {}, {} for x = 2, W - 1, RUNG_STEP do RX[#RX + 1] = x RP[#RP + 1] = PAIRS[1 + umod(hash32(x * 0x9E3779B1), 4)] end local DOT = { 64, 84, 170 } local SPARK = { 255, 255, 255 } local A, B, AB, BB = {}, {}, {}, {} local K, OMEGA = 0, 0 local function col(c, k) return { floor(c.r * k), floor(c.g * k), floor(c.b * k) } end function on_settings() local ca = cfg.ca or { r = 40, g = 220, b = 255 } local cb = cfg.cb or { r = 255, g = 150, b = 40 } A, AB = col(ca, 1), col(ca, 0.5) B, BB = col(cb, 1), col(cb, 0.5) K = (cfg.turns or 2) * TAU / W OMEGA = (cfg.speed or 4) * TAU / (16 * STEP_HZ) end local DX, DV, DO = {}, {}, {} for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) DX[i], DV[i], DO[i] = umod(h, W), 0.1 + umod(h >> 8, 20) / 100.0, umod(h >> 16, 997) end local function dots(t) for i = 1, #DX do local yf = H - 1 - (DO[i] + t * DV[i]) % (H + 4) local y = floor(yf) local f = yf - y blend(DX[i], y, DOT[1], DOT[2], DOT[3], 1 - f) blend(DX[i], y + 1, DOT[1], DOT[2], DOT[3], f) end end local YA, ZA = {}, {} local function helix(t) local ph = t * OMEGA for x = 0, W do local th = x * K + ph YA[x], ZA[x] = CY + AMP * sin(th), cos(th) end for x = 0, W - 1 do local a0, a1 = YA[x], YA[x + 1] local front = ZA[x] >= 0 local y0, y1 = a0, a1 if front then y0, y1 = 2 * CY - a0, 2 * CY - a1 end local c = front and BB or AB local top = floor(min(y0, y1) - 0.5) rect(x, top, 1, floor(max(y0, y1) + 0.5) - top + 1, c[1], c[2], c[3]) end for i = 1, #RX do local x = RX[i] local ya = floor(YA[x] + 0.5) local yb = 2 * CY - ya local p = RP[i] local ca, cb = p[1], p[2] if ya < CY then rect(x, ya, 1, CY - ya, ca[1], ca[2], ca[3]) rect(x, CY, 1, yb - CY, cb[1], cb[2], cb[3]) elseif ya > CY then rect(x, CY, 1, ya - CY, ca[1], ca[2], ca[3]) rect(x, yb, 1, CY - yb, cb[1], cb[2], cb[3]) end end for x = 0, W - 1 do local a0, a1 = YA[x], YA[x + 1] local front = ZA[x] >= 0 local y0, y1 = a0, a1 if not front then y0, y1 = 2 * CY - a0, 2 * CY - a1 end local c = front and A or B local top, bot = min(y0, y1) - 1.5, max(y0, y1) + 1.5 local ti, bi = floor(top), floor(bot) rect(x, ti + 1, 1, bi - ti - 1, c[1], c[2], c[3]) blend(x, ti, c[1], c[2], c[3], 1 - (top - ti)) blend(x, bi, c[1], c[2], c[3], bot - bi) end end local SCAN_P = W + 3 * STEP_HZ local function scan(t) local x = (t % SCAN_P) * 2 - 2 if x < 0 or x >= W then return end for dx = 0, 2 do local xx = x - dx if xx >= 0 then local y = YA[xx] if ZA[xx] < 0 then y = 2 * CY - y end px(xx, floor(y + 0.5), SPARK[1], SPARK[2], SPARK[3]) end end end function render() gfx.clear(0, 0, 0) local t = frame * STEP_HZ // FPS dots(t) helix(t) scan(t) end