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

Help me understand "colors.subtract" command please.

Started by mayaman79, 25 August 2012 - 02:23 PM
mayaman79 #1
Posted 25 August 2012 - 04:23 PM
easy little code:


rs.setBundledOutput("back", colors.combine(colors.red, colors.blue, colors.black))

sweet, that sounds good, combining colors, giving out signals to colors mentioned. awesome!

so we do some colors.subtract then. Code:

rs.setBudnledoutput("back", colors.subtract(colors.red, colors.blue))


Now i would from the "logic" of this, think that i took away the signal to red and blue. but my computer shuts down the black (!!) one?


How does subtracting mean keeping whatever you don't define?

This does not make any sense to me…. help?
Lyqyd #2
Posted 25 August 2012 - 11:16 PM
Colors.subtract is meant to remove a color from an existing group of colors. You should read the API documentation on the wiki.
Lettuce #3
Posted 25 August 2012 - 11:17 PM
Redpower cables are given binary values. That's what (I believe) you are subtracting. If you know binary, 1, 2, 4, 8, 16, 32, 64… then you know what binary values you are subtracting. You can get the bit order by typing "help redpower" then "help colors" in the computer. white is on the bottom, 1 bit. black is on top, 32 thousand-something, I forget exactly. Count from white and write them down so you know what you're doing in the future.

I learned this from "FunshineX's" tutorials. Look him up on youtube, he is called "rondouglas" and you will get much more details from him.
immibis #4
Posted 26 August 2012 - 12:36 PM
Whenever you call rs.setBundledOutput you set the state of ALL colours in that cable.
All the ones you specify are on.
All the ones you don't specify are off.

rs.setBundledOutput("left", colors.combine(colors.black, colors.red, colors.blue)) – Turns on black, red, and blue, *and turns everything else off*
rs.setBundledOutput("right", colors.subtract(colors.red, colors.blue)) – Turns on ???, *and turns everything else off*

What is the "???"? For that, look at what colors.subtract does. It takes two arguments (call the first A and the second :D/>/> and returns all the colours that are in A but not in B. For example colors.subtract(colors.combine(colors.black, colors.red, colors.blue), colors.red) == colors.combine(colors.black, colors.blue).
In your example, it returns all the colors that are red but not blue. So it returns red. And then you give that to rs.setBundledOutput which turns red on and everything else off.