George Larsen (Flynn)
George Larson (Flynn)

|
Hi!  These are a couple of color averaging scrips I wrote, and that I'd like to share.
Controls:
M: Color map
D: Int slider
Method One:
Recursive averaging
D dictates how many times the image gets slit into quarters. So, for instance: 1 means it will just sample the center of the image and return that.
2 means it will split the image into quarters and return the average of the centers of those quarters.
3 means it will split those quarters into MORE quarters, and measure all of the centers of those.
This goes on until the max value of ten, where, at which point, you are sampling 3.5 hundred thousand points
All those samples are added up and then averaged. Usually I keep it on D = 5. The code is:
Code |
---|
local cr, cg, cb, ca;
local beenDone;
local dd;
function prepare()
beenDone = -1;
dd = get_intslider_input(D);
end;
function get_sample(x, y)
local cols = {};
return doAvg(dd);
end;
function doAvg(detail)
if (beenDone < 0) then
local r,g,b,a,c = getAvg(0, detail, 0,1,0,1);
cr, cg, cb, ca = r/c, g/c, b/c, a/c;
beenDone = 1;
end
return cr, cg, cb, ca;
end;
function getAvg(level, mlevel, minx, maxx, miny, maxy)
local midx = midp(minx,maxx);
local midy = midp(miny,maxy);
local c1 = 0;
local r1, g1, b1, a1 = get_sample_map(midx,midy,M);
local r, g, b, a, c = 0, 0, 0, 0, 0;
r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + 1;
if (level ~= mlevel) then
r1, g1, b1, a1, c1 = getAvg(level+1, mlevel, minx, midx, miny, midy);
r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + c1;
r1, g1, b1, a1, c1 = getAvg(level+1, mlevel, midx, maxx, miny, midy);
r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + c1;
r1, g1, b1, a1, c1 = getAvg(level+1, mlevel, minx, midx, midy, maxy);
r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + c1;
r1, g1, b1, a1, c1 = getAvg(level+1, mlevel, midx, maxx, midy, maxy);
r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + c1;
end;
return r, g, b, a, c;
end;
function midp(minn,maxn)
return ((maxn-minn)/2)+minn;
end;
function merget(a,b)
for k,v in pairs(b) do a[k] = v end
end; |
Method Two:
This method is simple, faster, but far less reliable for results. It samples D number of random points on the image, and averages those. I recommend keeping d at around 1000 for best results.
The code is:
Code |
---|
local pts = {};
local beenDone;
function prepare()
beenDone = -1;
dd = get_intslider_input(D);
for i = 0, dd*2, 2 do
pts[i] = math.random();
pts[i+1] = math.random();
end;
end;
function get_sample(x, y)
return doAvg(dd);
end;
function doAvg(detail)
if (beenDone < 0) then
local r, g, b, a = 0, 0, 0, 1;
for i = 0, dd * 2, 2 do
local r1, b1, g1, a1 = get_sample_map(pts[i], pts[i+1], M);
r, g, b, a = r + r1, g + g1, b + b1, a + a1;
end;
beenDone = 1;
cr, cg, cb, ca = r/dd, g/dd, b/dd, a/dd;
end;
return cr, cg, cb, ca;
end;
|
Either of these methods are only run ONCE, so it is very scalable, and shouldn't lag at all for decent detail/accuracy.
Personally, I recommend method 2. Simplicity is beauty
|