Class: Sass::Constant::Color

Inherits:
Literal show all
Defined in:
lib/gems/haml-2.0.4/lib/sass/constant/color.rb

Overview

:nodoc:

Constant Summary collapse

HTML4_COLORS =
{
  '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
}
REGEXP =
/\##{"([0-9a-fA-F]{1,2})" * 3}/

Constants inherited from Literal

Literal::COLOR, Literal::NUMBER

Instance Attribute Summary

Attributes inherited from Literal

#value

Instance Method Summary collapse

Methods inherited from Literal

#concat, #initialize, parse, #perform

Constructor Details

This class inherits a constructor from Sass::Constant::Literal

Instance Method Details

#div(other) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/gems/haml-2.0.4/lib/sass/constant/color.rb', line 60

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

#minus(other) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/gems/haml-2.0.4/lib/sass/constant/color.rb', line 44

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

#mod(other) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/gems/haml-2.0.4/lib/sass/constant/color.rb', line 68

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

#parse(value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/gems/haml-2.0.4/lib/sass/constant/color.rb', line 27

def parse(value)
  if (value =~ REGEXP)
    @value = value.scan(REGEXP)[0].map { |num| num.ljust(2, num).to_i(16) }
  else
    color = HTML4_COLORS[value.downcase]
    @value = (0..2).map{ |n| color >> (n << 3) & 0xff }.reverse
  end
end

#plus(other) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/gems/haml-2.0.4/lib/sass/constant/color.rb', line 36

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

#times(other) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/gems/haml-2.0.4/lib/sass/constant/color.rb', line 52

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

#to_sObject



76
77
78
79
# File 'lib/gems/haml-2.0.4/lib/sass/constant/color.rb', line 76

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