info = { type = "image", title = "Rainbow battery", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A glossy rainbow battery charging and draining", api = 33, } local tr = ({ sk = { ["Mode"] = "Režim", ["Charge and drain"] = "Nabíjanie a vybíjanie", ["Charge only"] = "Len nabíjanie", ["Drain only"] = "Len vybíjanie", ["Speed"] = "Rýchlosť" }, cs = { ["Mode"] = "Režim", ["Charge and drain"] = "Nabíjení a vybíjení", ["Charge only"] = "Pouze nabíjení", ["Drain only"] = "Pouze vybíjení", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "mode", label = T("Mode"), type = "int", default = "cycle", opts = { keys = { "cycle", "charge", "drain" }, T("Charge and drain"), T("Charge only"), T("Drain only") } }, { key = "rate", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, min, max = math.floor, math.sin, math.min, math.max local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local CX0, CY0, CW, CH = 7, 34, 7, 17 local BASE = { { 255, 40, 40 }, { 255, 120, 20 }, { 255, 224, 30 }, { 40, 230, 90 }, { 40, 110, 255 }, { 160, 60, 255 }, } local HI, MID, LO, DIM = {}, {}, {}, {} for i = 1, 6 do local c = BASE[i] local function k(f, w) return { floor(c[1] * f + w * (255 - c[1])), floor(c[2] * f + w * (255 - c[2])), floor(c[3] * f + w * (255 - c[3])) } end HI[i], MID[i], LO[i], DIM[i] = k(1, 0.4), c, k(0.6, 0), k(0.2, 0) end local FONT = { font = "10x20", cell = true } local BOLT, ARROW local IW, IH = 14, 26 local function polygon(pts, fn) local n = #pts // 2 for y = 0, IH - 1 do local xs = {} for i = 1, n do local j = i % n + 1 local x0, y0, x1, y1 = pts[2 * i - 1], pts[2 * i], pts[2 * j - 1], pts[2 * j] if (y0 <= y + 0.5 and y1 > y + 0.5) or (y1 <= y + 0.5 and y0 > y + 0.5) then xs[#xs + 1] = x0 + (y + 0.5 - y0) / (y1 - y0) * (x1 - x0) end end if #xs >= 2 then table.sort(xs) local a, b = floor(xs[1] + 0.5), floor(xs[#xs] + 0.5) - 1 if b >= a then local r, g, bl = fn(y) rect(a, y, b - a + 1, 1, r, g, bl) px(a, y, 255, 255, 235) end end end end local function build_icons() rect(0, 0, IW, IH, 255, 0, 255) polygon({ 9, 0, 1, 14, 6, 14, 3, 25, 13, 10, 8, 10, 12, 0 }, function(y) local u = y / 25; return 255, floor(246 - 78 * u), floor(150 - 120 * u) end) BOLT = gfx.snapshot(0, 0, IW, IH) rect(0, 0, IW, IH, 255, 0, 255) polygon({ 4, 2, 10, 2, 10, 12, 13, 12, 7, 22, 1, 12, 4, 12 }, function(y) local u = y / 22; return floor(190 - 60 * u), floor(200 - 60 * u), floor(222 - 60 * u) end) ARROW = gfx.snapshot(0, 0, IW, IH) end local HOLD, QUICK = 45, 20 local function level(t) local mode = cfg.mode or "cycle" local run = 330 - 50 * (cfg.rate or 3) if mode == "cycle" then local P = 2 * run + 2 * HOLD local p = (t + 40) % P if p < run then local u = p / run; return 100 * (1 - (1 - u) * (1 - u)), "c" end p = p - run if p < HOLD then return 100, "f" end p = p - HOLD if p < run then return 100 * (1 - p / run), "d" end return 0, "e" elseif mode == "charge" then local P = run + HOLD + QUICK local p = (t + 40) % P if p < run then local u = p / run; return 100 * (1 - (1 - u) * (1 - u)), "c" end p = p - run if p < HOLD then return 100, "f" end return 100 * (1 - (p - HOLD) / QUICK), "d" else local P = run + HOLD + QUICK local p = (t + 40) % P if p < run then return 100 * (1 - p / run), "d" end p = p - run if p < HOLD then return 0, "e" end local u = (p - HOLD) / QUICK return 100 * (1 - (1 - u) * (1 - u)), "c" end end local function cells(lv, t, st) local lit_end = CX0 for i = 1, 6 do local sx = CX0 + (i - 1) * (CW + 1) local frac = max(0, min(1, (lv - (i - 1) * 100 / 6) / (100 / 6))) local w = frac * CW local wi = floor(w) local d, h, m, l = DIM[i], HI[i], MID[i], LO[i] if wi < CW then rect(sx + wi, CY0, CW - wi, CH, d[1], d[2], d[3]) end if wi > 0 then rect(sx, CY0, wi, 2, h[1], h[2], h[3]) rect(sx, CY0 + 2, wi, 10, m[1], m[2], m[3]) rect(sx, CY0 + 12, wi, 5, l[1], l[2], l[3]) lit_end = sx + wi end local f = w - wi if f > 0.02 and wi < CW then blend_rect(sx + wi, CY0, 1, 2, h[1], h[2], h[3], f) blend_rect(sx + wi, CY0 + 2, 1, 10, m[1], m[2], m[3], f) blend_rect(sx + wi, CY0 + 12, 1, 5, l[1], l[2], l[3], f) lit_end = sx + wi + f end end return lit_end end local function flow(t, st, lit_end) for k = 0, 4 do local u = (t * 0.55 + k * 9.5) % 47 if st == "d" then u = 47 - u end local x = CX0 + u if x < lit_end - 1 then local xi = floor(x) local f = x - xi local y = CY0 + 3 + (k * 4) % 11 blend(xi, y, 255, 255, 255, 0.85 * (1 - f)) blend(xi + 1, y, 255, 255, 255, 0.85 * f) end end end local function gloss(t) blend_rect(CX0, CY0, 47, 2, 255, 255, 255, 0.14) blend_rect(CX0, CY0 + CH - 2, 47, 1, 0, 0, 0, 0.25) local sx = -12 + (t % 160) * 0.7 for r = 0, 3 do local x = floor(sx + (3 - r) * 1.5 + 0.5) local x0 = max(x, CX0) local x1 = min(x + 3, CX0 + 47) if x1 > x0 then blend_rect(x0, CY0 + r * 4, x1 - x0, 4, 255, 255, 255, 0.13) end end end function render() if not BOLT then build_icons() end local t = frame * STEP_HZ // FPS local lv, st = level(t) gfx.clear(5, 7, 14) rect(0, 57, W, 7, 14, 18, 30) rect(0, 57, W, 1, 40, 48, 72) local rr, rg, rb = 138, 150, 172 if st == "d" and lv <= 15 and (t // 10) % 2 == 0 then rr, rg, rb = 255, 60, 50 elseif st == "f" and (t // 8) % 2 == 0 then rr, rg, rb = 120, 255, 160 end rect(57, 37, 4, 11, 90, 98, 116) rect(57, 37, 1, 11, 150, 160, 180) gfx.round_rect(4, 31, 53, 23, 12, 16, 26, rr, rg, rb, 3) local lit_end = cells(lv, t, st) for i = 1, 6 do local frac = max(0, min(1, (lv - (i - 1) * 100 / 6) / (100 / 6))) if frac > 0 then local sx = CX0 + (i - 1) * (CW + 1) blend_rect(sx, 58, max(1, floor(frac * CW)), 3, BASE[i][1], BASE[i][2], BASE[i][3], 0.16) end end if st == "c" or st == "d" then flow(t, st, lit_end) end gloss(t) local pulse = 0.5 + 0.5 * sin(t * 0.18) if st == "c" or st == "f" then gfx.blit_rgb(BOLT, 3, 3, IW, IH, 255, 0, 255) if (t // 3) % 4 == 0 then blend(1 + (t // 3) % 13, 4 + (t // 5) % 22, 255, 240, 160, 0.9) end else gfx.blit_rgb(ARROW, 3 + 0, 3 + floor(2 * pulse), IW, IH, 255, 0, 255) end local s = floor(lv + 0.5) .. "%" local r, g, b = 255, 255, 255 if st == "c" then r, g, b = 150, 255, 180 elseif st == "d" and lv <= 20 then r, g, b = 255, 96, 80 end gfx.text(62 - gfx.text_width(s, FONT), 6, s, r, g, b, FONT) end