Skip to content

Instantly share code, notes, and snippets.

@EdwardIII
Created August 10, 2020 18:06
Show Gist options
  • Save EdwardIII/0c0afefc5f0c697fd92b94353b527cd0 to your computer and use it in GitHub Desktop.
Save EdwardIII/0c0afefc5f0c697fd92b94353b527cd0 to your computer and use it in GitHub Desktop.
require 'rmagick'
require 'pp'
include Magick
image = ImageList.new("source.png")
rgb = image
.color_histogram
.keys
.map {|color| [color.red, color.green, color.blue]}
PP.pp rgb
# outputs
# [[0, 0, 0],
# [7710, 3598, 2056],
# [6425, 6425, 6425],
# [11565, 11565, 11565],
# [7710, 35466, 30583],
# [13107, 60395, 52171],
# [65535, 65535, 65535]]
@havenwood
Copy link

CHUNKS = (2 ** 16).times.each_slice(2 ** 8)
BIG_TO_SMALL = CHUNKS.with_index.reduce({}) do |acc, (numbers, index)|
  acc.merge numbers.to_h { |number| [number, index] }
end

outputs = [[0, 0, 0],
 [7710, 3598, 2056],
 [6425, 6425, 6425],
 [11565, 11565, 11565],
 [7710, 35466, 30583],
 [13107, 60395, 52171],
 [65535, 65535, 65535]]

outputs.map { |rgb| rgb.map &BIG_TO_SMALL }
[[0, 0, 0],
 [30, 14, 8],
 [25, 25, 25],
 [45, 45, 45],
 [30, 138, 119],
 [51, 235, 203],
 [255, 255, 255]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment