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

#brightness

Constructor Details

#initialize(threshold: 0.0) ⇒ Base

Returns a new instance of Base.



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

def initialize(threshold: 0.0)
  @threshold = threshold
  @result = Result.new(self, threshold)
end

Instance Attribute Details

#result ⇒ Object (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#threshold ⇒ Object (readonly)

Returns the value of attribute threshold.



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

def threshold
  @threshold
end

Instance Method Details

#calculate_bounds(diff) ⇒ Object

rubocop:disable Metrics/AbcSize



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/imatcher/modes/base.rb', line 39

def calculate_bounds(diff)
  xmin, xmax, ymin, ymax = result.image.width, 0, result.image.height, 0
  diff.each do |pixels_pair|
    xmin = pixels_pair[2] if pixels_pair[2] < xmin
    xmax = pixels_pair[2] if pixels_pair[2] > xmax
    ymin = pixels_pair[3] if pixels_pair[3] < ymin
    ymax = pixels_pair[3] if pixels_pair[3] > ymax
  end

  [xmin - 1, ymin - 1, xmax + 1, ymax + 1]
end

#compare(a, b) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/imatcher/modes/base.rb', line 13

def compare(a, b)
  result.image = a

  b.compare_each_pixel(a) do |b_pixel, a_pixel, x, y|
    next if pixels_equal?(b_pixel, a_pixel)
    update_result(b_pixel, a_pixel, x, y)
  end

  result.score = score
  result
end

#diff(bg, diff) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/imatcher/modes/base.rb', line 25

def diff(bg, diff)
  diff_image = background(bg)
  diff_image.render_bounds(*calculate_bounds(diff))
  diff.each do |pixels_pair|
    pixels_diff(diff_image, *pixels_pair)
  end
  create_diff_image(bg, diff_image)
end

#score ⇒ Object



34
35
36
# File 'lib/imatcher/modes/base.rb', line 34

def score
  @result.diff.length.to_f / @result.image.pixels.length
end