This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darkhog's profile picture

Little improvement for colors api

Started by darkhog, 03 June 2012 - 02:24 PM
darkhog #1
Posted 03 June 2012 - 04:24 PM
I've found following function written by me very useful when transmiting chunks of data to my redpower devices:
function colorsFromString(colors)
  local r = 0
  for i=1, 16 do
    local c = colors:sub(i,i)
    if not c~=nil then
	  if c =="1" then
	    r = bit.bor(r,math.pow(2,i-1))
	  end
    end
  end
  return r
end

What it does is to convert string passed to it (which is binary 16bit number) to redpower output. Colors are in RP2 order, i.e. white, orange, magenta, lightblue, etc.). Colors.combine is nice, but not when you have to enable many colored wires at once. Then your program looks clutered, unless you use function like this.

Could it be included in next version as colors.fromString (along with toString counterpart - I didn't write it, because I don't need it, but it may be useful for others).
MysticT #2
Posted 03 June 2012 - 04:47 PM
If you want it to send the data over rednet, it's easier to send the number with the combined colors, like:

local c = colors.combine(colors.red, colors.blue, colors.white, colors.black)
rednet.broadcast(tostring(c))
and then you receive it like:

local id, msg = rednet.receive()
local c = tonumber(msg)
if c then
  rs.setBundledOutput("back", c)
end
And the message would be even shorter, since the maximum number you can send is 65535, wich is only 5 characters instead of 16.
my_hat_stinks #3
Posted 03 June 2012 - 04:55 PM
colors.combine is unnecessary, imo
All the colors are arranged so they're a single bit, spread across 2 bytes, and colors.combine just performs a bitwise or

Meaning, it's equally viable to just add colours together, and you'd get the same result :)/>/>
MysticT #4
Posted 03 June 2012 - 05:00 PM
colors.combine is unnecessary, imo
All the colors are arranged so they're a single bit, spread across 2 bytes, and colors.combine just performs a bitwise or

Meaning, it's equally viable to just add colours together, and you'd get the same result :)/>/>
That's true only if you combine single colors. If you want to combine a variable with some colors to add some more, the addition would mess up the combination if a color is already on.
Example:

local c = colors.red + colors.white
-- some code here
c = c + colors.white -- wrong
c = colors.combine(c, colors.white) -- you need colors.combine
In simple programs you will know what colors are on, but if you have something a little more complex you could not know what colors are on, and doing a simple addition could mess you program.
darkhog #5
Posted 03 June 2012 - 08:10 PM
Yeah, colors.combine is nice, but only when you have to set limited numbers of cables (like red and blue, for example). With more than few colors (like when you have to turn at once all colors, but red and blue), it is more feasible to use my function, as code written with it takes less space.

I plan also to write function that will turn on or off specific color cable without touching anything else.
my_hat_stinks #6
Posted 04 June 2012 - 03:22 PM
That's true only if you combine single colors. If you want to combine a variable with some colors to add some more, the addition would mess up the combination if a color is already on.
Example:

local c = colors.red + colors.white
-- some code here
c = c + colors.white -- wrong
c = colors.combine(c, colors.white) -- you need colors.combine
In simple programs you will know what colors are on, but if you have something a little more complex you could not know what colors are on, and doing a simple addition could mess you program.

Hm… Yes, you're right

I've been using it as a data transfer, so all the colours change at once and the full thing needs to be re-calculated every time anyway :)/>/>