FBA The Creator
http://www.fbacreator.com/fbaforum/

New small functions
http://www.fbacreator.com/fbaforum/viewtopic.php?f=3&t=221
Page 1 of 1

Author:  Root [ Tue Sep 29, 2009 4:23 pm ]
Post subject:  New small functions

I want to quickly get image point color in RGB format.
I've made function for that:

red,green,blue = givecolor(fba) -- fba is color in 65536 mode (two bytes or 16bit)

function givecolor(fba)
return math.floor(fba/2048)*8,math.floor((fba-math.floor(fba/2048)*2048)/32)*4,((fba-math.floor(fba/2048)*2048)-math.floor((fba-math.floor(fba/2048)*2048)/32)*32)*8
end

it's like getcolor but vice versa (as you can see)
but the problem is that it takes too much time to implement something like that:

R,G,B = givecolor( imagecolor(h, x, y) )

small but too long if you want to scan image with screen size 240X320. so I would like to apply for other, faster function like imagecolor() but specified only on the shortest geting color data, I mean without choosing "post color" if "imagecolor(h, x, y, R, G, B)" or "get it" if "imagecolor(h, x, y)" and all other stuff. And if you don't mind add my function givecolor(fba) for example to your getcolor() for crossconverting e.g.

FBA = getcolor(R, G, B)
R, G, B = getcolor(FBA)

or as a separate function. Some people may need that...

Author:  Root [ Tue Sep 29, 2009 4:27 pm ]
Post subject:  Re: New small functions

And I got another interesting func.
xShift,yShift = rotate(angle)

function rotate(a)
return math.cos(a / 180 * math.pi), - math.sin(a / 180 * math.pi)
end

minus flippin' Y axis (because on screen we have it increasing in down direction)
can be used for determination of coordinates after turning on some angle. Try this to understand what I mean:
Code:
-----"main.fba"-----
local angle = 0
function onmainloop()
if 0<mousedown()then quit()end
clear(0,0,0)

line(100-3, 100, 100+3, 100, 0,127,0)-- assign
line(100, 100-3, 100, 100+3, 0,127,0)-- center

angle = angle + 1
x,y = rotate(50, angle)

x, y = 100+x, 100+y

line(x-3, y, x+3, y, 127,0,0)-- here's
line(x, y-3, x, y+3, 127,0,0)-- target
end

function rotate(r, a) -- added radius of rotation 'r'
return math.cos(a * math.pi / 180) * r, - math.sin( a * math.pi / 180) * r
end
-----end-----

allso x and y can be added.

ARC function
Based on rotate()
please add it to Fba! :roll:

arc(xc, yc, r, sa, ea, R,G,B)

xc, yc - center
sa, ea - starting angle, ending angle

Code:
-----"main.fba"-----
function onmainloop()
if 0<mousedown()then quit()end
clear(0,0,0)

line(100-3, 100, 100+3, 100, 0,127,0)-- assign
line(100, 100-3, 100, 100+3, 0,127,0)-- center

arc(100, 100, 50, 0, mousey(), 127,0,0)
print(10, 40, "Got ARC start angle: 0 deg;  end: " .. mousey() .. " deg; radius 50 pix.", 0,0,127)
end

function arc(xc, yc, r, sa, ea, R,G,B)
for angle = sa,ea,57/r do
    point(xc + math.cos(angle * math.pi / 180) * r, yc - math.sin(angle * math.pi / 180) * r, R,G,B)
end
end
-----end-----

in this way can be implemented sector() cluster() chord() with help of poly() and filled versions of them like in fillpoly().

Author:  Root [ Tue Sep 29, 2009 4:58 pm ]
Post subject:  Re: New small functions

byte (256) encoding
used in many files to store numerical info e.g. imageheight in images or bitrate in sound files and so on.

encoded = b256(number[, limit])

number - number to encode
limit - defines how many characters should have encoded string

function f256(n,limit)
local str=""
local por=(limit or string.len(n))-1
for x=0,por do
local dig=math.floor(n/256^(por-x))
n=n-dig*256^(por-x)
str=string.char(dig)..str
end
return str
end

decoding function looks like that:

decoded = unb256(str, limit)

function unb256(str, limit)
local n=0
local por=(limit or string.len(str))
for x=1,por do
n=n+string.byte(str,x)*256^(x-1)
end
return n
end

This one can be used for decoding header of files. This simplifying loading EXTERNAL images of many formats. You can even load video files .avi for example. and play it as an array of images.(unfortunaly without sound :( )

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/