Class: JekyllOgImage::Element::Border

Inherits:
Base
  • Object
show all
Defined in:
lib/jekyll_og_image/element/border.rb

Defined Under Namespace

Classes: Part

Instance Method Summary collapse

Constructor Details

#initialize(size, position: :bottom, fill: "#000000") ⇒ Border

Returns a new instance of Border.



7
8
9
10
11
12
13
# File 'lib/jekyll_og_image/element/border.rb', line 7

def initialize(size, position: :bottom, fill: "#000000")
  @size = size
  @position = position
  @fill = fill

  validate_position!
end

Instance Method Details

#apply_to(canvas, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll_og_image/element/border.rb', line 15

def apply_to(canvas, &block)
  border = Vips::Image.black(*dimensions(canvas))

  parts(canvas).each.with_index do |part, index|
    image = Vips::Image.black(part.width, part.height).ifthenelse([ 0, 0, 0 ], part.rgb)

    if vertical?
      border = border.composite(image, :over, x: [ 0 ], y: [ part.offset ]).flatten
    else
      border = border.composite(image, :over, x: [ part.offset ], y: [ 0 ]).flatten
    end
  end

  result = block.call(canvas, border) if block_given?

  x, y = result ? [ result.fetch(:x, 0), result.fetch(:y, 0) ] : [ 0, 0 ]

  if vertical?
    x = @position == :left ? x : canvas.width - @size - x
    canvas.composite(border, :over, x: [ x ], y: [ 0 ]).flatten
  else
    y = @position == :top ? y : canvas.height - @size - y
    canvas.composite(border, :over, x: [ 0 ], y: [ y ]).flatten
  end
end