Class: ColorCat::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/color_cat/process.rb

Constant Summary collapse

NUMBER_OF_COLORS =
6

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ Process

Returns a new instance of Process.



9
10
11
12
13
# File 'lib/color_cat/process.rb', line 9

def initialize(url, opts={})
  @url = url
  @opts = opts
  @color_details = []
end

Instance Attribute Details

#color_detailsObject (readonly)

Returns the value of attribute color_details.



5
6
7
# File 'lib/color_cat/process.rb', line 5

def color_details
  @color_details
end

#optsObject (readonly)

Returns the value of attribute opts.



5
6
7
# File 'lib/color_cat/process.rb', line 5

def opts
  @opts
end

#originalObject (readonly)

Returns the value of attribute original.



5
6
7
# File 'lib/color_cat/process.rb', line 5

def original
  @original
end

#primaryObject (readonly)

Returns the value of attribute primary.



5
6
7
# File 'lib/color_cat/process.rb', line 5

def primary
  @primary
end

#quantizedObject (readonly)

Returns the value of attribute quantized.



5
6
7
# File 'lib/color_cat/process.rb', line 5

def quantized
  @quantized
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/color_cat/process.rb', line 5

def url
  @url
end

Class Method Details

.call(url, opts = {}) ⇒ Object



25
26
27
# File 'lib/color_cat/process.rb', line 25

def self.call(url, opts={})
  new(url, opts).call
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
# File 'lib/color_cat/process.rb', line 15

def call
  begin
    process_image
    normal = sort_by_decreasing_frequency(quantized)
    get_pix(normal)
  rescue OpenURI::HTTPError, Magick::ImageMagickError
    # TODO logging
  end
end

#color_category(my_rgb) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/color_cat/process.rb', line 29

def color_category(my_rgb)
  tmp_dist = 255*3
  tmp_name = "none"
  tmp_category_color = "none"

  Colors.each_pair do |name, rgb|
    category_color = rgb[0]
    rgb = rgb[1]
    total_dist = Math.sqrt((rgb[0] - my_rgb[0]) ** 2 + (rgb[1] - my_rgb[1]) ** 2 + (rgb[2] - my_rgb[2]) ** 2)

    if (total_dist < tmp_dist)
      tmp_name = name
      tmp_category_color = category_color
      tmp_dist = total_dist
    end
  end

  [tmp_category_color, tmp_name]
end

#color_infoObject



53
54
55
# File 'lib/color_cat/process.rb', line 53

def color_info
  color_details.compact
end

#dominant_categoryObject



69
70
71
# File 'lib/color_cat/process.rb', line 69

def dominant_category
  color_info.first[:color_cat]
end

#dominant_nameObject



73
74
75
# File 'lib/color_cat/process.rb', line 73

def dominant_name
  color_info.first[:color_name]
end

#ignore_white_bg?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/color_cat/process.rb', line 49

def ignore_white_bg?
  opts[:ignore_white_bg]
end

#intensityObject



65
66
67
# File 'lib/color_cat/process.rb', line 65

def intensity
  color_info.first[:intensity]
end

#to_hexObject



57
58
59
# File 'lib/color_cat/process.rb', line 57

def to_hex
  color_info.first[:hex]
end

#to_rgbObject



61
62
63
# File 'lib/color_cat/process.rb', line 61

def to_rgb
  color_info.first[:rgb]
end