Class: RTF::ColourTable

Inherits:
Object
  • Object
show all
Defined in:
lib/rtf/colour.rb

Overview

This class represents a table of colours used within a RTF document. This class need not be directly instantiated as it will be used internally by, and can be obtained from a Document object.

Instance Method Summary collapse

Constructor Details

#initialize(*colours) ⇒ ColourTable

This is the constructor for the ColourTable class.

Parameters

*colours

An array of zero or more colours that make up the colour table entries.



86
87
88
89
# File 'lib/rtf/colour.rb', line 86

def initialize(*colours)
   @colours = []
   colours.each {|colour| add(colour)}
end

Instance Method Details

#[](index) ⇒ Object

This method overloads the array dereference operator for the ColourTable class. It is not possible to dereference the implicit default colour using this method. An invalid index will return a nil value.

Parameters

index

The index of the colour to be retrieved.



124
125
126
# File 'lib/rtf/colour.rb', line 124

def [](index)
   @colours[index]
end

#add(colour) ⇒ Object Also known as: <<

This method adds a new colour to a ColourTable object. If the colour already exists within the table or is not a Colour object then this method does nothing.

Parameters

colour

The colour to be added to the table.



103
104
105
106
107
108
# File 'lib/rtf/colour.rb', line 103

def add(colour)
   if colour.instance_of?(Colour)
      @colours.push(colour) if @colours.index(colour) == nil
   end
   self
end

#eachObject

This method iterates over the contents of a ColourTable object. This iteration does not include the implicit default colour entry.



112
113
114
115
116
# File 'lib/rtf/colour.rb', line 112

def each
   if block_given?
      @colours.each {|colour| yield colour}
   end
end

#index(colour) ⇒ Object

This method retrieves the index of a specified colour within the table. If the colour doesn’t exist within the table then nil is returned. It should be noted that the index of a colour will be one more than its order of entry to account for the implicit default colour entry.

Parameters

colour

The colour to retrieve the index of.



135
136
137
138
# File 'lib/rtf/colour.rb', line 135

def index(colour)
   index = @colours.index(colour)
   index == nil ? index : index + 1
end

#sizeObject

This method fetches a count of the number of colours within a colour table.



93
94
95
# File 'lib/rtf/colour.rb', line 93

def size
   @colours.size
end

#to_rtf(indent = 0) ⇒ Object

This method generates the RTF text for a ColourTable object.

Parameters

indent

The number of spaces to prefix to the lines generated by the method. Defaults to zero.



160
161
162
163
164
165
166
167
168
169
# File 'lib/rtf/colour.rb', line 160

def to_rtf(indent=0)
   prefix = indent > 0 ? ' ' * indent : ''
   text   = StringIO.new

   text << "#{prefix}{\\colortbl\n#{prefix};"
   @colours.each {|colour| text << "\n#{prefix}#{colour.to_rtf}"}
   text << "\n#{prefix}}"

   text.string
end

#to_s(indent = 0) ⇒ Object

This method generates a textual description for a ColourTable object.

Parameters

indent

The number of spaces to prefix to the lines generated by the method. Defaults to zero.



145
146
147
148
149
150
151
152
153
# File 'lib/rtf/colour.rb', line 145

def to_s(indent=0)
   prefix = indent > 0 ? ' ' * indent : ''
   text   = StringIO.new

   text << "#{prefix}Colour Table (#{@colours.size} colours)"
   @colours.each {|colour| text << "\n#{prefix}   #{colour}"}

   text.string
end