Class: RSAC::Color

Inherits:
LexicalUnit show all
Defined in:
lib/antisamy/csspool/rsac/sac/lexical_unit.rb

Instance Attribute Summary

Attributes inherited from LexicalUnit

#dimension_unit_text, #float_value, #function_name, #integer_value, #lexical_unit_type, #parameters, #string_value

Instance Method Summary collapse

Methods inherited from LexicalUnit

#eql?

Constructor Details

#initialize(value) ⇒ Color

Returns a new instance of Color.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/antisamy/csspool/rsac/sac/lexical_unit.rb', line 48

def initialize(value)
  self.string_value = value
  self.lexical_unit_type = :SAC_RGBCOLOR
  if value =~ /^#([A-F\d]{1,2})([A-F\d]{1,2})([A-F\d]{1,2})$/
    self.parameters = [$1, $2, $3].map { |x|
      x.length == 1 ? (x * 2).hex : x.hex
    }.map { |x|
      Number.new(x, '', :SAC_INTEGER)
    }
  else
    self.parameters = [LexicalIdent.new(value)]
  end
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/antisamy/csspool/rsac/sac/lexical_unit.rb', line 62

def ==(other)
  super && self.parameters == other.parameters
end

#hashObject



66
67
68
# File 'lib/antisamy/csspool/rsac/sac/lexical_unit.rb', line 66

def hash
  self.parameters.hash
end

#to_sObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/antisamy/csspool/rsac/sac/lexical_unit.rb', line 70

def to_s
  if self.parameters.length < 3
    super
  else
    hex = self.parameters.map { |x|
      sprintf("%02X", x.integer_value).split('').uniq
    }.flatten
    hex.length != 3 ? super : "##{hex.join()}"
  end
end