587 posts
Location
Wrocław, Poland
Posted 13 August 2013 - 09:40 AM
TermPlus
Terminal API Extended!
Hi all, this is API that I've wrote long time ago. It might be useful to someone, so here it is:
Main function of this API is to read from screen.
Functions:
term.getPixel(x, y)
Returns stored char, text color and background color
term.getBackgroundColor()
Returns background color
term.getTextColor()
Returns text color
color.isValid(color)
Returns true if color code is valid color, else returns false
color.isSingle(color)
Returns true is color is single color (2^n where 0 <= n < 16)
term.unloadPlusAPI()
Unloads TermPlus API. os.unloadAPI() will not do it!
There is British namespace (colour) in API.
How to load and unload this API:
--# Load:
os.loadAPI("termplus")
--# Unload:
term.unloadPlusAPI()
os.unloadAPI("termplus")
Download:
Screen:
1852 posts
Location
Sweden
Posted 13 August 2013 - 09:46 AM
Really nice! :)/>
I've been thinking of actually making somethinh like this myself but meh.. I'm too lazy :P/>
Great job, This can certainly be very useful ;)/>
7508 posts
Location
Australia
Posted 13 August 2013 - 09:49 AM
A valid (single) colour should be either:
2^n | 0 =< n =< 15
OR
2^(n-1) | -1 < n < 16
NOT
2^n | 0 < n < 16
As this has a range of 2^1—2^15 which is missing the first number 2^0.
EDIT: Also a valid colour (for use with cables only, since terminals can only show 1 at a time) is:
2^0—((2^16)-1)
587 posts
Location
Wrocław, Poland
Posted 13 August 2013 - 09:51 AM
Right, I derped, I'm sorry :(/>
Function in code works anyway since it is:
colors.isSingle = function(color)
for _,v in pairs(colors) do
if color == v and type(v) == "number" then
return true
end
end
return false
end
Really nice! :)/>
I've been thinking of actually making somethinh like this myself but meh.. I'm too lazy :P/>
Great job, This can certainly be very useful ;)/>
Thanks! :D/>
7508 posts
Location
Australia
Posted 13 August 2013 - 10:18 AM
Btw, forgot to mention… nice api :)/>
Right, I derped, I'm sorry :(/>
That's ok, just thought I'd point it out :P/>
Function in code works anyway since it is:
colors.isSingle = function(color)
for _,v in pairs(colors) do
if color == v and type(v) == "number" then
return true
end
end
return false
end
Just as a point of optimising…
Since
log2(n) is the inverse of
2n we can modify this function to the following, more computationally efficient (since there's no loop), code.
colors.isSingle = function( n )
local pow = math.log(n) / math.log(2)
return pow % 1 == 0 and pow >= 0 and pow <= 15
end
587 posts
Location
Wrocław, Poland
Posted 13 August 2013 - 10:21 AM
Never really messed with log (I know as much as I've learned by myself) so used this method :P/>
7508 posts
Location
Australia
Posted 13 August 2013 - 10:25 AM
Never really messed with log (I know as much as I've learned by myself) so used this method :P/>
The main thing that I know/use it for is to reverse the
logn operations… for example
10n is reversed with
log10(n), 4n is reversed with
log4(n) etc, etc, etc…
8543 posts
Posted 13 August 2013 - 01:02 PM
Also, the color setting functions will eat any number in range just fine (for instance, they're okay with 3 or 7 or 129), and will simply set the color to the highest true bit.
587 posts
Location
Wrocław, Poland
Posted 13 August 2013 - 03:35 PM
Lyqyd, I know it, forgot to change it back (I wrote this API for screen recorded which then changed color code to hex code). Will fix it in next release.
8543 posts
Posted 13 August 2013 - 04:37 PM
Perhaps you misunderstood: the built-in term color-setting functions will happily consume any integer value between 1 and 32768 (and probably 65535), inclusive. They will take the value you pass and set the relevant color to the highest true bit in the number you passed it. So your color validity function should reflect whichever values the color-setting functions will actually consume as usable values, not just the powers of two from zero to fifteen.
587 posts
Location
Wrocław, Poland
Posted 13 August 2013 - 05:22 PM
Yeah, I meant exactly this, but for compression sake I turned colors into color codes in video file. As I've said - this API was written for screen recorder :)/>
It will accept up to 216-1
7508 posts
Location
Australia
Posted 13 August 2013 - 09:09 PM
the built-in term color-setting functions will happily consume any integer value between 1 and 32768 (and probably 65535), inclusive.
Indeed it accepts up to 65535.
They will take the value you pass and set the relevant color to the highest true bit in the number you passed it.
Due to the fact that they cast a Double to an int, this can be replicated in Lua like so (easiest method):
local function parseColor(c)
local pow = math.floor(math.log(c) / math.log(2))
return 2^pow
end
Yeah, I meant exactly this, but for compression sake I turned colors into color codes in video file.
How are valid colours any more compressed?! At best they are 1 bit shorter.
467 posts
Location
Van Diemen's Land
Posted 14 August 2013 - 04:08 AM
Wow. How did you make it return pixels?? :o/>
587 posts
Location
Wrocław, Poland
Posted 14 August 2013 - 06:57 AM
When you change anything on screen I save it to the buffer. Returning pixels is just reading from buffer.
How are valid colours any more compressed?! At best they are 1 bit shorter.
I used same color codes as in cPrint (0 - white, 1 - something, …, a - something, f - black) so instead of writing "32768" to file I just write "f" :)/>
7508 posts
Location
Australia
Posted 14 August 2013 - 01:39 PM
I used same color codes as in cPrint (0 - white, 1 - something, …, a - something, f - black) so instead of writing "32768" to file I just write "f" :)/>
Ahh I see, so you just change to hex… so really you're changing (at best) a 2 byte number/colour into a single byte. Requiring 2 bytes for text and background colours for a single pixel. Gotcha.
587 posts
Location
Wrocław, Poland
Posted 14 August 2013 - 04:38 PM
Yup :)/>