chunky_png 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
3
3
 
4
4
  # Do not change the version and date fields by hand. This will be done
5
5
  # automatically by the gem release script.
6
- s.version = "0.5.5"
6
+ s.version = "0.5.6"
7
7
  s.date = "2010-02-16"
8
8
 
9
9
  s.summary = "Pure ruby library for read/write, chunk-level access to PNG files"
@@ -26,7 +26,7 @@ module ChunkyPNG
26
26
 
27
27
  # The current version of ChunkyPNG. This value will be updated automatically
28
28
  # by them gem:release rake task.
29
- VERSION = "0.5.5"
29
+ VERSION = "0.5.6"
30
30
 
31
31
  ###################################################
32
32
  # PNG international standard defined constants
@@ -238,23 +238,84 @@ module ChunkyPNG
238
238
  (color & 0xffffff00) | new_alpha
239
239
  end
240
240
 
241
+ # Decomposes a color, given a color, a mask color and a background color.
242
+ # The returned color will be a variant of the mask color, with the alpha
243
+ # channel set to the best fitting value. This basically is the reverse
244
+ # operation if alpha composition.
245
+ #
246
+ # If the color cannot be decomposed, this method will return the fully
247
+ # transparentvariant of the mask color.
248
+ #
249
+ # @param [Fixnum] color The color that was the result of compositing.
250
+ # @param [Fixnum] mask The opaque variant of the color that was being composed
251
+ # @param [Fixnum] bg The background color on which the color was composed.
252
+ # @param [Fixnum] tolerance The decomposition tolerance level, a value between 0 and 255.
253
+ # @return [Fixnum] The decomposed color,a variant of the masked color with the
254
+ # alpha channel set to an appropriate value.
255
+ def decompose_color(color, mask, bg, tolerance = 1)
256
+ if alpha_decomposable?(color, mask, bg, tolerance)
257
+ mask & 0xffffff00 | decompose_alpha(color, mask, bg)
258
+ else
259
+ mask & 0xffffff00
260
+ end
261
+ end
262
+
263
+ # Checks whether an alpha channel value can successfully be composed
264
+ # given the resulting color, the mask color and a background color,
265
+ # all of which should be opaque.
266
+ #
267
+ # @param [Fixnum] color The color that was the result of compositing.
268
+ # @param [Fixnum] mask The opauqe variant of the color that was being composed
269
+ # @param [Fixnum] bg The background color on which the color was composed.
270
+ # @param [Fixnum] tolerance The decomposition tolerance level, a value between 0 and 255.
271
+ # @return [Fixnum] The decomposed alpha channel value, between 0 and 255.
272
+ # @see #decompose_alpha
241
273
  def alpha_decomposable?(color, mask, bg, tolerance = 1)
242
274
  components = decompose_alpha_components(color, mask, bg)
243
275
  sum = components.inject(0) { |a,b| a + b }
244
276
  max = components.max * 3
245
- return (sum + tolerance * 3) >= max
277
+ return components.max <= 255 && components.min >= 0 && (sum + tolerance * 3) >= max
246
278
  end
247
279
 
280
+ # Decomposes the alpha channel value given the resulting color, the mask color
281
+ # and a background color, all of which should be opaque.
282
+ #
283
+ # Make sure to call {#alpha_decomposable?} first to see if the alpha channel
284
+ # value can successfully decomposed with a given tolerance, otherwise the return
285
+ # value of this method is undefined.
286
+ #
287
+ # @param [Fixnum] color The color that was the result of compositing.
288
+ # @param [Fixnum] mask The opauqe variant of the color that was being composed
289
+ # @param [Fixnum] bg The background color on which the color was composed.
290
+ # @return [Fixnum] The best fitting alpha channel, a value between 0 and 255.
291
+ # @see #alpha_decomposable?
248
292
  def decompose_alpha(color, mask, bg)
249
293
  components = decompose_alpha_components(color, mask, bg)
250
294
  (components.inject(0) { |a,b| a + b } / 3.0).round
251
295
  end
252
296
 
297
+ # Decomposes an alpha channel for either the r, g or b color channel.
298
+ # @param [:r, :g, :b] The channel to decompose the alpha channel from.
299
+ # @param [Fixnum] color The color that was the result of compositing.
300
+ # @param [Fixnum] mask The opauqe variant of the color that was being composed
301
+ # @param [Fixnum] bg The background color on which the color was composed.
302
+ # @param [Fixnum] The decomposed alpha value for the channel.
303
+ def decompose_alpha_component(channel, color, mask, bg)
304
+ ((send(channel, bg) - send(channel, color)).to_f /
305
+ (send(channel, bg) - send(channel, mask)).to_f * MAX).round
306
+ end
307
+
308
+ # Decomposes the alpha channels for the r, g and b color channel.
309
+ # @param [Fixnum] color The color that was the result of compositing.
310
+ # @param [Fixnum] mask The opauqe variant of the color that was being composed
311
+ # @param [Fixnum] bg The background color on which the color was composed.
312
+ # @return [Array<Fixnum>] The decomposed alpha values for the r, g and b channels.
253
313
  def decompose_alpha_components(color, mask, bg)
254
- a_r = ((r(bg) - r(color)).to_f / (r(bg) - r(mask)).to_f * MAX).round
255
- a_g = ((g(bg) - g(color)).to_f / (g(bg) - g(mask)).to_f * MAX).round
256
- a_b = ((b(bg) - b(color)).to_f / (b(bg) - b(mask)).to_f * MAX).round
257
- [a_r, a_g, a_b]
314
+ [
315
+ decompose_alpha_component(:r, color, mask, bg),
316
+ decompose_alpha_component(:g, color, mask, bg),
317
+ decompose_alpha_component(:b, color, mask, bg)
318
+ ]
258
319
  end
259
320
 
260
321
  ####################################################################
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chunky_png
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willem van Bergen