Class: Tuile::Size
- Inherits:
-
Object
- Object
- Tuile::Size
- Defined in:
- lib/tuile/size.rb,
sig/tuile.rbs
Overview
A size with integer width and height.
Constant Summary collapse
- ZERO =
An empty size constant.
Size.new(0, 0)
Instance Attribute Summary collapse
-
#height ⇒ Integer
readonly
@return — height.
-
#width ⇒ Integer
readonly
@return — width.
Instance Method Summary collapse
-
#clamp(max_size) ⇒ Size
Clamp both width and height and return a size.
-
#clamp_height(max_height) ⇒ Size
Clamp height and return a size.
- #empty? ⇒ Boolean
-
#plus(width, height) ⇒ Size
@param
width. - #to_s ⇒ String
Instance Attribute Details
#height ⇒ Integer (readonly)
@return — height.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tuile/size.rb', line 10 class Size < Data.define(:width, :height) # @return [String] def to_s = "#{width}x#{height}" # @return [Boolean] true if either {#width} or {#height} is zero or negative. def empty? width <= 0 || height <= 0 end # @param width [Integer] # @param height [Integer] # @return [Size] def plus(width, height) = Size.new(self.width + width, self.height + height) # Clamp both width and height and return a size. # @param max_size [Size] the max size # @return [Size] def clamp(max_size) new_width = width.clamp(nil, max_size.width) new_height = height.clamp(nil, max_size.height) new_width == width && new_height == height ? self : Size.new(new_width, new_height) end # Clamp height and return a size. # @param max_height [Integer] the max height # @return [Size] def clamp_height(max_height) = clamp(Size.new(width, max_height)) # An empty size constant. # @return [Size] ZERO = Size.new(0, 0) end |
#width ⇒ Integer (readonly)
@return — width.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tuile/size.rb', line 10 class Size < Data.define(:width, :height) # @return [String] def to_s = "#{width}x#{height}" # @return [Boolean] true if either {#width} or {#height} is zero or negative. def empty? width <= 0 || height <= 0 end # @param width [Integer] # @param height [Integer] # @return [Size] def plus(width, height) = Size.new(self.width + width, self.height + height) # Clamp both width and height and return a size. # @param max_size [Size] the max size # @return [Size] def clamp(max_size) new_width = width.clamp(nil, max_size.width) new_height = height.clamp(nil, max_size.height) new_width == width && new_height == height ? self : Size.new(new_width, new_height) end # Clamp height and return a size. # @param max_height [Integer] the max height # @return [Size] def clamp_height(max_height) = clamp(Size.new(width, max_height)) # An empty size constant. # @return [Size] ZERO = Size.new(0, 0) end |
Instance Method Details
#clamp(max_size) ⇒ Size
Clamp both width and height and return a size.
@param max_size — the max size
27 28 29 30 31 |
# File 'lib/tuile/size.rb', line 27 def clamp(max_size) new_width = width.clamp(nil, max_size.width) new_height = height.clamp(nil, max_size.height) new_width == width && new_height == height ? self : Size.new(new_width, new_height) end |
#clamp_height(max_height) ⇒ Size
Clamp height and return a size.
@param max_height — the max height
36 |
# File 'lib/tuile/size.rb', line 36 def clamp_height(max_height) = clamp(Size.new(width, max_height)) |
#empty? ⇒ Boolean
15 16 17 |
# File 'lib/tuile/size.rb', line 15 def empty? width <= 0 || height <= 0 end |
#plus(width, height) ⇒ Size
@param width
@param height
22 |
# File 'lib/tuile/size.rb', line 22 def plus(width, height) = Size.new(self.width + width, self.height + height) |
#to_s ⇒ String
12 |
# File 'lib/tuile/size.rb', line 12 def to_s = "#{width}x#{height}" |