Simplify color extraction code using `bandunfold` (#30869)

pull/2760/head
Claire 2024-06-29 00:41:27 +02:00 committed by GitHub
parent 1bccba1408
commit ba6a558a70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 23 deletions

View File

@ -116,34 +116,23 @@ module Paperclip
# The number of occurrences of a color (r, g, b) is thus encoded in band `b` at pixel position `(r, g)` # The number of occurrences of a color (r, g, b) is thus encoded in band `b` at pixel position `(r, g)`
histogram = image.hist_find_ndim(bins: BINS) histogram = image.hist_find_ndim(bins: BINS)
# `histogram.max` returns an array of maxima with their pixel positions, but we don't know in which # With `bandunfold`, we get back to a (BINS*BINS)×BINS 2D image with a single band.
# band they are # The number of occurrences of a color (r, g, b) is thus encoded at pixel position `(r * BINS + b, g)`
histogram = histogram.bandunfold
_, colors = histogram.max(size: 10, out_array: true, x_array: true, y_array: true) _, colors = histogram.max(size: 10, out_array: true, x_array: true, y_array: true)
colors['out_array'].zip(colors['x_array'], colors['y_array']).map do |v, x, y| colors['x_array'].zip(colors['y_array']).map do |x, y|
rgb_from_xyv(histogram, x, y, v) rgb_from_hist_xy(x, y)
end.flatten.reverse.uniq end.flatten.reverse
end end
# rubocop:disable Naming/MethodParameterName # rubocop:disable Naming/MethodParameterName
def rgb_from_xyv(image, x, y, v) def rgb_from_hist_xy(x, y)
pixel = image.getpoint(x, y) r = ((x / BINS) + 0.5) * 256 / BINS
g = (y + 0.5) * 256 / BINS
# As we only have the first 2 dimensions for this maximum, we b = ((x % BINS) + 0.5) * 256 / BINS
# can't distinguish with different maxima with the same `r` and `g` ColorDiff::Color::RGB.new(r, g, b)
# values but different `b` values.
#
# Therefore, we return an array of maxima, which is always non-empty,
# but may contain multiple colors with the same values.
pixel.filter_map.with_index do |pv, z|
next if pv != v
r = (x + 0.5) * 256 / BINS
g = (y + 0.5) * 256 / BINS
b = (z + 0.5) * 256 / BINS
ColorDiff::Color::RGB.new(r, g, b)
end
end end
def w3c_contrast(color1, color2) def w3c_contrast(color1, color2)