chunky_png 0.5.5 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/chunky_png.gemspec +1 -1
- data/lib/chunky_png.rb +1 -1
- data/lib/chunky_png/color.rb +66 -5
- metadata +1 -1
data/chunky_png.gemspec
CHANGED
@@ -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.
|
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"
|
data/lib/chunky_png.rb
CHANGED
@@ -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.
|
29
|
+
VERSION = "0.5.6"
|
30
30
|
|
31
31
|
###################################################
|
32
32
|
# PNG international standard defined constants
|
data/lib/chunky_png/color.rb
CHANGED
@@ -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
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
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
|
####################################################################
|