Class: Imatcher::Modes::Base

Inherits:
Object
  • Object
show all
Includes:
ColorMethods
Defined in:
lib/imatcher/modes/base.rb

Overview

:nodoc:

Direct Known Subclasses

Delta, Grayscale, RGB

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColorMethods

#blue, #brightness, #green, #red

Constructor Details

#initialize(threshold: 0.0, exclude_rect: nil, include_rect: nil) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
# File 'lib/imatcher/modes/base.rb', line 9

def initialize(threshold: 0.0, exclude_rect: nil, include_rect: nil)
  @include_rect = Rectangle.new(*include_rect) unless include_rect.nil?
  @exclude_rect = Rectangle.new(*exclude_rect) unless exclude_rect.nil?
  @threshold = threshold
  @result = Result.new(self, threshold)
end

Instance Attribute Details

#boundsObject (readonly)

Returns the value of attribute bounds.



7
8
9
# File 'lib/imatcher/modes/base.rb', line 7

def bounds
  @bounds
end

#exclude_rectObject (readonly)

Returns the value of attribute exclude_rect.



7
8
9
# File 'lib/imatcher/modes/base.rb', line 7

def exclude_rect
  @exclude_rect
end

#include_rectObject (readonly)

Returns the value of attribute include_rect.



7
8
9
# File 'lib/imatcher/modes/base.rb', line 7

def include_rect
  @include_rect
end

#resultObject (readonly)

Returns the value of attribute result.



7
8
9
# File 'lib/imatcher/modes/base.rb', line 7

def result
  @result
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



7
8
9
# File 'lib/imatcher/modes/base.rb', line 7

def threshold
  @threshold
end

Instance Method Details

#areaObject



56
57
58
59
60
# File 'lib/imatcher/modes/base.rb', line 56

def area
  area = include_rect.area
  return area if exclude_rect.nil?
  area - exclude_rect.area
end

#compare(a, b) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/imatcher/modes/base.rb', line 16

def compare(a, b)
  result.image = a
  @include_rect ||= a.bounding_rect
  @bounds = Rectangle.new(*include_rect.bounds)

  b.compare_each_pixel(a, area: include_rect) do |b_pixel, a_pixel, x, y|
    next if pixels_equal?(b_pixel, a_pixel)
    next if !exclude_rect.nil? && exclude_rect.contains_point?(x, y)
    update_result(b_pixel, a_pixel, x, y)
  end

  result.score = score
  result
end

#diff(bg, diff) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/imatcher/modes/base.rb', line 31

def diff(bg, diff)
  diff_image = background(bg).highlight_rectangle(exclude_rect, :blue)
  diff.each do |pixels_pair|
    pixels_diff(diff_image, *pixels_pair)
  end
  create_diff_image(bg, diff_image).
    highlight_rectangle(bounds).
    highlight_rectangle(include_rect, :green)
end

#scoreObject



41
42
43
# File 'lib/imatcher/modes/base.rb', line 41

def score
  result.diff.length.to_f / area
end

#update_bounds(x, y) ⇒ Object



49
50
51
52
53
54
# File 'lib/imatcher/modes/base.rb', line 49

def update_bounds(x, y)
  bounds.left = [x, bounds.left].max
  bounds.top = [y, bounds.top].max
  bounds.right = [x, bounds.right].min
  bounds.bot = [y, bounds.bot].min
end

#update_result(*_args, x, y) ⇒ Object



45
46
47
# File 'lib/imatcher/modes/base.rb', line 45

def update_result(*_args, x, y)
  update_bounds(x, y)
end