Class: Sass::Script::Color

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

Overview

:nodoc:

Constant Summary collapse

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

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

Methods inherited from Literal

#==, #and, #comma, #concat, #eq, #neq, #or, #perform, #to_bool, #to_i, #unary_not

Constructor Details

#initialize(rgb) ⇒ Color

Returns a new instance of Color.

Raises:



27
28
29
30
31
# File 'lib/sass/script/color.rb', line 27

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) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/sass/script/color.rb', line 57

def div(other)
  if other.is_a? Sass::Script::String
    raise NoMethodError.new(nil, :div)
  else
    piecewise(other, :/)
  end
end

#minus(other) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/sass/script/color.rb', line 41

def minus(other)
  if other.is_a? Sass::Script::String
    raise NoMethodError.new(nil, :minus)
  else
    piecewise(other, :-)
  end
end

#mod(other) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/sass/script/color.rb', line 65

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

#plus(other) ⇒ Object



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

def plus(other)
  if other.is_a? Sass::Script::String
    Sass::Script::String.new(self.to_s + other.to_s)
  else
    piecewise(other, :+)
  end
end

#times(other) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/sass/script/color.rb', line 49

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

#to_sObject Also known as: inspect



73
74
75
76
77
# File 'lib/sass/script/color.rb', line 73

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