Module: PhotoCook::Pixels

Defined in:
lib/photo-cook/pixels.rb

Defined Under Namespace

Classes: Invalid, OutOfBounds

Constant Summary collapse

MAX =
9999.freeze

Class Method Summary collapse

Class Method Details

.check!(x, ensure_in_bounds = true) ⇒ Object

Raises:



18
19
20
21
22
23
# File 'lib/photo-cook/pixels.rb', line 18

def check!(x, ensure_in_bounds = true)
  x = typecast(x)
  raise Invalid,     x if !x.kind_of?(Integer) && (x.nan? || x.infinite?)
  raise OutOfBounds, x if ensure_in_bounds && !in_bounds?(x)
  true
end

.in_bounds?(x) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/photo-cook/pixels.rb', line 25

def in_bounds?(x)
  x = typecast(x)
  0 < x && x <= MAX
end

.parse(x) ⇒ Object



9
10
11
# File 'lib/photo-cook/pixels.rb', line 9

def parse(x)
  round(typecast(x))
end

.parse!(x) ⇒ Object



13
14
15
16
# File 'lib/photo-cook/pixels.rb', line 13

def parse!(x)
  check!(x = parse(x))
  x
end

.round(x) ⇒ Object

Standardize how dimensions are rounded in PhotoCook



31
32
33
# File 'lib/photo-cook/pixels.rb', line 31

def round(x)
  x.floor
end

.to_magick_dimensions(width, height) ⇒ Object

Returns Imagemagick dimension-string



36
37
38
# File 'lib/photo-cook/pixels.rb', line 36

def to_magick_dimensions(width, height)
  "#{width}x#{height}"
end

.typecast(x) ⇒ Object



40
41
42
# File 'lib/photo-cook/pixels.rb', line 40

def typecast(x)
  x.kind_of?(Numeric) ? x : x.to_f
end