Class: Castaway::Size
- Inherits:
-
Struct
- Object
- Struct
- Castaway::Size
- Defined in:
- lib/castaway/size.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #*(factor) ⇒ Object
- #aspect_ratio ⇒ Object
- #empty? ⇒ Boolean
- #present? ⇒ Boolean
- #to_geometry ⇒ Object
- #to_resolution ⇒ Object
- #to_s ⇒ Object
- #with_height(height) ⇒ Object
- #with_width(width) ⇒ Object
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height
3 4 5 |
# File 'lib/castaway/size.rb', line 3 def height @height end |
#width ⇒ Object
Returns the value of attribute width
3 4 5 |
# File 'lib/castaway/size.rb', line 3 def width @width end |
Instance Method Details
#*(factor) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/castaway/size.rb', line 4 def *(factor) if factor.is_a?(Size) Size.new(width * factor.width, height * factor.height) else Size.new(width * factor, height * factor) end end |
#aspect_ratio ⇒ Object
20 21 22 |
# File 'lib/castaway/size.rb', line 20 def aspect_ratio @aspect_ratio ||= width.to_f / height end |
#empty? ⇒ Boolean
16 17 18 |
# File 'lib/castaway/size.rb', line 16 def empty? (width || 0).zero? && (height || 0).zero? end |
#present? ⇒ Boolean
12 13 14 |
# File 'lib/castaway/size.rb', line 12 def present? width || height end |
#to_geometry ⇒ Object
36 37 38 39 40 |
# File 'lib/castaway/size.rb', line 36 def to_geometry format('%.2fx%.2f', width, height).tap do |geometry| geometry << '!' if width && height end end |
#to_resolution ⇒ Object
42 43 44 |
# File 'lib/castaway/size.rb', line 42 def to_resolution format('%dx%d', width || 0, height || 0) end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/castaway/size.rb', line 32 def to_s format('(%.2f, %.2f)', width, height) end |
#with_height(height) ⇒ Object
24 25 26 |
# File 'lib/castaway/size.rb', line 24 def with_height(height) Size.new((aspect_ratio * height).to_i, height.to_i) end |
#with_width(width) ⇒ Object
28 29 30 |
# File 'lib/castaway/size.rb', line 28 def with_width(width) Size.new(width.to_i, (width / aspect_ratio).to_i) end |