Class: Sass::Script::Color

Inherits:
Literal show all
Extended by:
Haml::Util
Defined in:
lib/sass/script/color.rb

Overview

A SassScript object representing a CSS color.

Constant Summary collapse

HTML4_COLORS =

A hash from color names to [red, green, blue] value arrays.

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
}) {|color| (0..2).map {|n| color >> (n << 3) & 0xff}.reverse}
HTML4_COLORS_REVERSE =

A hash from [red, green, blue] value arrays to color names.

map_hash(HTML4_COLORS) {|k, v| [v, k]}

Constants included from Haml::Util

Haml::Util::RUBY_VERSION

Instance Attribute Summary

Attributes inherited from Literal

#value

Instance Method Summary collapse

Methods included from Haml::Util

check_encoding, def_static_method, enum_with_index, has?, map_hash, map_keys, map_vals, merge_adjacent_strings, powerset, ruby1_8?, scope, static_method_name, to_hash

Methods inherited from Literal

#==, #and, #assert_int!, #comma, #concat, #eq, #neq, #or, #perform, #to_bool, #to_i, #unary_div, #unary_minus, #unary_not

Methods inherited from Node

#perform

Constructor Details

#initialize(rgb) ⇒ Color

Returns a new instance of Color.

Parameters:

  • rgb (Array<Fixnum>)

    A three-element array of the red, green, and blue values (respectively) of the color

Raises:



33
34
35
36
37
# File 'lib/sass/script/color.rb', line 33

def initialize(rgb)
  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}
  super(rgb)
end

Instance Method Details

#div(other) ⇒ Color

The SassScript / operation. Its functionality depends on the type of its argument:

Number : Divides each of the RGB color channels by the number.

Sass::Script::Color : Divides each of this color's RGB color channels by the other color's.

Literal : See Literal#div.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



123
124
125
126
127
128
129
# File 'lib/sass/script/color.rb', line 123

def div(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :/)
  else
    super
  end
end

#minus(other) ⇒ Color

The SassScript - operation. Its functionality depends on the type of its argument:

Number : Subtracts the number from each of the RGB color channels.

Sass::Script::Color : Subtracts each of the other color's RGB color channels from this color's.

Literal : See Literal#minus.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



77
78
79
80
81
82
83
# File 'lib/sass/script/color.rb', line 77

def minus(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :-)
  else
    super
  end
end

#mod(other) ⇒ Color

The SassScript % operation. Its functionality depends on the type of its argument:

Number : Takes each of the RGB color channels module the number.

Sass::Script::Color : Takes each of this color's RGB color channels modulo the other color's.

Literal : See Literal#mod.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



146
147
148
149
150
151
152
# File 'lib/sass/script/color.rb', line 146

def mod(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :%)
  else
    raise NoMethodError.new(nil, :mod)
  end
end

#plus(other) ⇒ Color

The SassScript + operation. Its functionality depends on the type of its argument:

Number : Adds the number to each of the RGB color channels.

Sass::Script::Color : Adds each of the RGB color channels together.

Literal : See Literal#plus.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



54
55
56
57
58
59
60
# File 'lib/sass/script/color.rb', line 54

def plus(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :+)
  else
    super
  end
end

#times(other) ⇒ Color

The SassScript * operation. Its functionality depends on the type of its argument:

Number : Multiplies the number by each of the RGB color channels.

Sass::Script::Color : Multiplies each of the RGB color channels together.

Literal : See Literal#times.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Color)

    The resulting color

Raises:



100
101
102
103
104
105
106
# File 'lib/sass/script/color.rb', line 100

def times(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :*)
  else
    raise NoMethodError.new(nil, :times)
  end
end

#to_sString Also known as: inspect

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.

Returns:

  • (String)

    The string representation



159
160
161
162
163
# File 'lib/sass/script/color.rb', line 159

def to_s
  return HTML4_COLORS_REVERSE[@value] if HTML4_COLORS_REVERSE[@value]
  red, green, blue = @value.map { |num| num.to_s(16).rjust(2, '0') }
  "##{red}#{green}#{blue}"
end