Class: RRTF::Colour
- Inherits:
-
Object
- Object
- RRTF::Colour
- Defined in:
- lib/rrtf/colour.rb
Overview
This class represents a colour within a RTF document.
Instance Attribute Summary collapse
-
#blue ⇒ Object
readonly
Attribute accessor.
-
#green ⇒ Object
readonly
Attribute accessor.
-
#red ⇒ Object
readonly
Attribute accessor.
Class Method Summary collapse
-
.from_string(str) ⇒ Object
Format: HEX RGB with '#' prefix.
Instance Method Summary collapse
-
#==(object) ⇒ Object
This method overloads the comparison operator for the Colour class.
-
#initialize(red, green, blue) ⇒ Colour
constructor
This is the constructor for the Colour class.
- #to_decimal(options = {}) ⇒ Object
-
#to_rtf(indent = 0) ⇒ Object
This method generates the RTF text for a Colour object.
-
#to_s(indent = 0) ⇒ Object
This method returns a textual description for a Colour object.
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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rrtf/colour.rb', line 34 def initialize(red, green, blue) if !red.kind_of?(Integer) || red < 0 || red > 255 RTFError.fire("Invalid red intensity setting ('#{red}') specified "\ "for a Colour object.") end if !green.kind_of?(Integer) || green < 0 || green > 255 RTFError.fire("Invalid green intensity setting ('#{green}') "\ "specified for a Colour object.") end if !blue.kind_of?(Integer) || 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
#blue ⇒ Object (readonly)
Attribute accessor.
7 8 9 |
# File 'lib/rrtf/colour.rb', line 7 def blue @blue end |
#green ⇒ Object (readonly)
Attribute accessor.
7 8 9 |
# File 'lib/rrtf/colour.rb', line 7 def green @green end |
#red ⇒ Object (readonly)
Attribute accessor.
7 8 9 |
# File 'lib/rrtf/colour.rb', line 7 def red @red end |
Class Method Details
.from_string(str) ⇒ Object
Format: HEX RGB with '#' prefix
11 12 13 14 15 16 17 18 |
# File 'lib/rrtf/colour.rb', line 11 def self.from_string(str) if str =~ /\#[0-9a-f]{6}/i m = str.match /([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i self.new(m[1].hex, m[2].hex, m[3].hex) else RTFError.fire("Unreconized string colour format '#{str}'.") end 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.
57 58 59 60 61 62 |
# File 'lib/rrtf/colour.rb', line 57 def ==(object) object.instance_of?(Colour) and object.red == @red and object.green == @green and object.blue == @blue end |
#to_decimal(options = {}) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/rrtf/colour.rb', line 84 def to_decimal( = {}) if ["reverse_bytes"] (@blue * 65536) + (@green * 256) + @red else (@red * 65536) + (@green * 256) + @blue end # if 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.
79 80 81 82 |
# File 'lib/rrtf/colour.rb', line 79 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.
69 70 71 72 |
# File 'lib/rrtf/colour.rb', line 69 def to_s(indent=0) prefix = indent > 0 ? ' ' * indent : '' "#{prefix}Colour (#{@red}/#{@green}/#{@blue})" end |