info = { type = "image", title = "Hummingbird bloom", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A hummingbird hovers at a flower and feeds", api = 33, } local tr = ({ sk = { ["Flower"] = "Kvet" }, cs = { ["Flower"] = "Květ" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bloom", label = T("Flower"), type = "color", default = "#B8409C" }, } local rect, px, blend_rect, blit_rgb, blend_mask = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb, gfx.blend_mask local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local char, unpack, concat = string.char, table.unpack, table.concat local W, H = gfx.W, gfx.H local FLAT = { 172, 202, 160 } local KEY = { 255, 0, 255 } local function lerp(a, b, t) return a + (b - a) * t end local function clamp(v, lo, hi) return v < lo and lo or (v > hi and hi or v) end local function ss(u) u = clamp(u, 0, 1) return u * u * (3 - 2 * u) end local BG = nil local BL, BM, BD = { 184, 64, 156 }, { 184, 64, 156 }, { 100, 36, 86 } local function disc(cx, cy, r, c, a) for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3], a) end end local function floret(x, y, s) if s == 2 then rect(x + 2, y, 3, 1, BD[1], BD[2], BD[3]) rect(x, y - 1, 3, 3, BM[1], BM[2], BM[3]) rect(x, y - 1, 3, 1, BL[1], BL[2], BL[3]) px(x + 2, y + 1, BD[1], BD[2], BD[3]) else rect(x, y - 1, 2, 2, BM[1], BM[2], BM[3]) px(x, y - 1, BL[1], BL[2], BL[3]) end end local STEM = { 62, 112, 56 } local function stemx(y) return floor(8 - 4 * (y / 63) + 1.2 * sin(y * 0.2) + 0.5) end local STAGES = { function() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, floor(lerp(232, 104, t) + 0.5), floor(lerp(242, 148, t) + 0.5), floor(lerp(218, 104, t) + 0.5)) end end, function() disc(52, 10, 9, { 255, 255, 240 }, 0.22) disc(52, 10, 7, { 255, 255, 240 }, 0.18) disc(32, 20, 7, { 250, 255, 236 }, 0.16) disc(58, 34, 8, { 246, 252, 230 }, 0.14) disc(44, 54, 9, { 70, 118, 74 }, 0.2) disc(26, 50, 7, { 80, 132, 84 }, 0.2) disc(60, 58, 6, { 60, 104, 66 }, 0.22) blend_rect(0, 56, W, 8, 70, 118, 74, 0.25) end, function() for y = 2, 63 do rect(stemx(y), y, 1, 1, STEM[1], STEM[2], STEM[3]) end for k = 0, 13 do local y = 4 + k * 4 local x = stemx(y) floret(x + 1, y, k < 3 and 1 or 2) if k >= 3 then floret(x - 3, y + 2, 2) end end for i = 0, 3 do local x = 3 + i * 5 floret(x, 57 - i // 2 * 1, 2) end rect(0, 58, 18, 1, STEM[1], STEM[2], STEM[3]) disc(5, 63, 9, { 40, 86, 48 }, 0.55) disc(15, 65, 7, { 36, 78, 44 }, 0.5) BG = gfx.snapshot(0, 0, W, H) end, } local STAGE = 1 function on_settings() local c = cfg.bloom or { r = 184, g = 64, b = 156 } BM = { c.r, c.g, c.b } BL = { floor(c.r + (255 - c.r) * 0.45), floor(c.g + (255 - c.g) * 0.45), floor(c.b + (255 - c.b) * 0.45) } BD = { floor(c.r * 0.55), floor(c.g * 0.55), floor(c.b * 0.55) } BG, STAGE = nil, 1 end local BW, BH = 34, 30 local HX, HY, HRX, HRY = 14.2, 9, 4.8, 4.4 local B0X, B0Y, B0R, B1X, B1Y, B1R = 15, 12, 4.6, 22, 22, 4.3 local T1X, T1Y, T1R = 27, 29, 1.2 local BK0X, BK0Y, BK1X, BK1Y = 2, 12, 10.2, 10.2 local function seg(px_, py_, ax, ay, bx, by) local dx, dy = bx - ax, by - ay local u = clamp(((px_ - ax) * dx + (py_ - ay) * dy) / (dx * dx + dy * dy), 0, 1) local qx, qy = ax + dx * u, ay + dy * u local ex, ey = px_ - qx, py_ - qy return sqrt(ex * ex + ey * ey), u end local AXL = sqrt((B1X - B0X) ^ 2 + (B1Y - B0Y) ^ 2) local AXX, AXY = (B1X - B0X) / AXL, (B1Y - B0Y) / AXL local function sample(x, y) local rem, cr, cg, cb = 1, 0, 0, 0 local function add(cov, r, g, b) if cov <= 0 then return end local w = cov * rem cr, cg, cb = cr + r * w, cg + g * w, cb + b * w rem = rem - w end local ex, ey = x - 12.4, y - 8.1 local de = sqrt(ex * ex + ey * ey) - 0.95 if de < 0.5 then local gx, gy = x - 11.9, y - 7.6 if gx * gx + gy * gy < 0.3 then add(1, 235, 240, 235) else add(clamp(0.5 - de, 0, 1), 12, 10, 14) end end if x < 11 then local d, u = seg(x, y, BK0X, BK0Y, BK1X, BK1Y) add(clamp(0.5 - (d - (0.45 + 0.4 * u)), 0, 1), 48, 36, 34) end local hx, hy = (x - HX) / HRX, (y - HY) / HRY local dh = (sqrt(hx * hx + hy * hy) - 1) * HRY if dh < 0.5 then local v = clamp((y - (HY - HRY)) / (2 * HRY), 0, 1) local r, g, b = lerp(18, 64, v), lerp(88, 156, v), lerp(80, 122, v) local th = ss((y - (HY + 0.4)) / 2.6) * ss((HX + 2.2 - x) / 3.5 + 0.2) r, g, b = lerp(r, 218, th), lerp(g, 216, th), lerp(b, 196, th) local hl = clamp(1 - sqrt((x - (HX - 1.8)) ^ 2 + (y - (HY - 2.4)) ^ 2) / 2.6, 0, 1) * 34 add(clamp(0.5 - dh, 0, 1), r + hl, g + hl, b + hl) end local d0, u0 = seg(x, y, B0X, B0Y, B1X, B1Y) local rb = lerp(B0R, B1R, u0) local db = d0 - rb if db < 0.5 then local s = clamp(((x - B0X) * (-AXY) + (y - B0Y) * AXX) / rb, -1, 1) local r, g, b if s < -0.1 then local k = ss((s + 1) / 0.9) r, g, b = lerp(48, 100, k), lerp(86, 112, k), lerp(64, 78, k) local gr = (1 - u0) * (1 - k) * 0.7 r, g, b = lerp(r, 36, gr), lerp(g, 132, gr), lerp(b, 100, gr) else local k = ss((s + 0.1) / 1.1) r, g, b = lerp(100, 220, k), lerp(112, 192, k), lerp(78, 150, k) end add(clamp(0.5 - db, 0, 1), r, g, b) end if y > 19 then local dt, ut = seg(x, y, B1X, B1Y, T1X, T1Y) local dd = dt - lerp(2.8, T1R, ut) add(clamp(0.5 - dd, 0, 1), lerp(98, 150, ut), lerp(66, 108, ut), lerp(50, 80, ut)) end return cr, cg, cb, 1 - rem end local RAS, RJOB = {}, {} local function bake_rows(p, y0, y1) local fx = (p - 1) % 2 * 0.5 local fy = (p - 1) // 2 * 0.5 local rows = RAS[p] or {} RAS[p] = rows local row = {} for y = y0, y1 do local k = 0 for x = 0, BW - 1 do local r, g, b, a = sample(x + 0.5 - fx, y + 0.5 - fy) if a < 0.12 then r, g, b = KEY[1], KEY[2], KEY[3] else r = floor(clamp((r + FLAT[1] * (1 - a)) , 0, 255) + 0.5) g = floor(clamp((g + FLAT[2] * (1 - a)) , 0, 255) + 0.5) b = floor(clamp((b + FLAT[3] * (1 - a)) , 0, 255) + 0.5) if r == 255 and g == 0 and b == 255 then g = 1 end end row[k + 1], row[k + 2], row[k + 3] = r, g, b k = k + 3 end rows[y + 1] = char(unpack(row, 1, k)) end end local MW, MH, MY0 = 34, 38, 12 local HGX, HGY, WL = 17, 11, 15 local POSE = { -1.25, -0.2, 0.95 } local MASK = {} local function bake_mask(p, j0, j1) local rows = MASK[p] or {} MASK[p] = rows local row = {} local a0 = POSE[p] for j = j0, j1 do local y = j + 0.5 - MY0 for i = 0, MW - 1 do local x = i + 0.5 local best = 0 for k = -1, 1 do local a = a0 + k * 0.3 local dx, dy = cos(a), sin(a) local rx, ry = x - HGX, y - HGY local s = (rx * dx + ry * dy) / WL if s > 0 and s < 1.08 then local perp = abs(-rx * dy + ry * dx) local hw = 3.6 * (s < 0.5 and (0.4 + 1.2 * s) or (1 - 3.6 * (s - 0.5) ^ 2)) local v = clamp((hw - perp) / 1.8, 0, 1) * (k == 0 and 1 or 0.5) * (1 - 0.35 * s) if v > best then best = v end end end row[i + 1] = floor(best * 255 + 0.5) end rows[j + 1] = char(unpack(row, 1, MW)) end end local JOBS = {} for p = 1, 4 do for c = 0, 4 do JOBS[#JOBS + 1] = { 1, p, c * 6, c * 6 + 5 } end end for p = 1, 3 do for c = 0, 3 do JOBS[#JOBS + 1] = { 2, p, c * 10, math.min(MH - 1, c * 10 + 9) } end end local JOB = 1 local RASTER, MASKS = {}, {} local LOOP = 650 local FA, FB, FC = { 12.5, 25 }, { 12, 14 }, { 12.5, 40 } local TAU = 6.28318 local function feed(t0, ax, ay, tc) local n = 0.5 - 0.5 * cos(TAU * (tc - t0) / 45) return ax + 4.6 - 4.2 * n, ay + 0.8 * sin(tc * 0.06) end local function bird_at(tc) if tc >= 28 and tc < 60 then local u = (tc - 28) / 32 local e = 1 - (1 - u) ^ 3 return lerp(FA[1] + 75, FA[1] + 4.6, e), lerp(FA[2] - 16, FA[2], e) elseif tc >= 60 and tc < 150 then return feed(60, FA[1], FA[2], tc) elseif tc >= 150 and tc < 180 then local e = ss((tc - 150) / 30) return lerp(FA[1] + 4.6, FB[1] + 4.6, e), lerp(FA[2], FB[2], e) + 0.8 * sin(tc * 0.06) elseif tc >= 180 and tc < 270 then return feed(180, FB[1], FB[2], tc) elseif tc >= 270 and tc < 300 then local e = ss((tc - 270) / 30) return FB[1] + 4.6 + 6 * e, FB[2] + 0.8 * sin(tc * 0.06) elseif tc >= 300 and tc < 345 then local u = (tc - 300) / 45 return FB[1] + 10.6 + 110 * u * u, FB[2] - 26 * u * u elseif tc >= 420 and tc < 452 then local u = (tc - 420) / 32 local e = 1 - (1 - u) ^ 3 return lerp(FC[1] + 75, FC[1] + 4.6, e), lerp(FC[2] + 22, FC[2], e) elseif tc >= 452 and tc < 560 then return feed(452, FC[1], FC[2], tc) elseif tc >= 560 and tc < 595 then local u = (tc - 560) / 35 return FC[1] + 4.6 + 100 * u * u, FC[2] + 30 * u * u end end function render() if not BG then STAGES[STAGE]() STAGE = STAGE + 1 if not BG then return end end local job = JOBS[JOB] if job then if job[1] == 1 then bake_rows(job[2], job[3], job[4]) else bake_mask(job[2], job[3], job[4]) end JOB = JOB + 1 if not JOBS[JOB] then for i = 1, 4 do RASTER[i] = concat(RAS[i]) end for i = 1, 3 do MASKS[i] = concat(MASK[i]) end RAS, MASK = nil, nil end end blit_rgb(BG, 0, 0, W, H) if JOBS[JOB] then return end local t = frame local bx, by = bird_at(t % LOOP) if not bx then return end local qx, qy = floor(bx * 2 + 0.5) / 2, floor(by * 2 + 0.5) / 2 local ix, iy = floor(qx), floor(qy) local p = (qx > ix and 2 or 1) + (qy > iy and 2 or 0) local ox, oy = ix - 2, iy - 12 local pose = t % 3 + 1 local far = (pose % 3) + 1 blend_mask(MASKS[far], 120, 86, 62, ox, oy - MY0, MW, MH, MW, MH, 0.4) blit_rgb(RASTER[p], ox, oy, BW, BH, KEY[1], KEY[2], KEY[3]) blend_mask(MASKS[pose], 150, 104, 70, ox, oy - MY0, MW, MH, MW, MH, 0.72) end