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
|
2009-04-24 19:48:35 -07:00
|
|
|
# A SassScript object representing a CSS color.
|
|
|
|
class Color < Literal
|
2008-11-22 19:22:34 -08:00
|
|
|
class << self; include Haml::Util; end
|
|
|
|
|
2009-04-24 19:48:35 -07:00
|
|
|
# A hash from color names to [red, green, blue] value arrays.
|
2008-11-22 19:22:34 -08:00
|
|
|
HTML4_COLORS = map_vals({
|
2008-11-22 19:17:06 -08:00
|
|
|
'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-11-22 19:22:34 -08:00
|
|
|
}) {|color| (0..2).map {|n| color >> (n << 3) & 0xff}.reverse}
|
2009-04-24 19:48:35 -07:00
|
|
|
# A hash from [red, green, blue] value arrays to color names.
|
2008-11-22 19:22:34 -08:00
|
|
|
HTML4_COLORS_REVERSE = map_hash(HTML4_COLORS) {|k, v| [v, k]}
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-11-10 18:29:48 -08:00
|
|
|
# The red component of the color.
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
attr_reader :red
|
|
|
|
|
|
|
|
# The green component of the color.
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
attr_reader :green
|
|
|
|
|
|
|
|
# The blue component of the color.
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
attr_reader :blue
|
|
|
|
|
2009-04-24 19:48:35 -07:00
|
|
|
# @param rgb [Array<Fixnum>] A three-element array of the red, green, and blue values (respectively)
|
|
|
|
# of the color
|
|
|
|
# @raise [Sass::SyntaxError] if any color value isn't between 0 and 255
|
2008-10-11 14:55:45 -07:00
|
|
|
def initialize(rgb)
|
2008-10-25 18:57:10 -07:00
|
|
|
rgb = rgb.map {|c| c.to_i}
|
|
|
|
raise Sass::SyntaxError.new("Color values must be between 0 and 255") if rgb.any? {|c| c < 0 || c > 255}
|
2009-11-10 18:29:48 -08:00
|
|
|
@red = rgb[0]
|
|
|
|
@green = rgb[1]
|
|
|
|
@blue = rgb[2]
|
|
|
|
super(nil)
|
2006-12-23 22:31:32 +00:00
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-11-10 18:27:13 -08:00
|
|
|
# @deprecated This will be removed in version 2.6.
|
|
|
|
# @see #rgb
|
|
|
|
def value
|
|
|
|
warn <<END
|
|
|
|
DEPRECATION WARNING:
|
|
|
|
The Sass::Script::Color #value attribute is deprecated and will be
|
|
|
|
removed in version 2.6. Use the #rgb attribute instead.
|
|
|
|
END
|
|
|
|
rgb
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the red, green, and blue components of the color.
|
|
|
|
#
|
|
|
|
# @return [Array<Fixnum>] A three-element array of the red, green, and blue
|
|
|
|
# values (respectively) of the color
|
|
|
|
def rgb
|
2009-11-10 18:29:48 -08:00
|
|
|
[red, green, blue]
|
|
|
|
end
|
|
|
|
|
|
|
|
# The SassScript `==` operation.
|
|
|
|
# **Note that this returns a {Sass::Script::Bool} object,
|
|
|
|
# not a Ruby boolean**.
|
|
|
|
#
|
|
|
|
# @param other [Literal] The right-hand side of the operator
|
|
|
|
# @return [Bool] True if this literal is the same as the other,
|
|
|
|
# false otherwise
|
|
|
|
def eq(other)
|
|
|
|
Sass::Script::Bool.new(other.is_a?(Color) && rgb == other.rgb)
|
2009-11-10 18:27:13 -08:00
|
|
|
end
|
|
|
|
|
2009-04-24 19:48:35 -07:00
|
|
|
# The SassScript `+` operation.
|
|
|
|
# Its functionality depends on the type of its argument:
|
|
|
|
#
|
|
|
|
# {Number}
|
|
|
|
# : Adds the number to each of the RGB color channels.
|
|
|
|
#
|
|
|
|
# {Color}
|
|
|
|
# : Adds each of the RGB color channels together.
|
|
|
|
#
|
|
|
|
# {Literal}
|
|
|
|
# : See {Literal#plus}.
|
|
|
|
#
|
|
|
|
# @param other [Literal] The right-hand side of the operator
|
|
|
|
# @return [Color] The resulting color
|
|
|
|
# @raise [Sass::SyntaxError] if `other` is a number with units
|
2006-12-24 00:23:28 +00:00
|
|
|
def plus(other)
|
2009-03-24 01:31:37 -07:00
|
|
|
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
|
2006-12-24 23:05:07 +00:00
|
|
|
piecewise(other, :+)
|
2009-03-24 01:31:37 -07:00
|
|
|
else
|
|
|
|
super
|
2006-12-24 23:05:07 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-04-24 19:48:35 -07:00
|
|
|
# The SassScript `-` operation.
|
|
|
|
# Its functionality depends on the type of its argument:
|
|
|
|
#
|
|
|
|
# {Number}
|
|
|
|
# : Subtracts the number from each of the RGB color channels.
|
|
|
|
#
|
|
|
|
# {Color}
|
|
|
|
# : Subtracts each of the other color's RGB color channels from this color's.
|
|
|
|
#
|
|
|
|
# {Literal}
|
|
|
|
# : See {Literal#minus}.
|
|
|
|
#
|
|
|
|
# @param other [Literal] The right-hand side of the operator
|
|
|
|
# @return [Color] The resulting color
|
|
|
|
# @raise [Sass::SyntaxError] if `other` is a number with units
|
2006-12-24 23:43:24 +00:00
|
|
|
def minus(other)
|
2009-03-24 01:31:37 -07:00
|
|
|
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
|
2006-12-24 23:43:24 +00:00
|
|
|
piecewise(other, :-)
|
2009-03-24 01:31:37 -07:00
|
|
|
else
|
|
|
|
super
|
2006-12-24 23:43:24 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-04-24 19:48:35 -07:00
|
|
|
# The SassScript `*` operation.
|
|
|
|
# Its functionality depends on the type of its argument:
|
|
|
|
#
|
|
|
|
# {Number}
|
|
|
|
# : Multiplies the number by each of the RGB color channels.
|
|
|
|
#
|
|
|
|
# {Color}
|
|
|
|
# : Multiplies each of the RGB color channels together.
|
|
|
|
#
|
|
|
|
# {Literal}
|
|
|
|
# : See {Literal#times}.
|
|
|
|
#
|
|
|
|
# @param other [Literal] The right-hand side of the operator
|
|
|
|
# @return [Color] The resulting color
|
|
|
|
# @raise [Sass::SyntaxError] if `other` is a number with units
|
2006-12-24 23:05:07 +00:00
|
|
|
def times(other)
|
2009-03-24 01:31:37 -07:00
|
|
|
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
|
2006-12-24 23:05:07 +00:00
|
|
|
piecewise(other, :*)
|
2009-03-24 01:31:37 -07:00
|
|
|
else
|
|
|
|
raise NoMethodError.new(nil, :times)
|
2006-12-24 00:23:28 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-04-24 19:48:35 -07:00
|
|
|
# The SassScript `/` operation.
|
|
|
|
# Its functionality depends on the type of its argument:
|
|
|
|
#
|
|
|
|
# {Number}
|
|
|
|
# : Divides each of the RGB color channels by the number.
|
|
|
|
#
|
|
|
|
# {Color}
|
|
|
|
# : Divides each of this color's RGB color channels by the other color's.
|
|
|
|
#
|
|
|
|
# {Literal}
|
|
|
|
# : See {Literal#div}.
|
|
|
|
#
|
|
|
|
# @param other [Literal] The right-hand side of the operator
|
|
|
|
# @return [Color] The resulting color
|
|
|
|
# @raise [Sass::SyntaxError] if `other` is a number with units
|
2006-12-24 23:43:24 +00:00
|
|
|
def div(other)
|
2009-03-24 01:31:37 -07:00
|
|
|
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
|
2006-12-24 23:43:24 +00:00
|
|
|
piecewise(other, :/)
|
2009-03-24 01:31:37 -07:00
|
|
|
else
|
|
|
|
super
|
2006-12-24 23:43:24 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-04-24 19:48:35 -07:00
|
|
|
# The SassScript `%` operation.
|
|
|
|
# Its functionality depends on the type of its argument:
|
|
|
|
#
|
|
|
|
# {Number}
|
|
|
|
# : Takes each of the RGB color channels module the number.
|
|
|
|
#
|
|
|
|
# {Color}
|
|
|
|
# : Takes each of this color's RGB color channels modulo the other color's.
|
|
|
|
#
|
|
|
|
# {Literal}
|
|
|
|
# : See {Literal#mod}.
|
|
|
|
#
|
|
|
|
# @param other [Literal] The right-hand side of the operator
|
|
|
|
# @return [Color] The resulting color
|
|
|
|
# @raise [Sass::SyntaxError] if `other` is a number with units
|
2006-12-24 23:53:38 +00:00
|
|
|
def mod(other)
|
2009-03-24 01:31:37 -07:00
|
|
|
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
|
2006-12-24 23:53:38 +00:00
|
|
|
piecewise(other, :%)
|
2009-03-24 01:31:37 -07:00
|
|
|
else
|
|
|
|
raise NoMethodError.new(nil, :mod)
|
2006-12-24 23:53:38 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-04-24 19:48:35 -07:00
|
|
|
# Returns a string representation of the color.
|
|
|
|
# This is usually the color's hex value,
|
|
|
|
# but if the color has a name that's used instead.
|
|
|
|
#
|
|
|
|
# @return [String] The string representation
|
2006-12-23 22:31:32 +00:00
|
|
|
def to_s
|
2009-11-10 18:27:13 -08:00
|
|
|
return HTML4_COLORS_REVERSE[rgb] if HTML4_COLORS_REVERSE[rgb]
|
|
|
|
red, green, blue = rgb.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
|
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
|
|
|
|
2009-11-10 18:27:13 -08:00
|
|
|
result = []
|
2006-12-24 00:23:28 +00:00
|
|
|
for i in (0...3)
|
2009-11-10 18:27:13 -08:00
|
|
|
res = rgb[i].send(operation, other_num ? other.value : other.rgb[i])
|
|
|
|
result[i] = [ [res, 255].min, 0 ].max
|
2006-12-24 00:23:28 +00:00
|
|
|
end
|
2009-11-10 18:27:13 -08:00
|
|
|
Color.new(result)
|
2006-12-24 00:23:28 +00:00
|
|
|
end
|
2006-12-23 22:31:32 +00:00
|
|
|
end
|
|
|
|
end
|