Constant multiplication.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@261 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-12-24 23:05:07 +00:00
parent 12255afcd0
commit c232d19f15
5 changed files with 64 additions and 10 deletions

View File

@ -13,7 +13,15 @@ module Sass::Constant
if other.is_a? Sass::Constant::String
Sass::Constant::String.from_value(self.to_s + other.to_s)
else
Color.piecewise(self, other) { |val1, val2| [val1 + val2, 255].min }
piecewise(other, :+)
end
end
def times(other)
if other.is_a? Sass::Constant::String
raise NoMethodError.new(nil, :times)
else
piecewise(other, :*)
end
end
@ -24,13 +32,19 @@ module Sass::Constant
protected
def self.piecewise(color1, other)
def self.filter_value(value)
value.map { |num| num.to_i }
end
private
def piecewise(other, operation)
other_num = other.is_a? Number
other_val = other.value
rgb = []
for i in (0...3)
rgb[i] = yield(color1.value[i], other_num ? other_val : other_val[i])
rgb[i] = [@value[i].send(operation, other_num ? other_val : other_val[i]), 255].min
end
Color.from_value(rgb)
end

View File

@ -34,9 +34,13 @@ class Sass::Constant::Literal
protected
def self.filter_value(value)
value
end
def self.from_value(value)
instance = self.new
instance.instance_variable_set('@value', value)
instance.instance_variable_set('@value', self.filter_value(value))
instance
end
end

View File

@ -5,20 +5,46 @@ module Sass::Constant
def parse(value)
value = value.to_f
value = value.to_i if value % 1 == 0.0
value = self.class.filter_value(value)
@value = value
end
def plus(other)
if other.is_a? Number
Number.from_value(self.value + other.value)
operate(other, :+)
elsif other.is_a? Color
other.plus(self)
else
Sass::Constant::String.from_value(self.to_s + other.to_s)
end
end
def times(other)
if other.is_a? Number
operate(other, :*)
elsif other.is_a? Color
other.times(self)
else
raise NoMethodError.new(nil, :times)
end
end
def to_s
@value.to_s
end
protected
def operate(other, operation)
Number.from_value(self.value.send(operation, other.value))
end
def self.filter_value(value)
if value % 1 == 0.0
value.to_i
else
value
end
end
end
end

View File

@ -1,4 +1,6 @@
#main { width: 30; background-color: #000; color: #ffffaf; }
#main #sidebar { background-color: #00ff98; num-normal: 10; num-dec: 10.2; num-dec0: 99; num-neg: -10; esc: 10+12; many: 6; complex: 18#3a8b9fhi4; }
#main #sidebar { background-color: #00ff98; num-normal: 10; num-dec: 10.2; num-dec0: 99; num-neg: -10; esc: 10+12; many: 6; complex: #4c9db1hi4; }
#plus { num-num: 7; num-str: 100px; num-col: 13#ffffff; str-str: hi there; str-num: times: 13; col-num: #ff8aaa; col-col: #5f80ff; }
#plus { num-num: 7; num-str: 100px; num-col: #bcbcbc; str-str: hi there; str-num: times: 13; col-num: #ff8aaa; col-col: #5f80ff; }
#times { num-num: 7; num-col: #7496b8; col-num: #092345; col-col: #243648; }

View File

@ -26,10 +26,18 @@
:num
:num= 5+2
:str= 100 + px
:col= 13 + #fff
:col= 13 + #aaa
:str
:str= hi + \ there
:num= times:\ + 13
:col
:num= #f02 + 123
:num= #f02 + 123.5
:col= #12a + #405162
#times
:num
:num= 2 * 3.5
:col= 2 * #3a4b5c
:col
:num= #12468a * 0.5
:col= #121212 * #020304