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



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



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

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



63
64
65
66
# File 'lib/image_compare/modes/color.rb', line 63

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

#diff(bg, diff) ⇒ Object



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

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

  if area_in_exclude_rect?
    diff_image
  else
    diff_image.highlight_rectangle(bounds)
  end
end

#pixels_equal?(a, b) ⇒ Boolean



52
53
54
55
56
# File 'lib/image_compare/modes/color.rb', line 52

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



58
59
60
61
# File 'lib/image_compare/modes/color.rb', line 58

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