Class: Happo::SnapshotComparisonImage::Base
- Inherits:
-
Object
- Object
- Happo::SnapshotComparisonImage::Base
- Includes:
- ChunkyPNG::Color
- Defined in:
- lib/happo/snapshot_comparison_image/base.rb
Overview
This model represents a “comparison image”. Basically it’s just a wrapper around a ChunkyPNG image with some nice methods to make life easier in the world of diffs.
This model is never persisted.
Constant Summary collapse
- BASE_OPACITY =
0.1
- BASE_ALPHA =
(255 * BASE_OPACITY).round
- BASE_DIFF_ALPHA =
BASE_ALPHA * 2
- MAGENTA =
ChunkyPNG::Color.from_hex '#b33682'
- RED =
ChunkyPNG::Color.from_hex '#dc322f'
- GREEN =
ChunkyPNG::Color.from_hex '#859900'
Instance Method Summary collapse
-
#diff_alpha(diff_score) ⇒ Integer
A number between 0 and 255 that represents the alpha channel of of the difference.
-
#initialize(offset, canvas) ⇒ Base
constructor
A new instance of Base.
-
#pixel_diff_score(pixel_after, pixel_before) ⇒ Float
Compute a score that represents the difference between 2 pixels.
- #render_added_row(y, row) ⇒ Object
- #render_changed_row(y, row) ⇒ Object
- #render_deleted_row(y, row) ⇒ Object
-
#render_pixel(x, y, pixel) ⇒ Object
Renders a pixel on the specified x and y position.
- #render_row(y, row) ⇒ Object
- #render_unchanged_row(y, row) ⇒ Object
Constructor Details
#initialize(offset, canvas) ⇒ Base
24 25 26 27 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 24 def initialize(offset, canvas) @offset = offset @canvas = canvas end |
Instance Method Details
#diff_alpha(diff_score) ⇒ Integer
93 94 95 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 93 def diff_alpha(diff_score) (BASE_DIFF_ALPHA + ((255 - BASE_DIFF_ALPHA) * diff_score)).round end |
#pixel_diff_score(pixel_after, pixel_before) ⇒ Float
Compute a score that represents the difference between 2 pixels
This method simply takes the Euclidean distance between the RGBA channels of 2 colors over the maximum possible Euclidean distance. This gives us a percentage of how different the two colors are.
Although it would be more perceptually accurate to calculate a proper Delta E in Lab colorspace, we probably don’t need perceptual accuracy for this application, and it is nice to avoid the overhead of converting RGBA to Lab.
85 86 87 88 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 85 def pixel_diff_score(pixel_after, pixel_before) ChunkyPNG::Color::euclidean_distance_rgba(pixel_after, pixel_before) / ChunkyPNG::Color::MAX_EUCLIDEAN_DISTANCE_RGBA end |
#render_added_row(y, row) ⇒ Object
60 61 62 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 60 def render_added_row(y, row) # no default implementation end |
#render_changed_row(y, row) ⇒ Object
54 55 56 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 54 def render_changed_row(y, row) # no default implementation end |
#render_deleted_row(y, row) ⇒ Object
66 67 68 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 66 def render_deleted_row(y, row) # no default implementation end |
#render_pixel(x, y, pixel) ⇒ Object
Renders a pixel on the specified x and y position. Uses the offset that the comparison image has been configured with.
103 104 105 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 103 def render_pixel(x, y, pixel) @canvas.set_pixel(x + @offset, y, pixel) end |
#render_row(y, row) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 31 def render_row(y, row) if row.unchanged? render_unchanged_row(y, row) elsif row.deleting? render_deleted_row(y, row) elsif row.adding? render_added_row(y, row) else # changing? render_changed_row(y, row) end end |
#render_unchanged_row(y, row) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/happo/snapshot_comparison_image/base.rb', line 45 def render_unchanged_row(y, row) row.new_element.each_with_index do |pixel, x| # Render the unchanged pixel as-is render_pixel(x, y, pixel) end end |