Class: GreenOnion::Compare

Inherits:
Object
  • Object
show all
Defined in:
lib/green_onion/compare.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#changed_pxObject

Returns the value of attribute changed_px.



7
8
9
# File 'lib/green_onion/compare.rb', line 7

def changed_px
  @changed_px
end

#diffed_imageObject (readonly)

Returns the value of attribute diffed_image.



8
9
10
# File 'lib/green_onion/compare.rb', line 8

def diffed_image
  @diffed_image
end

#percentage_changedObject

Returns the value of attribute percentage_changed.



7
8
9
# File 'lib/green_onion/compare.rb', line 7

def percentage_changed
  @percentage_changed
end

#total_pxObject

Returns the value of attribute total_px.



7
8
9
# File 'lib/green_onion/compare.rb', line 7

def total_px
  @total_px
end

Instance Method Details

#channel_difference(chan, pixel, x, y) ⇒ Object

Interface to run the R, G, B methods on ChunkyPNG



48
49
50
# File 'lib/green_onion/compare.rb', line 48

def channel_difference(chan, pixel, x, y)
  ChunkyPNG::Color.send(chan, pixel) + ChunkyPNG::Color.send(chan, @images.last[x,y]) - 2 * [ChunkyPNG::Color.send(chan, pixel), ChunkyPNG::Color.send(chan, @images.last[x,y])].min
end

#diff_images(org, fresh) ⇒ Object

Pulled from Jeff Kreeftmeijer’s post here: jeffkreeftmeijer.com/2011/comparing-images-and-creating-image-diffs/ Thanks Jeff!



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/green_onion/compare.rb', line 12

def diff_images(org, fresh)
  @images = [
    ChunkyPNG::Image.from_file(org),
    ChunkyPNG::Image.from_file(fresh)
  ]

  @diff_index = []
  begin
    diff_iterator
  rescue ChunkyPNG::OutOfBounds
    warn "Skins are different sizes. Please delete #{org} and/or #{fresh}.".color(:yellow)
  end
end

#diff_iteratorObject

Run through all of the pixels on both org image, and fresh image. Change the pixel color accordingly.



27
28
29
30
31
32
33
34
35
36
# File 'lib/green_onion/compare.rb', line 27

def diff_iterator
  @images.first.height.times do |y|
    @images.first.row(y).each_with_index do |pixel, x|
      unless pixel == @images.last[x,y]
        @diff_index << [x,y] 
        pixel_difference_filter(pixel, x, y)
      end
    end
  end
end

#percentage_diff(org, fresh) ⇒ Object

Returns the numeric results of the diff of 2 images



53
54
55
56
57
58
# File 'lib/green_onion/compare.rb', line 53

def percentage_diff(org, fresh)
  diff_images(org, fresh)
  @total_px = @images.first.pixels.length
  @changed_px = @diff_index.length
  @percentage_changed = ( (@diff_index.length.to_f / @images.first.pixels.length) * 100 ).round(2)
end

#pixel_difference_filter(pixel, x, y) ⇒ Object

Changes the pixel color to be the opposite RGB value



39
40
41
42
43
44
45
# File 'lib/green_onion/compare.rb', line 39

def pixel_difference_filter(pixel, x, y)
  chans = []
  [:r, :b, :g].each do |chan| 
    chans << channel_difference(chan, pixel, x, y)
  end
  @images.last[x,y] = ChunkyPNG::Color.rgb(chans[0], chans[1], chans[2])
end

#save_visual_diff(org, fresh) ⇒ Object

Saves the visual diff as a separate file



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/green_onion/compare.rb', line 67

def save_visual_diff(org, fresh)
  x, y = @diff_index.map{ |xy| xy[0] }, @diff_index.map{ |xy| xy[1] }
  @diffed_image = org.insert(-5, '_diff')

  begin
    @images.last.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color.rgb(0,255,0))
  rescue NoMethodError
    puts "Both skins are the same.".color(:yellow)
  end
  
  @images.last.save(@diffed_image)
end

#visual_diff(org, fresh) ⇒ Object

Returns the visual results of the diff of 2 images



61
62
63
64
# File 'lib/green_onion/compare.rb', line 61

def visual_diff(org, fresh)
  diff_images(org, fresh)
  save_visual_diff(org, fresh)
end