lib/capybara/screenshot/diff/image_compare.rb in capybara-screenshot-diff-0.12.0 vs lib/capybara/screenshot/diff/image_compare.rb in capybara-screenshot-diff-0.12.1

- old
+ new

@@ -34,12 +34,14 @@ # Compare the two image files and return `true` or `false` as quickly as possible. # Return falsish if the old file does not exist or the image dimensions do not match. def quick_equal? return nil unless old_file_exists? return true if new_file_size == old_file_size + old_bytes, new_bytes = load_image_files(@old_file_name, @new_file_name) return true if old_bytes == new_bytes + images = load_images(old_bytes, new_bytes) old_bytes = new_bytes = nil # rubocop: disable Lint/UselessAssignment crop_images(images, @dimensions) if @dimensions return false if sizes_changed?(*images) @@ -145,10 +147,12 @@ end def calculate_max_color_distance(new_image, old_image) pixel_pairs = old_image.pixels.zip(new_image.pixels) @max_color_distance = pixel_pairs.inject(0) do |max, (p1, p2)| + next max unless p1 && p2 + d = ChunkyPNG::Color.euclidean_distance_rgba(p1, p2) [max, d].max end end @@ -192,10 +196,11 @@ [old_file, new_file] end def sizes_changed?(org_image, new_image) return unless org_image.dimension != new_image.dimension + change_msg = [org_image, new_image].map { |i| "#{i.width}x#{i.height}" }.join(' => ') puts "Image size has changed for #{@new_file_name}: #{change_msg}" true end @@ -237,10 +242,11 @@ left = @left || old_img.width - 1 right = @right || 0 old_img.height.times do |y| (0...left).find do |x| next if same_color?(old_img, new_img, x, y) + top ||= y bottom = y left = x right = x if x > right x @@ -273,10 +279,11 @@ @max_color_distance = color_distance end color_matches = color_distance == 0 || (@color_distance_limit && @color_distance_limit > 0 && color_distance <= @color_distance_limit) return color_matches if !@shift_distance_limit || @max_shift_distance == Float::INFINITY + shift_distance = if color_matches 0 else shift_distance_at(new_img, old_img, x, y, color_distance_limit: @color_distance_limit) @@ -352,17 +359,19 @@ else bounds_breached += 1 end end break if bounds_breached == 4 + shift_distance += 1 end Float::INFINITY end def color_matches(new_img, org_color, dx, dy, color_distance_limit) new_color = new_img[dx, dy] return new_color == org_color unless color_distance_limit + color_distance = ChunkyPNG::Color.euclidean_distance_rgba(org_color, new_color) color_distance <= color_distance_limit end end end