2006-12-23 22:32:05 +00:00
|
|
|
require 'sass/constant/literal'
|
|
|
|
|
2008-06-05 15:06:48 -07:00
|
|
|
module Sass::Constant
|
2007-03-01 05:52:47 +00:00
|
|
|
class Color < Literal # :nodoc:
|
2007-03-17 02:03:10 +00:00
|
|
|
|
|
|
|
HTML4_COLORS = {
|
|
|
|
'black' => 0x000000,
|
|
|
|
'silver' => 0xc0c0c0,
|
|
|
|
'gray' => 0x808080,
|
|
|
|
'white' => 0xffffff,
|
|
|
|
'maroon' => 0x800000,
|
|
|
|
'red' => 0xff0000,
|
|
|
|
'purple' => 0x800080,
|
|
|
|
'fuchsia' => 0xff00ff,
|
|
|
|
'green' => 0x008000,
|
|
|
|
'lime' => 0x00ff00,
|
|
|
|
'olive' => 0x808000,
|
|
|
|
'yellow' => 0xffff00,
|
|
|
|
'navy' => 0x000080,
|
|
|
|
'blue' => 0x0000ff,
|
|
|
|
'teal' => 0x008080,
|
|
|
|
'aqua' => 0x00ffff
|
|
|
|
}
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2007-02-05 00:53:31 +00:00
|
|
|
REGEXP = /\##{"([0-9a-fA-F]{1,2})" * 3}/
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-23 22:57:31 +00:00
|
|
|
def parse(value)
|
2007-03-17 02:03:10 +00:00
|
|
|
if (value =~ REGEXP)
|
|
|
|
@value = value.scan(REGEXP)[0].map { |num| num.ljust(2, num).to_i(16) }
|
|
|
|
else
|
|
|
|
color = HTML4_COLORS[value.downcase]
|
|
|
|
@value = (0..2).map{ |n| color >> (n << 3) & 0xff }.reverse
|
|
|
|
end
|
2006-12-23 22:31:32 +00:00
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 00:23:28 +00:00
|
|
|
def plus(other)
|
|
|
|
if other.is_a? Sass::Constant::String
|
|
|
|
Sass::Constant::String.from_value(self.to_s + other.to_s)
|
|
|
|
else
|
2006-12-24 23:05:07 +00:00
|
|
|
piecewise(other, :+)
|
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 23:43:24 +00:00
|
|
|
def minus(other)
|
|
|
|
if other.is_a? Sass::Constant::String
|
|
|
|
raise NoMethodError.new(nil, :minus)
|
|
|
|
else
|
|
|
|
piecewise(other, :-)
|
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 23:05:07 +00:00
|
|
|
def times(other)
|
|
|
|
if other.is_a? Sass::Constant::String
|
|
|
|
raise NoMethodError.new(nil, :times)
|
|
|
|
else
|
|
|
|
piecewise(other, :*)
|
2006-12-24 00:23:28 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 23:43:24 +00:00
|
|
|
def div(other)
|
|
|
|
if other.is_a? Sass::Constant::String
|
|
|
|
raise NoMethodError.new(nil, :div)
|
|
|
|
else
|
|
|
|
piecewise(other, :/)
|
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 23:53:38 +00:00
|
|
|
def mod(other)
|
|
|
|
if other.is_a? Sass::Constant::String
|
|
|
|
raise NoMethodError.new(nil, :mod)
|
|
|
|
else
|
|
|
|
piecewise(other, :%)
|
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-23 22:31:32 +00:00
|
|
|
def to_s
|
2006-12-23 22:57:31 +00:00
|
|
|
red, green, blue = @value.map { |num| num.to_s(16).rjust(2, '0') }
|
2006-12-23 22:31:32 +00:00
|
|
|
"##{red}#{green}#{blue}"
|
|
|
|
end
|
2008-06-01 15:10:02 -07:00
|
|
|
alias_method :inspect, :to_s
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 00:23:28 +00:00
|
|
|
protected
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 23:05:07 +00:00
|
|
|
def self.filter_value(value)
|
|
|
|
value.map { |num| num.to_i }
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 23:05:07 +00:00
|
|
|
private
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 23:05:07 +00:00
|
|
|
def piecewise(other, operation)
|
2006-12-24 00:23:28 +00:00
|
|
|
other_num = other.is_a? Number
|
|
|
|
other_val = other.value
|
2008-09-21 00:29:21 -07:00
|
|
|
if other_num && !other.unitless?
|
|
|
|
raise Sass::SyntaxError.new("Cannot add a number with units (#{other}) to a color (#{self}).")
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-24 00:23:28 +00:00
|
|
|
rgb = []
|
|
|
|
for i in (0...3)
|
2006-12-24 23:43:24 +00:00
|
|
|
res = @value[i].send(operation, other_num ? other_val : other_val[i])
|
|
|
|
rgb[i] = [ [res, 255].min, 0 ].max
|
2006-12-24 00:23:28 +00:00
|
|
|
end
|
|
|
|
Color.from_value(rgb)
|
|
|
|
end
|
2006-12-23 22:31:32 +00:00
|
|
|
end
|
|
|
|
end
|