Class: ImageCompare::Modes::Color

Inherits:
Base
  • Object
show all
Includes:
ColorMethods
Defined in:
lib/image_compare/modes/color.rb

Constant Summary collapse

DEFAULT_TOLERANCE =
16

Instance Attribute Summary collapse

Attributes inherited from Base

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

Instance Method Summary collapse

Methods included from ColorMethods

#blue, #brightness, #green, #orange, #red, #transparent, #yellow

Methods inherited from Base

#area, #compare, #score, #update_bounds

Constructor Details

#initialize(**options) ⇒ Color

Returns a new instance of Color.



14
15
16
17
# File 'lib/image_compare/modes/color.rb', line 14

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

Instance Attribute Details

#toleranceObject (readonly)

Returns the value of attribute tolerance.



12
13
14
# File 'lib/image_compare/modes/color.rb', line 12

def tolerance
  @tolerance
end

Instance Method Details

#area_in_exclude_rect?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/image_compare/modes/color.rb', line 27

def area_in_exclude_rect?
  return false if exclude_rect.nil?

  diff_area = {
    left: bounds.bounds[0],
    top: bounds.bounds[1],
    right: bounds.bounds[2],
    bot: bounds.bounds[3]
  }

  exclude_area = {
    left: exclude_rect.bounds[0],
    top: exclude_rect.bounds[1],
    right: exclude_rect.bounds[2],
    bot: exclude_rect.bounds[3]
  }

  diff_area[:left] <= exclude_area[:left] &&
    diff_area[:top] <= exclude_area[:top] &&
    diff_area[:right] >= exclude_area[:right] &&
    diff_area[:bot] >= exclude_area[:bot]
end

#color_similar?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/image_compare/modes/color.rb', line 61

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

#diff(bg, diff) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/image_compare/modes/color.rb', line 19

def diff(bg, diff)
  diff_image = bg.highlight_rectangle(exclude_rect, :blue)

  return diff_image if self.result.match?

  diff_image.highlight_rectangle(bounds) unless area_in_exclude_rect?
end

#pixels_equal?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/image_compare/modes/color.rb', line 50

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



56
57
58
59
# File 'lib/image_compare/modes/color.rb', line 56

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