Class: LooksGood::Comparison

Inherits:
Object
  • Object
show all
Defined in:
lib/looks_good/comparison.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actual_image, expected_image, within = LooksGood::Configuration.default_within) ⇒ Comparison



7
8
9
10
11
12
13
14
15
# File 'lib/looks_good/comparison.rb', line 7

def initialize(actual_image, expected_image, within=LooksGood::Configuration.default_within)
  @actual_image = actual_image
  @expected_image = expected_image
  @comparison = compare_image
  @within = within
  # within is a float of % image difference between the two images
  @match = @comparison <= @within
  @diff_image =LooksGood::Image.new(@diff_image_file, @expected_image.file_name)
end

Instance Attribute Details

#actual_imageObject

Returns the value of attribute actual_image.



5
6
7
# File 'lib/looks_good/comparison.rb', line 5

def actual_image
  @actual_image
end

#diff_imageObject

Returns the value of attribute diff_image.



5
6
7
# File 'lib/looks_good/comparison.rb', line 5

def diff_image
  @diff_image
end

#diff_image_fileObject

Returns the value of attribute diff_image_file.



5
6
7
# File 'lib/looks_good/comparison.rb', line 5

def diff_image_file
  @diff_image_file
end

#expected_imageObject

Returns the value of attribute expected_image.



5
6
7
# File 'lib/looks_good/comparison.rb', line 5

def expected_image
  @expected_image
end

Instance Method Details

#compare_imageObject



29
30
31
# File 'lib/looks_good/comparison.rb', line 29

def compare_image
  compare_images_with_same_size? ? compare_images_with_same_size : compare_images_with_different_size
end

#compare_images_with_different_sizeObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/looks_good/comparison.rb', line 38

def compare_images_with_different_size
  row = [@actual_image.image.rows, @expected_image.image.rows].max
  column = [@actual_image.image.columns, @expected_image.image.columns].max

  images_to_compare = prep_images_for_comparison do |image|
    expanded_image = image.extent(column, row)
    expanded_image.background_color = 'white'
    expanded_image
  end
  mini_compare(images_to_compare)
end

#compare_images_with_same_sizeObject



33
34
35
36
# File 'lib/looks_good/comparison.rb', line 33

def compare_images_with_same_size
  images_to_compare = prep_images_for_comparison
  mini_compare(images_to_compare)
end

#compare_images_with_same_size?Boolean



50
51
52
# File 'lib/looks_good/comparison.rb', line 50

def compare_images_with_same_size?
  @actual_image.image.rows == @expected_image.image.rows && @actual_image.image.columns == @expected_image.image.columns
end

#matches?Boolean



17
18
19
# File 'lib/looks_good/comparison.rb', line 17

def matches?
  @match
end

#mini_compare(images) ⇒ Object

drop in refactor to utilize mini_magick. returns a float % of difference TODO: remove rmagick



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/looks_good/comparison.rb', line 57

def mini_compare(images)
  actual_image, expected_image = images
  actual_image.write(actual_image.filename)
  pixel_difference_count = nil
  MiniMagick::Tool::Compare.new(whiny: false) do |comp|
    comp.fuzz(LooksGood::Configuration.fuzz)  
    comp.metric("AE")
    comp << actual_image.filename  
    comp << expected_image.filename
    diff_file_name = File.join(LooksGood::Configuration.path(:diff), @actual_image.file_name)
    FileUtils::mkdir_p(File.dirname(diff_file_name)) unless File.exists?(diff_file_name)
    comp << diff_file_name
    comp.call do |stdout, stderr, status|
     pixel_difference_count = stderr
    end
    @diff_image_file = Magick::Image.read(diff_file_name).first
  end
  pixel_difference_count.to_f / (expected_image.rows * expected_image.columns)
end

#percent_differenceObject



25
26
27
# File 'lib/looks_good/comparison.rb', line 25

def percent_difference
  @comparison
end

#prep_images_for_comparisonObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/looks_good/comparison.rb', line 77

def prep_images_for_comparison
  [
      @actual_image,
      @expected_image,
  ].collect do |looks_good_image|
    image = looks_good_image.image.clone
    image = yield image if block_given?

    # Important: ensure the image 0,0 is reset to the top-left of the image before comparison
    image.offset = 0
    image
  end
end

#withinObject



21
22
23
# File 'lib/looks_good/comparison.rb', line 21

def within
  @within
end