Class: Imatcher::Modes::Grayscale

Inherits:
Base
  • Object
show all
Defined in:
lib/imatcher/modes/grayscale.rb

Overview

Compare pixels by alpha and brightness.

Options:

  • tolerance - defines the maximum allowed difference for alpha/brightness (default value is 16)

Constant Summary collapse

DEFAULT_TOLERANCE =
16

Instance Attribute Summary collapse

Attributes inherited from Base

#result, #threshold

Instance Method Summary collapse

Methods inherited from Base

#calculate_bounds, #compare, #diff, #score

Methods included from ColorMethods

#brightness

Constructor Details

#initialize(options) ⇒ Grayscale

Returns a new instance of Grayscale.



15
16
17
18
# File 'lib/imatcher/modes/grayscale.rb', line 15

def initialize(options)
  @tolerance = options.delete(:tolerance) || DEFAULT_TOLERANCE
  super(options)
end

Instance Attribute Details

#tolerance ⇒ Object (readonly)

Returns the value of attribute tolerance.



13
14
15
# File 'lib/imatcher/modes/grayscale.rb', line 13

def tolerance
  @tolerance
end

Instance Method Details

#background(bg) ⇒ Object



30
31
32
# File 'lib/imatcher/modes/grayscale.rb', line 30

def background(bg)
  bg.to_grayscale
end

#color_similar?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/imatcher/modes/grayscale.rb', line 42

def color_similar?(a, b)
  d = (a - b).abs
  d <= tolerance
end

#create_diff_image(_bg, diff_image) ⇒ Object



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

def create_diff_image(_bg, diff_image)
  diff_image
end

#pixels_diff(d, _a, _b, x, y) ⇒ Object



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

def pixels_diff(d, _a, _b, x, y)
  d[x, y] = rgb(255, 0, 0)
end

#pixels_equal?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/imatcher/modes/grayscale.rb', line 20

def pixels_equal?(a, b)
  alpha = color_similar?(a(a), a(b))
  brightness = color_similar?(brightness(a), brightness(b))
  brightness && alpha
end

#update_result(a, b, x, y) ⇒ Object



26
27
28
# File 'lib/imatcher/modes/grayscale.rb', line 26

def update_result(a, b, x, y)
  @result.diff << [a, b, x, y]
end