fixed sum stuff
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user