Finding the primary color in an image for Shrine.
Ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/shrine/plugins Allow fetching of multiple colors from palette Jun 29, 2018
spec
.gitignore Add .bundle to ignore Aug 8, 2016
.travis.yml Remove jruby Jun 30, 2018
Gemfile
Gemfile.lock Fix dependency tags Jun 30, 2018
LICENSE
README.md
Rakefile
shrine-color.gemspec

README.md

Shrine Color

Get the dominant color of an image using colorscore (updated version by me). This can be used as a background color while an image is loading or search by color.

This is based on carrierwave-color by Sunny Ripert (sunny).

NOTE: The palettes/dominiant_color grabbed from images may differ between imagemagick versions.

Requirements

This gem requires imagemagick (convert) and grabs colors via a command line. So mini_magick, etc. isn't required.

Installation

Add this line to your application's Gemfile:

gem "shrine-color"

And then call:

$ bundle

Add the plugin to your uploader

In your uploader, include the module and call the processor:

class ImageUploader < Shrine
  # plugin add_metadata should be loaded automagically.
  # otherwise add it here
  plugin :color

  # dominant color
  add_metadata :dominant_color do |io, context|
    dominant_color(io.path)
  end

  # palette color with version, color count and own array of colors.
  add_metadata :palette_color do |io, context|
    if context[:version] == :small
      palette_color(io.path, 3, ['ff0000', '00ff00', '0000ff'])
    end
  end
end

You can now use the code below to get a color:

## Dominant color
photo.image[:small].metadata["dominant_color"]
# or
photo.image[:small].dominant_color