Sha256: 5286aacddad0f06ba295e53d1c24855bbe5a7556c93f77aaa9aa3dc0b2b61e77

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# -*- encoding: utf-8 -*-
require 'minitest/autorun'
require 'minitest/unit'
require 'minitest/spec'
require 'minitest/pride'
require 'minitest/matchers'
require 'tempfile'

require 'placeholder_image'

# References:
#   - MiniMagick: https://github.com/probablycorey/mini_magick/blob/master/test/image_test.rb
#   - ChunkyPNG: https://github.com/wvanbergen/chunky_png/blob/master/spec/chunky_png/color_spec.rb
#   - RMagick: http://www.imagemagick.org/RMagick/doc/draw.html

module PlaceholderImage
  module SpecHelpers
    module RMagick

      def open_image(path)
        ::Magick::ImageList.new(path).flatten_images
      end

      def colors(image)
        pixels = {}

        image.each_pixel do |pixel, c, r|
          # RMagick's API just kills me...
          color = pixel.to_color(compliance=::Magick::AllCompliance, matte=true, depth=8, hex=true)

          if pixels[color]
            pixels[color] += 1
          else
            pixels[color] = 1
          end
        end

        pixels.sort_by { |k,v| v }.reverse.map { |v| v[0].downcase }
      end

      def background_color(image)
        colors(image)[0]
      end

      def text_color(image)
        colors(image)[1]
      end

      def dimensions(image)
        [image.columns, image.rows]
      end

      def format(image)
        image.format.downcase
      end

      def fixture_image(filename)
        filename = File.join(File.dirname(__FILE__), 'fixtures', filename)
        open_image(filename)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
merchii-placeholder_image-0.1.0 spec/spec_helper.rb