Tai
Banned User
Just some random, interesting, maybe useful functions.
Splitting a String Array:
Testing the String Prefix:
Changing the Brightness of a Given Color:
Gauss Error Function:
Inverse Gauss Error Function:
Splitting a String Array:
Lua:
function split(str, separator)
local t = {}
local fpat = "(.-)" .. separator
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
Testing the String Prefix:
Lua:
function startsWith(str, prefix)
local ret = false;
if str ~= nil and prefix ~= nil then
ret = string.sub(str,1,string.len(prefix))==prefix;
end
return ret;
end
Changing the Brightness of a Given Color:
Lua:
function gradient (color, bright)
-- brightness is giving in times
local R,G,B = color2rgb(color)
-- RGB -> HSV
local MAX = R;
local MIN = R;
if MAX < G then
MAX = G
end
if MAX < B then
MAX = B
end
if MIN > G then
MIN = G
end
if MIN > B then
MIN = B
end
local H,S,V;
if MIN == MAX then
H = 0;
elseif MAX == R and G > B then
H = 60 * (G-B) / (MAX-MIN)
elseif MAX == R and G < B then
H = 60 * (G-B) / (MAX-MIN) + 360
elseif MAX == G then
H = 60 * (B-R) / (MAX-MIN) + 120
elseif MAX == B then
H = 60 * (R-G) / (MAX-MIN) + 240
end
if MAX == 0 then
S = 0;
else
S = 1 - MIN/MAX;
end
V = MAX;
V = bright * V;
-- HSV -> RGB
local Hi = math.floor(H/60);
local f = H%60;
local p = V*(1-S);
local q = V*(1-f*S);
local t = V*(1-(1-f)*S);
if Hi == 0 then
R = V;
G = t;
B = p;
elseif Hi == 1 then
R = q;
G = V;
B = p;
elseif Hi == 2 then
R = p;
G = V;
B = t;
elseif Hi == 3 then
R = p;
G = q;
B = V;
elseif Hi == 4 then
R = t;
G = p;
B = V;
elseif Hi == 5 then
R = V;
G = p;
B = q;
end
R = math.floor(R * 255)
G = math.floor(G * 255)
B = math.floor(B * 255)
return core.rgb(R, G, B)
end
function color2rgb (color)
local R = color % 256 / 255;
local G = ((color - R) / 256) % 256 / 255;
local B = ((color - R - G*256) /(256 * 256)) % 256 / 255;
return R,G,B;
end
Gauss Error Function:
Lua:
function erf(x)
local ret = 0;
local t,z,ans;
z = x;
t = 1.0/(1.0 + 0.5*z);
ans = t * math.exp( -z * z - 1.26551223
+ t*(1.00002368 + t*(0.37409196 + t*(0.09678418
+ t*( -0.18628806 + t*(0.27886807 + t*( -1.13520398 + t*(1.48851587
+ t*( -0.82215223 + t*0.17087277)))))))));
if x >= 0.0 then
ret = ans;
else
ret = 2.0 - ans;
end
return 1.0 - ret;
end
Inverse Gauss Error Function:
Lua:
function erfinv(y)
if y > 1 or y < -1 then
return 0;
end
local a ={};
a[1] = 0.886226899;
a[2] = -1.645349621;
a[3] = 0.914624893;
a[4] = -0.140543331;
local b ={};
b[1] = -2.118377725;
b[2] = 1.442710462;
b[3] = -0.329097515;
b[4] = 0.012229801;
local c ={};
c[1] = -1.970840454;
c[2] = -1.624906493;
c[3] = 3.429567803;
c[4] = 1.641345311;
local d ={};
d[1] = 3.543889200;
d[2] = 1.637067800;
local y0 = 0.7;
local x, z;
if math.abs(y) == 1.0 then
x = -y*math.log(0.0);
elseif y < -y0 then
z = math.sqrt(-math.log((1.0+y)/2.0));
x = -(((c[4]*z+c[3])*z+c[2])*z+c[1])/((d[2]*z+d[1])*z+1.0);
elseif y<y0 then
z = y*y;
x = y*(((a[4]*z+a[3])*z+a[2])*z+a[1])/((((b[4]*z+b[4])*z+b[2])*z+b[1])*z+1.0);
else
z = math.sqrt(-math.log((1.0-y)/2.0));
x = (((c[4]*z+c[3])*z+c[2])*z+c[1])/((d[2]*z+d[1])*z+1.0);
end
--polish x to full accuracy
x = x - (erf(x) - y) / (2.0/math.sqrt(math.pi) * math.exp(-x*x));
x = x - (erf(x) - y) / (2.0/math.sqrt(math.pi) * math.exp(-x*x));
return x;
end
Last edited: