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

#boundsObject (readonly)

Returns the value of attribute bounds.



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

def bounds
  @bounds
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#thresholdObject (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

#compare(a, b) ⇒ Object



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

def compare(a, b)
  result.image = a
  @bounds = [0, 0, result.image.width - 1, result.image.height - 1]

  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



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

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

#scoreObject



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

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

#update_bounds(x, y) ⇒ Object



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

def update_bounds(x, y)
  bounds[0] = [x, bounds[0]].max
  bounds[1] = [y, bounds[1]].max
  bounds[2] = [x, bounds[2]].min
  bounds[3] = [y, bounds[3]].min
end

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



38
39
40
# File 'lib/imatcher/modes/base.rb', line 38

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