prizm 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/prizm.rb +10 -13
  2. data/lib/prizm/version.rb +1 -1
  3. data/test/test.rb +4 -5
  4. metadata +3 -3
@@ -3,34 +3,31 @@ require 'RMagick'
3
3
 
4
4
  module Prizm
5
5
  class Extractor
6
- def get_colors(img_location, nc=6, remove_bg=true)
7
- begin
8
- image = remove_bg ? remove_bg(img_location, nc) : reduce_size(Magick::ImageList.new(img_location), 100, 100)
6
+ def initialize(img_location)
7
+ @image = Magick::ImageList.new(img_location)
8
+ end
9
+
10
+ def get_colors(nc=6, remove_bg=true)
11
+ image = remove_bg ? remove_bg(nc) : reduce_size(100, 100)
9
12
  q = image.quantize(nc)
10
13
  colors = q.color_histogram.sort {|a, b| b[1] <=> a[1]}.map { |color, count| color }
11
- rescue Magick::ImageMagickError => e
12
- puts e.message
13
- puts e.backtrace
14
- nil
15
- end
16
14
  end
17
15
 
18
16
  def to_hex(pixel)
19
17
  pixel.to_color(Magick::AllCompliance, false, 8)
20
18
  end
21
19
 
22
- def reduce_size(image, width, height)
23
- image.change_geometry!("#{width}x#{height}") { |cols, rows, img| img.resize!(cols, rows) }
20
+ def reduce_size(width, height)
21
+ @image.change_geometry!("#{width}x#{height}") { |cols, rows, img| img.resize!(cols, rows) }
24
22
  end
25
23
 
26
- def remove_bg(img_location, nc, fuzz=20)
27
- image = reduce_size Magick::ImageList.new(img_location), 100, 100
24
+ def remove_bg(nc, fuzz=20)
25
+ image = reduce_size(100, 100)
28
26
  image = image.border(1, 1, image.pixel_color(0,0))
29
27
  image = image.quantize(nc+4)
30
28
  image.fuzz = "#{fuzz}%"
31
29
  a = image.color_floodfill(0, 0, image.pixel_color(0,0))
32
30
  a = a.matte_floodfill(0,0)
33
- a
34
31
  end
35
32
  end
36
33
  end
@@ -1,3 +1,3 @@
1
1
  module Prizm
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -3,18 +3,17 @@ require 'test/unit'
3
3
 
4
4
  class TestExtractor < Test::Unit::TestCase
5
5
  def test_functional
6
- colors = Prizm::Extractor.new.get_colors("#{File.dirname(__FILE__)}/rainbow.jpg", 6, false)
6
+ colors = Prizm::Extractor.new("#{File.dirname(__FILE__)}/rainbow.jpg").get_colors(6, false)
7
7
  assert_equal(6, colors.size)
8
8
  end
9
9
 
10
10
  def test_to_hex
11
- extr = Prizm::Extractor.new
12
- colors = extr.get_colors("#{File.dirname(__FILE__)}/rainbow.jpg", 6, false)
11
+ extr = Prizm::Extractor.new("#{File.dirname(__FILE__)}/rainbow.jpg")
12
+ colors = extr.get_colors(6, false)
13
13
  assert_equal('#322D31', extr.to_hex(colors[0]))
14
14
  end
15
15
 
16
16
  def test_error
17
- c = Prizm::Extractor.new.get_colors("blahblah.jpg")
18
- assert_equal(nil, c)
17
+ assert_raise(Magick::ImageMagickError) { Prizm::Extractor.new("blahblah.jpg").get_colors }
19
18
  end
20
19
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prizm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Quan Nguyen