Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Takes a color sum like (colors.white + colors.orange) and a boolean
- -- (which is 3)
- --
- --returns t={white=state, orange=state}
- function colors.decompose(color, state)
- if type(color) ~= "number" or color < 0 then
- return false
- end
- if color == 0 then
- return {}
- end
- if state == nil then
- state = true
- end
- local colorsName = {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
- local a = color
- local i = 1
- local t = {}
- --convert an integer to binary
- --Method: Do an euclidian division over and over again on the color
- --check every time if there is a rest
- --if there is indeed a rest, it means that the bit is true and that the corresponding color is in the sum
- while a > 0 do
- -- if a/2 has a rest then
- if a%2 == 1 then
- t[colorsName[i]] = state
- end
- a = math.floor(a/2)
- i = i+1
- end
- return t
- end
- --Merges two tables
- --t2 takes priority
- function table.merge(t1, t2)
- local tn = {}
- for i,v in pairs(t1) do
- tn[i] = v
- end
- for i,v in pairs(t2) do
- tn[i] = v
- end
- return tn
- end
- function newSetBundledOutput(side, composedColor, state)
- --Example: composedColors = colors.white + colors.orange
- -- that's 3. Because 1 + 2 = 3
- if not tonumber(composedColor) then
- error("Expected string, number, bool. Got string, string")
- end
- local newOutputs = colors.decompose(composedColor, state)
- -- newOutputs = {white=state, orange=state}
- local currentOutputs = colors.decompose(rs.getBundledOutput(side))
- --We are currently outputting to the front white cable.
- -- currentOutputs = {white=true}
- local outputs = table.merge(currentOutputs, newOutputs)
- --outputs = {white=state, orange=state}
- local colorValue = 0
- for i, v in pairs(outputs) do
- if v == true then
- colorValue = colorValue + colors[i]
- end
- end
- --if state == true,
- -- colorValue = colors.white + colors.orange
- --else,
- -- colorValue = 0
- rs.setBundledOutput(side, colorValue)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement