Class: RTF::Colour

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

Overview

This class represents a colour within a RTF document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue) ⇒ Colour

This is the constructor for the Colour class.

Parameters

red

The intensity setting for red in the colour. Must be an integer between 0 and 255.

green

The intensity setting for green in the colour. Must be an integer between 0 and 255.

blue

The intensity setting for blue in the colour. Must be an integer between 0 and 255.

Exceptions

RTFError

Generated whenever an invalid intensity setting is specified for the red, green or blue values.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rtf/colour.rb', line 25

def initialize(red, green, blue)
   if red.kind_of?(Integer) == false || red < 0 || red > 255
      RTFError.fire("Invalid red intensity setting ('#{red}') specified "\
                    "for a Colour object.")
   end
   if green.kind_of?(Integer) == false || green < 0 || green > 255
      RTFError.fire("Invalid green intensity setting ('#{green}') "\
                    "specified for a Colour object.")
   end
   if blue.kind_of?(Integer) == false || blue < 0 || blue > 255
      RTFError.fire("Invalid blue intensity setting ('#{blue}') "\
                    "specified for a Colour object.")
   end

   @red   = red
   @green = green
   @blue  = blue
end

Instance Attribute Details

#blueObject (readonly)

Attribute accessor.



9
10
11
# File 'lib/rtf/colour.rb', line 9

def blue
  @blue
end

#greenObject (readonly)

Attribute accessor.



9
10
11
# File 'lib/rtf/colour.rb', line 9

def green
  @green
end

#redObject (readonly)

Attribute accessor.



9
10
11
# File 'lib/rtf/colour.rb', line 9

def red
  @red
end

Instance Method Details

#==(object) ⇒ Object

This method overloads the comparison operator for the Colour class.

Parameters

object

A reference to the object to be compared with.



48
49
50
51
52
53
# File 'lib/rtf/colour.rb', line 48

def ==(object)
   object.instance_of?(Colour) &&
   object.red   == @red &&
   object.green == @green &&
   object.blue  == @blue
end

#to_rtf(indent = 0) ⇒ Object

This method generates the RTF text for a Colour object.

Parameters

indent

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



70
71
72
73
# File 'lib/rtf/colour.rb', line 70

def to_rtf(indent=0)
   prefix = indent > 0 ? ' ' * indent : ''
   "#{prefix}\\red#{@red}\\green#{@green}\\blue#{@blue};"
end

#to_s(indent = 0) ⇒ Object

This method returns a textual description for a Colour object.

Parameters

indent

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



60
61
62
63
# File 'lib/rtf/colour.rb', line 60

def to_s(indent=0)
   prefix = indent > 0 ? ' ' * indent : ''
   "#{prefix}Colour (#{@red}/#{@green}/#{@blue})"
end