From 5fa756996028fc7ce6be943f3e47c10231dbed0d Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 6 Aug 2022 22:56:33 +0100 Subject: img --- img/shitty_disp.lua | 116 ++-------------------------------------------------- 1 file changed, 3 insertions(+), 113 deletions(-) (limited to 'img/shitty_disp.lua') diff --git a/img/shitty_disp.lua b/img/shitty_disp.lua index 8bd1b61..ea9681b 100644 --- a/img/shitty_disp.lua +++ b/img/shitty_disp.lua @@ -1,116 +1,6 @@ -- bad displayment thing - -local function color_dist(c1,c2) - local total = 0 - for i=1,3 do total = total + (c1[i]-c2[i])^2 end - return total -end - -local function closest_palette_color(R,c) - local closest = nil - local closest_dist = 99999999 - for i=0,15 do - local pc = {R.getPaletteColor(2^i)} - local dist = color_dist(pc,c) - if dist < closest_dist then - closest = 2^i - closest_dist = dist - end - end - return closest -end - -local function average_color(pxlist) - local count = #pxlist - local tr,tg,tb = 0,0,0 - for _,px in ipairs(pxlist) do - tr = tr + px[1] - tg = tg + px[2] - tb = tb + px[3] - end - return {tr/count,tg/count,tb/count} -end - -local function sortby_r(pxlist) table.sort(pxlist,function(a,b) return a[1] rangeg then - if ranger > rangeb then - -- red - sortby_r(pxlist) - else - -- blue - sortby_b(pxlist) - end - else - if rangeg > rangeb then - -- green - sortby_g(pxlist) - else - -- blue - sortby_b(pxlist) - end - end - local count = #pxlist - local hcount = math.floor(count/2) - - local lhalf,uhalf = {},{} - for i=1,hcount do - lhalf[i] = pxlist[i] - end - for i=hcount+1,count do - uhalf[i-hcount] = pxlist[i] - end - - local lcols = median_cut(lhalf,remaining-1) - local ucols = median_cut(uhalf,remaining-1) - - for i=1,#ucols do table.insert(lcols,ucols[i]) end - return lcols -end - -local function reset_palette(R) - for i=0,15 do - local cx = 2^i - R.setPaletteColor(cx,term.nativePaletteColor(cx)) - end -end - -local function improve_palette(R,img,w,h,imgx,imgy) - local pxlist = {} - local idx=1 - for px=imgx,imgx+w-1 do - for py=imgy,imgy+h-1 do - pxlist[idx] = img[py][px] - idx = idx + 1 - end - end - local palette = median_cut(pxlist) - assert(#palette==16,#palette.." doesn't equal 16") - for i=0,15 do - local pc = palette[i+1] - R.setPaletteColor(2^i,table.unpack(pc)) - end -end - +local disp_common = require("disp_common") +local improve_palette,closest_palette_color = disp_common.improve_palette,disp_common.closest_palette_color local function disp(R,img,scrx,scry,w,h,imgx,imgy) @@ -134,4 +24,4 @@ local function disp(R,img,scrx,scry,w,h,imgx,imgy) end end -return {disp=disp,color_dist=color_dist,closest_palette_color=closest_palette_color,reset_palette=reset_palette} +return {disp=disp} -- cgit v1.2.3