2008-10-12 19:03:06 -07:00
|
|
|
require 'sass/script/literal'
|
2006-12-23 22:32:05 +00:00
|
|
|
|
2008-10-12 19:03:06 -07:00
|
|
|
module Sass::Script
|
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
|
|
|
|
2008-10-11 14:55:45 -07:00
|
|
|
def initialize(rgb)
|
|
|
|
super(rgb.map {|c| c.to_i})
|
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)
|
2008-10-12 19:03:06 -07:00
|
|
|
if other.is_a? Sass::Script::String
|
|
|
|
Sass::Script::String.new(self.to_s + other.to_s)
|
2006-12-24 00:23:28 +00:00
|
|
|
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)
|
2008-10-12 19:03:06 -07:00
|
|
|
if other.is_a? Sass::Script::String
|
2006-12-24 23:43:24 +00:00
|
|
|
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)
|
2008-10-12 19:03:06 -07:00
|
|
|
if other.is_a? Sass::Script::String
|
2006-12-24 23:05:07 +00:00
|
|
|
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)
|
2008-10-12 19:03:06 -07:00
|
|
|
if other.is_a? Sass::Script::String
|
2006-12-24 23:43:24 +00:00
|
|
|
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)
|
2008-10-12 19:03:06 -07:00
|
|
|
if other.is_a? Sass::Script::String
|
2006-12-24 23:53:38 +00:00
|
|
|
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 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
|
2008-10-11 14:55:45 -07:00
|
|
|
Color.new(rgb)
|
2006-12-24 00:23:28 +00:00
|
|
|
end
|
2006-12-23 22:31:32 +00:00
|
|
|
end
|
|
|
|
end
|