fixed sum stuff

This commit is contained in:
2025-12-23 12:46:47 -08:00
parent 9df738465a
commit aa64d18060
9 changed files with 15514 additions and 20 deletions

View File

@@ -1323,7 +1323,18 @@ function lib.getENV(fspath, term_override, perms)
global.network = network
end
if app and perms.app then
global.app = app
global.app = deepcopy(app)
global.app.launch = function(id,app_perms) app.launch(id,app_perms,perms) end
end
if global and not perms.peripheral then
global.peripheral.wrap = function () end
global.peripheral.find = function () end
global.peripheral.getNames = function () return {} end
global.peripheral.getType = function () end
global.peripheral.isPresent = function () return false end
global.peripheral.hasType = function () end
global.peripheral.call = function () end
global.peripheral.getMethods = function () end
end
global.settings = settings
global._ENV = global

View File

@@ -7,6 +7,60 @@ local function add(t, v)
end
end
end
local function normalize_color(c)
if type(c) ~= "number" then error("bad color", 2) end
if c < 1 then error("bad color", 2) end
-- If c is already a power of two, this keeps it.
-- If it's a mask, this picks the highest power-of-two <= c.
local p = 1
while p * 2 <= c do p = p * 2 end
return p
end
-- Returns: bestColor, bestCount, totalCells, countsTable
-- countsTable maps colorBit -> count
local function most_common_bg(win, opts)
opts = opts or {}
local w, h
if win.getSize then
w, h = win.getSize()
else
w, h = win.w, win.h
end
local ignore = opts.ignore
if ignore ~= nil then ignore = normalize_color(ignore) end
local counts = {}
local total = 0
for x = 1, w do
local col = win.buffer and win.buffer[x]
for y = 1, h do
local cell = col and col[y]
local bc = cell and cell.bc or win.bgColor or colors.black
bc = normalize_color(bc)
if not ignore or bc ~= ignore then
counts[bc] = (counts[bc] or 0) + 1
total = total + 1
end
end
end
local fallback = normalize_color(opts.fallback or win.bgColor or colors.black)
local best, bestN = fallback, -1
for c, n in pairs(counts) do
if n > bestN or (n == bestN and c == fallback) then
best, bestN = c, n
end
end
return best, bestN, total, counts
end
-- hex <-> color bit lookups (Lua 5.1 safe)
local HEX = "0123456789abcdef"
@@ -64,6 +118,11 @@ function lib.create(name, w, h, x, y)
closing = false,
_palette = {}, -- optional local palette store
}
function t.getMostCommonBackgroundColor(opts)
local c, n, total, counts = most_common_bg(t, opts)
return c, color_to_hex[c], n, total, counts
end
local function init_col(xi)
local w = t.w