Class: Tuile::Size

Inherits:
Object
  • Object
show all
Defined in:
lib/tuile/size.rb

Overview

A size with integer ‘width` and `height`.

Constant Summary collapse

ZERO =

An empty size constant.

Returns:

Size.new(0, 0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heightInteger (readonly)

Returns height.

Returns:

  • (Integer)

    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

#widthInteger (readonly)

Returns width.

Returns:

  • (Integer)

    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.

Parameters:

  • max_size (Size)

    the max size

Returns:



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.

Parameters:

  • max_height (Integer)

    the max height

Returns:



36
# File 'lib/tuile/size.rb', line 36

def clamp_height(max_height) = clamp(Size.new(width, max_height))

#empty?Boolean

Returns true if either #width or #height is zero or negative.

Returns:

  • (Boolean)

    true if either #width or #height is zero or negative.



15
16
17
# File 'lib/tuile/size.rb', line 15

def empty?
  width <= 0 || height <= 0
end

#plus(width, height) ⇒ Size

Parameters:

  • width (Integer)
  • height (Integer)

Returns:



22
# File 'lib/tuile/size.rb', line 22

def plus(width, height) = Size.new(self.width + width, self.height + height)

#to_sString

Returns:

  • (String)


12
# File 'lib/tuile/size.rb', line 12

def to_s = "#{width}x#{height}"