Class: Imatcher::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/imatcher/matcher.rb

Overview

Matcher contains information about compare mode

Constant Summary collapse

MODES =
{
  rgb: 'RGB',
  delta: 'Delta',
  grayscale: 'Grayscale'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Matcher

Returns a new instance of Matcher.



16
17
18
19
20
# File 'lib/imatcher/matcher.rb', line 16

def initialize(options = {})
  mode_type = options.delete(:mode) || :rgb
  fail ArgumentError, "Undefined mode: #{ mode_type }" unless MODES.keys.include?(mode_type)
  @mode = Modes.const_get(MODES[mode_type]).new(options)
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



14
15
16
# File 'lib/imatcher/matcher.rb', line 14

def mode
  @mode
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



14
15
16
# File 'lib/imatcher/matcher.rb', line 14

def threshold
  @threshold
end

Instance Method Details

#compare(a, b) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/imatcher/matcher.rb', line 22

def compare(a, b)
  a = Image.from_file(a) unless a.is_a?(Image)
  b = Image.from_file(b) unless b.is_a?(Image)

  fail SizesMismatchError,
       "Size mismatch: first image size: " \
       "#{a.width}x#{a.height}, " \
       "second image size: " \
       "#{b.width}x#{b.height}" unless a.sizes_match?(b)

  image_area = Rectangle.new(0, 0, a.width - 1, a.height - 1)

  unless mode.exclude_rect.nil?
    fail ArgumentError,
         "Bounds must be in image" unless image_area.contains?(mode.exclude_rect)
  end

  unless mode.include_rect.nil?
    fail ArgumentError,
         "Bounds must be in image" unless image_area.contains?(mode.include_rect)
    unless mode.exclude_rect.nil?
      fail ArgumentError,
           "Included area must contain excluded" unless mode.include_rect.contains?(mode.exclude_rect)
    end
  end

  mode.compare(a, b)
end