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-11-23 14:24:53 -08: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-11-23 14:24:53 -08: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 17:11:25 -08:00
|
|
|
# Constructs an RGB or RGBA color object.
|
|
|
|
# The RGB values must be between 0 and 255,
|
|
|
|
# and the alpha value is generally expected to be between 0 and 1.
|
|
|
|
# However, the alpha value can be greater than 1
|
|
|
|
# in order to allow it to be used for color multiplication.
|
|
|
|
#
|
|
|
|
# @param rgba [Array<Numeric>] A three-element array of the red, green, blue,
|
|
|
|
# and optionally alpha values (respectively) of the color
|
|
|
|
# @raise [Sass::SyntaxError] if any color value isn't between 0 and 255,
|
|
|
|
# or the alpha value is negative
|
|
|
|
def initialize(rgba)
|
2009-11-10 20:59:33 -08:00
|
|
|
@red, @green, @blue = rgba[0...3].map {|c| c.to_i}
|
|
|
|
@alpha = rgba[3] ? rgba[3].to_f : 1
|
|
|
|
super(nil)
|
2009-11-10 17:11:25 -08:00
|
|
|
|
|
|
|
unless rgb.all? {|c| (0..255).include?(c)}
|
|
|
|
raise Sass::SyntaxError.new("Color values must be between 0 and 255")
|
|
|
|
end
|
|
|
|
|
|
|
|
unless (0..1).include?(alpha)
|
|
|
|
raise Sass::SyntaxError.new("Color opacity value must between 0 and 1")
|
|
|
|
end
|
2006-12-23 22:31:32 +00:00
|
|
|
end
|
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-11-10 20:59:33 -08:00
|
|
|
# The alpha channel (opacity) of the color.
|
|
|
|
# This is 1 unless otherwise defined.
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
2009-11-26 14:10:33 -08:00
|
|
|
attr_reader :alpha
|
2009-11-10 20:59:33 -08:00
|
|
|
|
|
|
|
# Returns whether this color object is translucent;
|
|
|
|
# that is, whether the alpha channel is non-1.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def alpha?
|
|
|
|
alpha < 1
|
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.
|
|
|
|
#
|
2009-11-18 21:46:03 -08:00
|
|
|
# @return [Array<Fixnum>] A frozen three-element array of the red, green, and blue
|
2009-11-10 18:27:13 -08:00
|
|
|
# values (respectively) of the color
|
|
|
|
def rgb
|
2009-11-23 12:35:24 -08:00
|
|
|
[red, green, blue].freeze
|
2009-11-10 18:29:48 -08:00
|
|
|
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)
|
2009-11-10 20:59:33 -08:00
|
|
|
Sass::Script::Bool.new(
|
|
|
|
other.is_a?(Color) && rgb == other.rgb && alpha == other.alpha)
|
2009-11-10 18:27:13 -08:00
|
|
|
end
|
|
|
|
|
2009-11-18 21:46:03 -08:00
|
|
|
# Returns a copy of this color with one or more channels changed.
|
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
2009-11-26 16:58:08 -08:00
|
|
|
# Color.new([10, 20, 30]).with(:blue => 40)
|
2009-11-18 21:46:03 -08:00
|
|
|
# #=> rgb(10, 40, 30)
|
|
|
|
# Color.new([126, 126, 126]).with(:red => 0, :green => 255)
|
|
|
|
# #=> rgb(0, 255, 126)
|
2009-11-23 12:35:24 -08:00
|
|
|
# Color.new([1, 2, 3]).with(:alpha => 0.4)
|
|
|
|
# #=> rgba(1, 2, 3, 0.4)
|
2009-11-18 21:46:03 -08:00
|
|
|
#
|
2009-12-01 18:26:14 -08:00
|
|
|
# @param attrs [{Symbol => Numeric}]
|
2009-11-23 12:35:24 -08:00
|
|
|
# A map of channel names (`:red`, `:green`, `:blue`, or `:alpha`) to values
|
2009-11-18 21:46:03 -08:00
|
|
|
# @return [Color] The new Color object
|
|
|
|
def with(attrs)
|
|
|
|
Color.new([
|
2009-11-23 12:35:24 -08:00
|
|
|
attrs[:red] || red,
|
|
|
|
attrs[:green] || green,
|
|
|
|
attrs[:blue] || blue,
|
|
|
|
attrs[:alpha] || alpha,
|
2009-11-18 21:46:03 -08:00
|
|
|
])
|
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.
|
|
|
|
#
|
2009-11-23 14:24:53 -08:00
|
|
|
# @param other [Number, Color] The right-hand side of the operator
|
2009-04-24 19:48:35 -07:00
|
|
|
# @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.
|
|
|
|
#
|
2009-11-23 14:24:53 -08:00
|
|
|
# @param other [Number, Color] The right-hand side of the operator
|
2009-04-24 19:48:35 -07:00
|
|
|
# @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-12-03 14:42:08 -08:00
|
|
|
return rgba_str if alpha?
|
|
|
|
return smallest if options[:style] == :compressed
|
2009-11-10 17:11:25 -08:00
|
|
|
return HTML4_COLORS_REVERSE[rgb] if HTML4_COLORS_REVERSE[rgb]
|
2009-12-03 14:42:08 -08:00
|
|
|
hex_str
|
2006-12-23 22:31:32 +00:00
|
|
|
end
|
2008-06-01 15:10:02 -07:00
|
|
|
alias_method :inspect, :to_s
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-12-03 14:42:08 -08:00
|
|
|
# Returns a string representation of the color.
|
|
|
|
#
|
|
|
|
# @return [String] The hex value
|
|
|
|
def inspect
|
|
|
|
alpha? ? rgba_str : hex_str
|
|
|
|
end
|
|
|
|
|
2006-12-24 23:05:07 +00:00
|
|
|
private
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-12-03 14:42:08 -08:00
|
|
|
def smallest
|
|
|
|
small_hex_str = hex_str.gsub(/^#(.)\1(.)\2(.)\3$/, '#\1\2\3')
|
|
|
|
return small_hex_str unless (color = HTML4_COLORS_REVERSE[rgb]) &&
|
|
|
|
color.size <= small_hex_str.size
|
|
|
|
return color
|
|
|
|
end
|
|
|
|
|
|
|
|
def rgba_str
|
|
|
|
"rgba(#{rgb.join(', ')}, #{alpha % 1 == 0.0 ? alpha.to_i : alpha})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def hex_str
|
|
|
|
red, green, blue = rgb.map { |num| num.to_s(16).rjust(2, '0') }
|
|
|
|
"##{red}#{green}#{blue}"
|
|
|
|
end
|
|
|
|
|
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 17:11:25 -08:00
|
|
|
result = []
|
2006-12-24 00:23:28 +00:00
|
|
|
for i in (0...3)
|
2009-11-10 17:11:25 -08:00
|
|
|
res = rgb[i].send(operation, other_num ? other.value : other.rgb[i])
|
|
|
|
result[i] = [ [res, 255].min, 0 ].max
|
|
|
|
end
|
|
|
|
|
|
|
|
if !other_num && other.alpha != alpha
|
|
|
|
raise Sass::SyntaxError.new("Alpha channels must be equal: #{self} #{operation} #{other}")
|
2006-12-24 00:23:28 +00:00
|
|
|
end
|
2009-11-10 17:11:25 -08:00
|
|
|
|
2009-11-18 21:46:03 -08:00
|
|
|
with(:red => result[0], :green => result[1], :blue => result[2])
|
2006-12-24 00:23:28 +00:00
|
|
|
end
|
2006-12-23 22:31:32 +00:00
|
|
|
end
|
|
|
|
end
|