class Algoheader::SvgGenerator

SvgGenerator

Author

Dick Davis

Copyright

Copyright 2021 Dick Davis

License

GNU Public License 3

Generates SVGs by providing randomized input to algorithm.

Constants

LINECAPS
OPACITIES
SYMBOLS
WIDTHS

Attributes

fill_colors[R]
stroke_colors[R]

Public Class Methods

new(fill_colors:, stroke_colors:) click to toggle source

rubocop:disable Lint/MissingSuper

# File lib/algoheader/svg_generator.rb, line 40
def initialize(fill_colors:, stroke_colors:)
  @fill_colors = fill_colors
  @stroke_colors = stroke_colors
end

Public Instance Methods

call() click to toggle source

rubocop:enable Lint/MissingSuper

# File lib/algoheader/svg_generator.rb, line 46
def call
  ascii = AsciiToSvg.example_string(random_symbol_sample, 80)
  AsciiToSvg.from_string(ascii, 16, **options)
rescue ZeroDivisionError
  retry
end

Private Instance Methods

canvas_color() click to toggle source
# File lib/algoheader/svg_generator.rb, line 55
def canvas_color
  @canvas_color ||= random_fill_color
end
options() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/algoheader/svg_generator.rb, line 60
def options
  {
    canvas__size__x: 1500,
    style__canvas__fill__color: canvas_color,
    style__line__stroke__width: random_width,
    style__line__stroke__color: random_stroke_color,
    style__line__stroke__opacity: random_opacity,
    style__line__stroke__linecap: random_linecap,
    style__ellipse__stroke__width: random_width,
    style__ellipse__stroke__color: random_stroke_color,
    style__ellipse__stroke__opacity: random_opacity,
    style__ellipse__stroke__linecap: random_linecap,
    style__ellipse__fill: random_fill_color,
    style__rectangle__fill__color: random_fill_color
  }
end
random_fill_color() click to toggle source
# File lib/algoheader/svg_generator.rb, line 94
def random_fill_color
  return fill_colors.sample if @canvas_color.nil?

  fill_colors.reject { |color| color == canvas_color }.sample
end
random_linecap() click to toggle source
# File lib/algoheader/svg_generator.rb, line 90
def random_linecap
  LINECAPS.sample
end
random_opacity() click to toggle source
# File lib/algoheader/svg_generator.rb, line 86
def random_opacity
  OPACITIES.sample
end
random_stroke_color() click to toggle source
# File lib/algoheader/svg_generator.rb, line 100
def random_stroke_color
  stroke_colors.reject { |color| color == canvas_color }.sample
end
random_symbol_sample() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/algoheader/svg_generator.rb, line 78
def random_symbol_sample
  SYMBOLS.sample(rand(SYMBOLS.length))
end
random_width() click to toggle source
# File lib/algoheader/svg_generator.rb, line 82
def random_width
  WIDTHS.sample
end