info = { type = "app", title = "Neon clock", author = "BRRRO", license = "MIT", desc = "Time as a neon sign over a rainy city", category = "time", api = 41, } local T = ({ sk = { time = "Farba času", date = "Farba dátumu", col = { "ružová", "modrá", "zelená", "žltá", "oranžová" } }, cs = { time = "Barva času", date = "Barva data", col = { "růžová", "modrá", "zelená", "žlutá", "oranžová" } } })[lang] or { time = "Time colour", date = "Date colour", col = { "pink", "blue", "green", "yellow", "orange" } } local KEYS = { "pink", "blue", "green", "yellow", "orange" } settings = { { key = "time", type = "int", default = "blue", label = T.time, opts = { keys = KEYS, table.unpack(T.col) } }, { key = "date", type = "int", default = "pink", label = T.date, opts = { keys = KEYS, table.unpack(T.col) } }, } local W, H = gfx.W, gfx.H local clear, px, blend, rect, blit_rgb = gfx.clear, gfx.px, gfx.blend, gfx.rect, gfx.blit_rgb local text, text_center, text_width = gfx.text, gfx.text_center, gfx.text_width local floor, sin = math.floor, math.sin local hash32, umod = math.hash32, math.umod local fmt, find = string.format, string.find local function lround(v) if v >= 0 then return floor(v + 0.5) else return -floor(-v + 0.5) end end local function mix(ar, ag, ab, br, bg, bb, t) return floor(ar + (br - ar) * t), floor(ag + (bg - ag) * t), floor(ab + (bb - ab) * t) end local DAYS = ({ sk = { "NEDEĽA", "PONDELOK", "UTOROK", "STREDA", "ŠTVRTOK", "PIATOK", "SOBOTA" }, cs = { "NEDĚLE", "PONDĚLÍ", "ÚTERÝ", "STŘEDA", "ČTVRTEK", "PÁTEK", "SOBOTA" }, })[lang] or { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" } local function win_at(len) local t = data.time return t.unix // len, t.unix % len + t.ms / 1000 end local N_TOP, N_BOT = { 6, 2, 22 }, { 72, 16, 98 } local N_REF = 38 local N_FAR = { { 0, 9, 22 }, { 8, 7, 30 }, { 16, 10, 19 }, { 27, 8, 32 }, { 36, 11, 21 }, { 48, 7, 28 }, { 56, 8, 22 } } local N_NEAR = { { 0, 6, 14 }, { 6, 8, 20 }, { 15, 6, 12 }, { 22, 9, 25 }, { 33, 7, 16 }, { 41, 10, 22 }, { 52, 5, 13 }, { 57, 7, 19 } } local N_WIN = { { 0, 200, 230 }, { 230, 40, 170 }, { 255, 170, 60 } } local NEON = { pink = { { 255, 30, 160 }, { 255, 205, 240 }, { 30, 4, 24 }, { 80, 26, 64 } }, blue = { { 0, 170, 255 }, { 205, 250, 255 }, { 0, 16, 30 }, { 24, 60, 84 } }, green = { { 30, 255, 90 }, { 215, 255, 225 }, { 4, 28, 10 }, { 30, 76, 40 } }, yellow = { { 255, 210, 0 }, { 255, 248, 200 }, { 30, 24, 0 }, { 84, 72, 24 } }, orange = { { 255, 100, 0 }, { 255, 222, 190 }, { 30, 10, 0 }, { 84, 40, 16 } }, } local RAIN_C = { 120, 140, 215 } local HALO1 = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } } local HALO2 = { { -2, 0 }, { 2, 0 }, { 0, -2 }, { 0, 2 }, { -1, -1 }, { 1, -1 }, { -1, 1 }, { 1, 1 } } local O_NT = { font = "12x24", tight = false, hide = nil, alpha = 1, colors = nil } local O_ND = { font = "4x6", alpha = 1 } local N_BG = nil local function build_neon_bg() for y = 0, N_REF - 1 do local r, g, b = mix(N_TOP[1], N_TOP[2], N_TOP[3], N_BOT[1], N_BOT[2], N_BOT[3], (y / (N_REF - 1)) ^ 1.6) rect(0, y, W, 1, r, g, b) end for _, b in ipairs(N_FAR) do rect(b[1], N_REF - b[3], b[2], b[3], 40, 14, 66) end for _, b in ipairs(N_NEAR) do rect(b[1], N_REF - b[3], b[2], b[3], 18, 7, 38) for y = N_REF - b[3], N_REF - 1 do if y % 3 == 1 then for x = b[1] + 1, math.min(W - 1, b[1] + b[2] - 2) do if x % 2 == 1 then local h = hash32(y * W + x) if umod(h, 7) < 2 then local w = N_WIN[umod(h >> 4, 3) + 1] px(x, y, mix(18, 7, 38, w[1], w[2], w[3], 0.45)) end end end end end end N_BG = gfx.snapshot(0, 0, W, N_REF) end local GC, CC = {}, {} local function neon_text(x, y, s, o, n, off_at, wide) for i = 1, #s do if i == off_at then GC[i], CC[i] = n[3], n[4] else GC[i], CC[i] = n[1], n[2] end end for i = #s + 1, #GC do GC[i], CC[i] = nil, nil end o.colors = GC o.alpha = 0.10 if wide then for _, d in ipairs(HALO2) do text(x + d[1], y + d[2], s, 0, 0, 0, o) end end o.alpha = 0.32 for _, d in ipairs(HALO1) do text(x + d[1], y + d[2], s, 0, 0, 0, o) end o.colors, o.alpha = CC, 1 text(x, y, s, 0, 0, 0, o) end local function flicker_at(hm, tday) local win, sec = win_at(8) local h = hash32(win * 0x9E3779B1 + 7) if umod(h, 3) ~= 0 then return nil end local s0 = umod(h >> 6, 60) / 10 if sec < s0 or sec > s0 + 1.1 then return nil end if umod(hash32(floor(tday * 16) + h), 4) == 0 then return nil end local p = umod(h >> 12, #hm) + 1 if hm:sub(p, p) == ":" then p = p + 1 end return p end local N_CACHE, N_CN = {}, 0 local function neon_raster(key, draw_sign) local c = N_CACHE[key] if c then return c end if N_CN >= 4 then N_CACHE, N_CN = {}, 0 end blit_rgb(N_BG, 0, 0, W, N_REF) draw_sign() local up, RW, rows = gfx.snapshot(0, 0, W, N_REF), W * 3, {} for k = 0, H - 1 - N_REF do local src = N_REF - 1 - floor(k * 1.35) blit_rgb(up:sub(src * RW + 1, (src + 1) * RW), 0, N_REF + k, W, 1) local a = 0.42 - k * 0.011 gfx.dim_rect(0, N_REF + k, W, 1, a * 0.85, a * 0.8, a) rows[k + 1] = gfx.snapshot(0, N_REF + k, W, 1) end c = { up = up, rows = rows } N_CACHE[key], N_CN = c, N_CN + 1 return c end local RAIN_N = 16 local function neon_rain(tday) for i = 0, RAIN_N - 1 do local h = hash32(i * 0x9E3779B1 + 101) local land = N_REF + 2 + umod(h >> 8, H - N_REF - 3) local span = land + 9 local s = tday * (44 + umod(h, 26)) / span + umod(h >> 16, 1000) / 1000 local n = floor(s) local y = floor((s - n) * span) - 3 local x = umod(hash32(h + n * 0x85EBCA6B), W + 10) - 2 if y < land then for j = 0, 2 do local py = y - j if py >= 0 then blend(x - py // 5, py, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.5 - j * 0.15) end end elseif y < land + 6 then local e, sx = y - land, x - land // 5 blend(sx - 1 - e // 2, land, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.45 - e * 0.07) blend(sx + 1 + e // 2, land, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.45 - e * 0.07) end end end function render() clear(0, 0, 0) local t = data.time if not (t and t.ok) then text_center(30, "LOADING", 255, 205, 240) return end if not N_BG then build_neon_bg() end local tday = (t.h * 60 + t.m) * 60 + t.s + t.ms / 1000 local hm if t.fmt12 then hm = fmt("%d:%02d", t.h12, t.m) else hm = fmt("%02d:%02d", t.h, t.m) end local hide = (t.s % 2 == 1) and find(hm, ":", 1, true) or nil local off = flicker_at(hm, tday) local d = DAYS[(t.wday % 7) + 1] .. fmt(" %d.%d", t.day, t.month) if t.fmt12 then d = (t.pm and "PM " or "AM ") .. d end local nt, nd = NEON[cfg.time] or NEON.blue, NEON[cfg.date] or NEON.pink local c = neon_raster(fmt("%s|%s|%s|%s|%s|%s", hm, hide, off, d, cfg.time, cfg.date), function() O_NT.hide = hide neon_text((W - text_width(hm, O_NT)) // 2, 12, hm, O_NT, nt, off, true) neon_text((W - text_width(d, O_ND)) // 2, 3, d, O_ND, nd) end) blit_rgb(c.up, 0, 0, W, N_REF) for k = 1, #c.rows do blit_rgb(c.rows[k], lround(sin(tday * 2.6 + k * 0.8) * (0.3 + k / 16)), N_REF + k - 1, W, 1) end rect(0, N_REF, W, 1, 22, 8, 34) neon_rain(tday) end