Class: ImageCompare::Matcher

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

Constant Summary collapse

MODES =
{
  rgb: "RGB",
  delta: "Delta",
  grayscale: "Grayscale",
  color: "Color"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Matcher

Returns a new instance of Matcher.

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/image_compare/matcher.rb', line 18

def initialize(**options)
  mode_type = options.delete(:mode) || :color
  raise ArgumentError, "Undefined mode: #{mode_type}" unless MODES.key?(mode_type)
  @mode = Modes.const_get(MODES[mode_type]).new(**options)
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



16
17
18
# File 'lib/image_compare/matcher.rb', line 16

def mode
  @mode
end

Instance Method Details

#compare(a, b) ⇒ Object



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
50
51
52
53
54
55
# File 'lib/image_compare/matcher.rb', line 24

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

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

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

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

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

  mode.compare(a, b)
end