Coder Social home page Coder Social logo

carrierwave-color's Introduction

CarrierWave Color

Add the dominant color of an image to your database whenever you upload it with the CarrierWave gem on your Rails app.

Installation

Add this line to your application's Gemfile:

gem "carrierwave-color"

And then call:

$ bundle

Add a processor to your uploader

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

class PhotoUploader < CarrierWave::Uploader::Base
  include include CarrierWave::Color

  process :store_dominant_color

  # …
end

Add a dominant color attribute

To store the color, you need an attribute on the Rails model where you attach your uploader. Call it {uploader}_dominant_color.

For example if you have the following model:

class Article
  mount_uploader :photo, PhotoUploader
end

Then you would need to add a photo_dominant_color column to the articles table. You could create a migration by executing:

$ rails g migration AddPhotoDominantColorToArticles photo_dominant_color
$ rake db:migrate

Using your dominant color

An easy way of using your dominant colors is to put them as a background to your image tags:

<%= image_tag(article.photo.url(:thumb), style: "background: #{article.photo_dominant_color}") %>

For example, on cults3d:

Cults 3D Animated Screenshot with dominant colors during image load

Resizing

If you do any resizing or if you have several versions of your uploader, store the color on the smallest so that processing goes faster. For example:

# …
version :medium do
  process resize_to_fill: [200, 200]
end

version :thumb, from_version: :medium do
  process resize_to_fill: [42, 42]
  process :store_dominant_color
end
# …

Dominant color from a palette

If you wish to find the dominant color from a selection of colors, you can use the following processor with a {uploader}_palette_color field on your model:

process store_palette_color: ['ff0000', '00ff00', '0000ff']

Contributing

  1. Fork it ( https://github.com/sunny/carrierwave-color/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

carrierwave-color's People

Contributors

sunny avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.