Class Sass::Script::Color
In: lib/sass/script/color.rb
Parent: Literal

A SassScript object representing a CSS color.

Methods

div   inspect   minus   mod   new   plus   times   to_s  

Included Modules

Haml::Util

Constants

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.
HTML4_COLORS_REVERSE = map_hash(HTML4_COLORS) {|k, v| [v, k]}   A hash from [red, green, blue] value arrays to color names.

Public Class methods

@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

[Source]

    # File lib/sass/script/color.rb, line 33
33:     def initialize(rgb)
34:       rgb = rgb.map {|c| c.to_i}
35:       raise Sass::SyntaxError.new("Color values must be between 0 and 255") if rgb.any? {|c| c < 0 || c > 255}
36:       super(rgb)
37:     end

Public Instance methods

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

[Source]

     # File lib/sass/script/color.rb, line 123
123:     def div(other)
124:       if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
125:         piecewise(other, :/)
126:       else
127:         super
128:       end
129:     end
inspect()

Alias for to_s

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

[Source]

    # File lib/sass/script/color.rb, line 77
77:     def minus(other)
78:       if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
79:         piecewise(other, :-)
80:       else
81:         super
82:       end
83:     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.

{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

[Source]

     # File lib/sass/script/color.rb, line 146
146:     def mod(other)
147:       if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
148:         piecewise(other, :%)
149:       else
150:         raise NoMethodError.new(nil, :mod)
151:       end
152:     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

[Source]

    # File lib/sass/script/color.rb, line 54
54:     def plus(other)
55:       if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
56:         piecewise(other, :+)
57:       else
58:         super
59:       end
60:     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.

{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

[Source]

     # File lib/sass/script/color.rb, line 100
100:     def times(other)
101:       if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
102:         piecewise(other, :*)
103:       else
104:         raise NoMethodError.new(nil, :times)
105:       end
106:     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

[Source]

     # File lib/sass/script/color.rb, line 159
159:     def to_s
160:       return HTML4_COLORS_REVERSE[@value] if HTML4_COLORS_REVERSE[@value]
161:       red, green, blue = @value.map { |num| num.to_s(16).rjust(2, '0') }
162:       "##{red}#{green}#{blue}"
163:     end

[Validate]