Skip to content

Commit

Permalink
Reverted auto application of layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Smith-Heisters committed Aug 20, 2009
1 parent 283bc22 commit e38d3cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/layers.rb
Expand Up @@ -7,6 +7,19 @@ def initialize count, universe
super(count){|i|Rdmx::Layer.new(self)}
end

def apply!
universe.values = blend
end

def blend
inject(Array.new(universe.values.size, 0)) do |blended, layer|
layer.values.each_index do |i|
blended[i] = [([(blended[i] + layer[i]), 255].min), 0].max
end
blended
end
end

def push *obj
if obj.empty? || (obj.size == 1 && obj.first.is_a?(Integer))
num = obj.pop || 1
Expand All @@ -17,26 +30,15 @@ def push *obj
end

class Layer
include Rdmx::Universe::Accessors

attr_accessor :values, :fixtures, :parent

def initialize parent
self.parent = parent
self.values = parent.universe.values
self.values = Array.new parent.universe.values.size, 0
self.fixtures = Rdmx::Universe::FixtureArray.new self,
parent.universe.fixture_class
end

def update_channel channel, *new_values
new_values = extrapolate_pattern channel, new_values
index = coerce_index channel, new_values.size
blended = values[*index].each_with_index.map do |v, i|
new_value = new_values[i]
new_value ? [[v + new_values[i], 255].min, 0].max : v
end
self.values[*index] = blended
end

include Rdmx::Universe::Accessors # after redefinition so alias works
end
end
12 changes: 12 additions & 0 deletions spec/layer_spec.rb
Expand Up @@ -11,24 +11,28 @@

it "should be easy to wash the background" do
@layers[0][0..-1] = 255, 0, 0
@layers.apply!
@universe.values.should == (([255, 0, 0] * (Rdmx::Universe::NUM_CHANNELS / 3)) + [0, 0])
end

it "should be easy to create a blend" do
@layers[0][0..-1] = 255, 0, 0
@layers[1][0..-1] = 0, 255, 0
@layers.apply!
@universe.values.should == (([255, 255, 0] * (Rdmx::Universe::NUM_CHANNELS / 3)) + [0, 0])
end

it "should not blend higher than 255" do
@layers[0][0] = 255
@layers[1][0] = 1
@layers.apply!
@universe.values[0].should == 255
end

it "should not blend lower than 0" do
@layers[0][0] = 1
@layers[1][0] = -3
@layers.apply!
@universe.values[0].should == 0
end

Expand All @@ -43,6 +47,7 @@
@layers[0][0..-1] = 255, 0, 0
@layers[1][0..-1] = 0, 255, 0
@layers[2][0..-1] = 0, 0, 255
@layers.apply!
@universe.values.should == (([255, 255, 255] * (Rdmx::Universe::NUM_CHANNELS / 3)) + [0, 0])
end

Expand All @@ -52,6 +57,13 @@
@layers[0][0..-1] = 255, 0, 0
@layers[1][0..-1] = 0, 255, 0
@layers[11][0..-1] = 0, 0, 255
@layers.apply!
@universe.values.should == (([255, 255, 255] * (Rdmx::Universe::NUM_CHANNELS / 3)) + [0, 0])
end

it "should not copy values from the parent universe" do
@universe[0..-1] = 100
@layers.push
@layers.last.values.uniq.should == [0]
end
end

0 comments on commit e38d3cc

Please sign in to comment.