Class: ChunkyPNG::Dimension

Inherits:
Object
  • Object
show all
Defined in:
lib/chunky_png/dimension.rb

Overview

Class that represents the dimension of something, e.g. a Canvas.

This class contains some methods to simplify performing dimension related checks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Dimension

Initializes a new dimension instance.

Parameters:

  • width (Integer)

    The width-component of the new dimension.

  • height (Integer)

    The height-component of the new dimension.



69
70
71
# File 'lib/chunky_png/dimension.rb', line 69

def initialize(width, height)
  @width, @height = width.to_i, height.to_i
end

Instance Attribute Details

#heightInteger

Returns The height-component of this dimension.

Returns:

  • (Integer)

    The height-component of this dimension.



64
65
66
# File 'lib/chunky_png/dimension.rb', line 64

def height
  @height
end

#widthInteger

Returns The width-component of this dimension.

Returns:

  • (Integer)

    The width-component of this dimension.



61
62
63
# File 'lib/chunky_png/dimension.rb', line 61

def width
  @width
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Compares the size of 2 dimensions.

Parameters:

Returns:

  • (-1, 0, 1)

    -1 if the other dimension has a larger area, 1 of this dimension is larger, 0 if both are identical in size.



101
102
103
# File 'lib/chunky_png/dimension.rb', line 101

def <=>(other)
  other.area <=> area
end

#areaInteger

Returns the area of this dimension.

Returns:

  • (Integer)

    The area in number of pixels.



75
76
77
# File 'lib/chunky_png/dimension.rb', line 75

def area
  width * height
end

#eql?(other) ⇒ true, false Also known as: ==

Checks whether 2 dimensions are identical.

Parameters:

Returns:

  • (true, false)

    true iff width and height match.



91
92
93
# File 'lib/chunky_png/dimension.rb', line 91

def eql?(other)
  other.width == width && other.height == height
end

#include?(*point_like) ⇒ true, false

Checks whether a point is within bounds of this dimension.

Parameters:

Returns:

  • (true, false)

    True iff the x and y coordinate fall in this dimension.

See Also:



83
84
85
86
# File 'lib/chunky_png/dimension.rb', line 83

def include?(*point_like)
  point = ChunkyPNG::Point(*point_like)
  point.x >= 0 && point.x < width && point.y >= 0 && point.y < height
end

#to_aArray<Integer> Also known as: to_ary

Casts this dimension into an array.

Returns:

  • (Array<Integer>)

    [width, height] for this dimension.



107
108
109
# File 'lib/chunky_png/dimension.rb', line 107

def to_a
  [width, height]
end