osulyanov
9/15/2017 - 10:09 AM

Get average color of image. In this example `Image` model with `file` file.

Get average color of image. In this example Image model with file file.

  after_commit :set_average_color

  def set_average_color
    return unless self == section.images.first || section.bg_color.blank?
    section.update_attribute :bg_color, get_average_color
  end

  def get_average_color
    img = Magick::Image.read(file.path).first
    pix = img.scale(1, 1)
    pix.to_color(pix.pixel_color(0, 0))
  end