| Class | Sass::Script::Color |
| In: |
lib/sass/script/color.rb
|
| Parent: | Literal |
A SassScript object representing a CSS color.
| HTML4_COLORS | = | map_vals({ '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 | A hash from color names to `[red, green, blue]` value arrays. @private | |
| HTML4_COLORS_REVERSE | = | map_hash(HTML4_COLORS) {|k, v| [v, k]} | A hash from `[red, green, blue]` value arrays to color names. @private |
Creates a new color from RGB components. Note: when modifying the components of an existing color, use \{with} rather than creating a new color object. This preserves forwards-compatiblity for alpha channels and such.
@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
# File lib/sass/script/color.rb, line 40
40: def initialize(rgb)
41: rgb = rgb.map {|c| c.to_i}
42: raise Sass::SyntaxError.new("Color values must be between 0 and 255") if rgb.any? {|c| c < 0 || c > 255}
43: super(rgb.freeze)
44: end
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
# File lib/sass/script/color.rb, line 167
167: def div(other)
168: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
169: piecewise(other, :/)
170: else
171: super
172: end
173: end
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
# File lib/sass/script/color.rb, line 124
124: def minus(other)
125: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
126: piecewise(other, :-)
127: else
128: super
129: end
130: end
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.
@param other [Number, Color] The right-hand side of the operator @return [Color] The resulting color @raise [Sass::SyntaxError] if `other` is a number with units
# File lib/sass/script/color.rb, line 187
187: def mod(other)
188: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
189: piecewise(other, :%)
190: else
191: raise NoMethodError.new(nil, :mod)
192: end
193: end
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
# File lib/sass/script/color.rb, line 101
101: def plus(other)
102: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
103: piecewise(other, :+)
104: else
105: super
106: end
107: end
Returns the red, green, and blue components of the color.
@return [Array<Fixnum>] A frozen three-element array of the red, green, and blue
values (respectively) of the color
# File lib/sass/script/color.rb, line 62
62: def rgb
63: @value
64: end
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.
@param other [Number, Color] The right-hand side of the operator @return [Color] The resulting color @raise [Sass::SyntaxError] if `other` is a number with units
# File lib/sass/script/color.rb, line 144
144: def times(other)
145: if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
146: piecewise(other, :*)
147: else
148: raise NoMethodError.new(nil, :times)
149: end
150: end
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
# File lib/sass/script/color.rb, line 200
200: def to_s
201: return HTML4_COLORS_REVERSE[rgb] if HTML4_COLORS_REVERSE[rgb]
202: red, green, blue = rgb.map { |num| num.to_s(16).rjust(2, '0') }
203: "##{red}#{green}#{blue}"
204: end
Returns a copy of this color with one or more channels changed.
For example:
Color.new([10, 20, 30]).with(:blue => 40)
#=> rgb(10, 40, 30)
Color.new([126, 126, 126]).with(:red => 0, :green => 255)
#=> rgb(0, 255, 126)
@param attrs [{Symbol => Fixnum}]
A map of channel names (`:red`, `:green`, or `:blue`) to values
@return [Color] The new Color object
# File lib/sass/script/color.rb, line 78
78: def with(attrs)
79: Color.new([
80: attrs[:red] || rgb[0],
81: attrs[:green] || rgb[1],
82: attrs[:blue] || rgb[2],
83: ])
84: end