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

#bounds, #exclude_rect, #include_rect, #result, #threshold

Instance Method Summary collapse

Methods inherited from Base

#area, #compare, #diff, #score, #update_bounds

Methods included from ColorMethods

#blue, #brightness, #green, #red

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

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



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

def background(bg)
  bg.to_grayscale
end

#color_similar?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#create_diff_image(_bg, diff_image) ⇒ Object



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

def create_diff_image(_bg, diff_image)
  diff_image
end

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



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

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
29
# File 'lib/imatcher/modes/grayscale.rb', line 26

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