module ImageParadise::Svg::Rectangle

Constants

DEFAULT_FILL_COLOUR
#

DEFAULT_FILL_COLOUR

#
DEFAULT_HEIGHT
#

DEFAULT_HEIGHT

#
DEFAULT_WIDTH
#

DEFAULT_WIDTH

#

Public Class Methods

create( width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT, fill_colour = DEFAULT_FILL_COLOUR, stroke_width = '3' ) { || ... } click to toggle source
#

Svg::Rectangle.create

This will generate a SVG rectangle.

A proper rectangle has a width and a height setting.

You can optionally handle opacity if you pass in a block.

#
# File lib/image_paradise/svg/rectangle.rb, line 37
def self.create(
    width        = DEFAULT_WIDTH,
    height       = DEFAULT_HEIGHT,
    fill_colour  = DEFAULT_FILL_COLOUR,
    stroke_width = '3'
  )

  extra_elements = '' # Extra values for the SVG attribute.
  opacity = ''.dup # This variable keeps track over whether we require opacity.

  # ======================================================================= #
  # === Handle block arguments next
  # ======================================================================= #
  if block_given?
    yielded = yield
    # ===================================================================== #
    # Next convert a Proc to its "real" value.
    # ===================================================================== #
    if yielded.is_a? Proc
      yielded = yielded.call
    end
    if yielded.is_a?(String)
      if yielded.include? 'opacity'
        # ================================================================= #
        # === Handle opacity
        #
        # Popular options are:
        #
        #   'fill-opacity:0.9;stroke-opacity:0.9'
        #
        # ================================================================= #
        opacity << yielded
        opacity << ';' unless opacity.end_with? ';'
      end
      if yielded.include? 'rounded'
        # ================================================================= #
        # === Handle rounded edges for a rectangle
        #
        # The rx and the ry attributes rounds the corners of the rectangle.
        # ================================================================= #
        extra_elements = ' x="50" y="20" rx="20" ry="20" '
      end
    end
  end

  # ======================================================================= #
  # === Handle Hash given as input to us next
  # ======================================================================= #
  if width.is_a? Hash
    if width.has_key? :fill_colour
      fill_colour = width.delete(:fill_colour)
    end
    if width.has_key? :width
      width = width.delete(:width)
    end if width.is_a? Hash
    if width.has_key? :height
      height = width.delete(:height) # This is ok because width and height are the same.
    end if width.is_a? Hash
    if width.has_key? :stroke_width
      stroke_width = width.delete(:stroke_width)
    end if width.is_a? Hash
    if width.has_key? :colour
      fill_colour = width.delete(:colour)
    end if width.is_a? Hash
    unless width.is_a? Numeric
      width = DEFAULT_WIDTH if width.empty?
    end
  end
  # ======================================================================= #
  # Convert the various arguments passed to this method into a String.
  # ======================================================================= #
  width         = width.to_s.dup unless width.is_a? Hash
  height        = height.to_s
  fill_colour   = fill_colour.to_s
  stroke_width  = stroke_width.to_s
  # ======================================================================= #
  # === Handle random fill colour
  # ======================================================================= #
  if fill_colour == 'random'
    fill_colour = Colours::HtmlColours.random
  end
  # ======================================================================= #
  # We will put the inner-rectangle at 80%.
  # ======================================================================= #
  _ = Svg.strip(
  '<svg width="'+width+'" height="'+height+'">

      <rect '+extra_elements+'width="'+(width.to_f * 80 / 100).to_i.to_s+'" height="'+(height.to_f * 80 / 100).to_i.to_s+'" style="'+opacity+'fill:'+fill_colour+';stroke-width:'+stroke_width+';stroke:rgb(0,0,0)">
      '+Svg.close_svg
  )+N
  _ = _.to_s.dup
  Svg.add(_)
  return _
end