Class: Cheers::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/cheers/component.rb

Direct Known Subclasses

Background, ImageComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas, randomizer) ⇒ Component



6
7
8
9
# File 'lib/cheers/component.rb', line 6

def initialize(canvas, randomizer)
  @canvas     = canvas
  @randomizer = randomizer
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



4
5
6
# File 'lib/cheers/component.rb', line 4

def canvas
  @canvas
end

#randomizerObject (readonly)

Returns the value of attribute randomizer.



4
5
6
# File 'lib/cheers/component.rb', line 4

def randomizer
  @randomizer
end

Instance Method Details

#applyObject



45
46
47
48
# File 'lib/cheers/component.rb', line 45

def apply
  color = self.color
  composite_with_mask(canvas, colored_image(color), image)
end

#base_imageObject



15
16
17
# File 'lib/cheers/component.rb', line 15

def base_image
  MiniMagick::Image.open(image_path('blank.png'))
end

#colored_image(color) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/cheers/component.rb', line 19

def colored_image(color)
  image =  MiniMagick::Image.open(image_path('blank.png'))

  image.combine_options do |c|
    c.resize '512x512'
    c.fuzz "100%"
    c.fill color
    c.floodfill "+0+0", 'white'
  end
end

#composite_with_mask(base, second, mask_image) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/cheers/component.rb', line 30

def composite_with_mask(base, second, mask_image)
  mask = MiniMagick::Image.open(image_path(mask_image))
  mask.combine_options do |m|
    m.alpha 'copy'
  end

  base.composite(second, 'png', mask)
end

#extract_background_color(image) ⇒ Object

assumes top left pixel is background since none of the parts overlap there



40
41
42
43
# File 'lib/cheers/component.rb', line 40

def extract_background_color(image)
  rgb = image.get_pixels[0][0]
  Color.rgb_to_hex(rgb)
end

#image_path(component) ⇒ Object



11
12
13
# File 'lib/cheers/component.rb', line 11

def image_path(component)
  [Cheers.root, 'components', component].join '/'
end