1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/sass/constant/color.rb
nex3 67397758bd Plus operation defined for all constants.
git-svn-id: svn://hamptoncatlin.com/haml/trunk@259 7063305b-7217-0410-af8c-cdc13e5119b9
2006-12-24 00:23:28 +00:00

38 lines
911 B
Ruby

require 'sass/constant/literal'
module Sass::Constant
class Color < Literal
REGEXP = /\##{"([0-9a-f]{1,2})" * 3}/
def parse(value)
@value = value.scan(REGEXP)[0].map { |num| num.ljust(2, 'f').to_i(16) }
end
def plus(other)
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 }
end
end
def to_s
red, green, blue = @value.map { |num| num.to_s(16).rjust(2, '0') }
"##{red}#{green}#{blue}"
end
protected
def self.piecewise(color1, other)
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])
end
Color.from_value(rgb)
end
end
end