1522 posts
Location
The Netherlands
Posted 12 March 2013 - 07:34 AM
Hello,
I am creating something ( Its not important what it is :P/> ) that rougly uses this function:
function someFunc( color1, color2, color3 )
-- How can I check if color1, color2, color3 is a valid color, like cyan
term.setTextColor( colors.color1 )
print( "Something" )
term.setTextColor( colors.color2 )
print( "Something again" )
term.setTextColor( colors.color3 )
print( "Something again, again" )
end
-- This is thought how I would write it, but I dont think it is efficient
function secondFunc( color1, color2, color3 )
local tColors = { "white", "orange", "magenta", } -- etc
local col1, col2, col3
for i = 1, #tColors do
if color1 == tColors[i] and not col1 then
color1 = colors. --curr color, not sure how to do this
col1 = true
end
if color2 == tColors[i] and not col2 then
color2 = colors. --curr color, not sure how to do this
col2 = true
end
if color3 == tColors[i] and not col3 then
color3 = colors. --curr color, not sure how to do this
col3 = true
end
end
if col1 and col2 and col3 then
term.setColor( col1 )
print( "Something" )
--Everything else from previous function except differnt color vars
return true
else
return false
end
end
I want to make this more efficient and I dont know how to actually set the color.
Thanks in advance,
Engineer
1190 posts
Location
RHIT
Posted 12 March 2013 - 07:39 AM
-- pass just a string color such as 'blue'
if colors[color] then
print("Color exists")
else
print("Color does not exist")
end
Alternatively you could loop through the colors table and check each value…
local function checkColor(color)
for i,v in pairs(colors) do
if v==color then return true end
end
return false
end
8543 posts
Posted 12 March 2013 - 07:43 AM
function somefunc(...)
local cols = {...}
for i =1, #cols do
cols[i] = colors[cols[i]] or colours[cols[i]]
end
return cols
end
Not sure what you're attempting to accomplish with the second function.
1522 posts
Location
The Netherlands
Posted 12 March 2013 - 07:51 AM
-- pass just a string color such as 'blue'
if colors[color] then
print("Color exists")
else
print("Color does not exist")
end
Alternatively you could loop through the colors table and check each value…
local function checkColor(color)
for i,v in pairs(colors) do
if v==color then return true end
end
return false
end
Thanks, this way I can check if the color exists. Now how do I set it to that specific color?
function somefunc(...)
local cols = {...}
for i =1, #cols do
cols[i] = colors[cols[i]] or colours[cols[i]]
end
return cols
end
Not sure what you're attempting to accomplish with the second function.
The second should be checking if the colors exist, and if only all the 3 colors exist then it should do some code involving those colors.
I now know how to check it properly, but I don't know how to set it to the color.
1604 posts
Posted 12 March 2013 - 07:58 AM
You set it the same way you did to check:
term.setBackgroundColor(colors[color]) -- color is the passed string, like "blue" or "red", etc.
You may want to check in colours too, just in case someone writes grey instead of gray or something:
local col = colors[color] or colours[color]
if not col then
error("That color doesn't exist!")
end
term.setBackgroundColor(col)
1522 posts
Location
The Netherlands
Posted 12 March 2013 - 08:30 AM
You set it the same way you did to check:
term.setBackgroundColor(colors[color]) -- color is the passed string, like "blue" or "red", etc.
You may want to check in colours too, just in case someone writes grey instead of gray or something:
local col = colors[color] or colours[color]
if not col then
error("That color doesn't exist!")
end
term.setBackgroundColor(col)
This pushed me definitely in the right direction.
Than I need the create a table like this:
local tColors = {
["gray"] = colors.gray
[""red"] = colors.red
--etc
}
Thanks a lot everyone!
8543 posts
Posted 12 March 2013 - 08:42 AM
Wait, why? What does that table accomplish that's different than using the colors table?
tColors.gray == colors.gray
tColors["gray"] == colors["gray"]
col = "gray"
tColors[col] == colors[col]
So what's the advantage?
1522 posts
Location
The Netherlands
Posted 12 March 2013 - 08:58 AM
Wait, why? What does that table accomplish that's different than using the colors table?
tColors.gray == colors.gray
tColors["gray"] == colors["gray"]
col = "gray"
tColors[col] == colors[col]
So what's the advantage?
Can you just call colors?
Wow i am such a derp :/
Thanks for pointing that out.
148 posts
Posted 12 March 2013 - 10:02 AM
Or you can do it the mathematical way:
function chkColor(color)
local exp = math.log(color, 2)
if exp == math.floor(exp) and exp <= 16 then
return true
end
return false
end
I know this is solved, but maybe someone is interesed in it…:D/>
871 posts
Posted 12 March 2013 - 11:11 AM
math.log only takes one argument, and returns the natural log, so to get the base 2 log, you have to do log(n)/log(2).
Also, log will give 0 to 15, so your upper condition test should be exp<16 or <=15, and you need to check the lower condition, exp>=0, as well.
Also also, the whole point was to validate strings describing colors, not the numeric values, so your alternative rather misses the original point
148 posts
Posted 13 March 2013 - 04:29 AM
math.log only takes one argument, and returns the natural log, so to get the base 2 log, you have to do log(n)/log(2).
math.log(x [, base])
Thats how it's written on the lua documentation, so base should be the base…
[…] and you need to check the lower condition, exp>=0, as well.
Didn't expext anyone to type a float as a color
Also also, the whole point was to validate strings describing colors, not the numeric values, so your alternative rather misses the original point
Yes, and it was already solved, too. That's what I wrote in my post. But still this may be usefull if you pass over the number of the color, you can't always use a string. (Expecially if you have combined colors and you want to filter the colors out, this is the method you want to use)
871 posts
Posted 13 March 2013 - 04:50 AM
math.log may take the base in proper lua, but it doesn't in luaj.
1604 posts
Posted 13 March 2013 - 04:54 AM
math.log(x [, base])
Thats how it's written on the lua documentation, so base should be the base…
That's for Lua 5.2, CC uses Lua 5.1. This is the
manual for 5.1, math.log just takes one argument.
8543 posts
Posted 13 March 2013 - 05:38 AM
If you filter against some out-of-range data, you should filter against all out-of-range data, within reason.