Class: GeoPattern::PatternGenerator
- Inherits:
-
Object
- Object
- GeoPattern::PatternGenerator
- Defined in:
- lib/geo_pattern/pattern_generator.rb
Constant Summary collapse
- DEFAULTS =
{ :base_color => '#933c3c' }
- PATTERNS =
{ 'chevrons' => ChevronPattern, 'concentric_circles' => ConcentricCirclesPattern, 'diamonds' => DiamondPattern, 'hexagons' => HexagonPattern, 'mosaic_squares' => MosaicSquaresPattern, 'nested_squares' => NestedSquaresPattern, 'octagons' => OctagonPattern, 'overlapping_circles' => OverlappingCirclesPattern, 'overlapping_rings' => OverlappingRingsPattern, 'plaid' => PlaidPattern, 'plus_signs' => PlusSignPattern, 'sine_waves' => SineWavePattern, 'squares' => SquarePattern, 'tessellation' => TessellationPattern, 'triangles' => TrianglePattern, 'xes' => XesPattern, }.freeze
- FILL_COLOR_DARK =
"#222"
- FILL_COLOR_LIGHT =
"#ddd"
- STROKE_COLOR =
"#000"
- STROKE_OPACITY =
0.02
- OPACITY_MIN =
0.02
- OPACITY_MAX =
0.15
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#svg ⇒ Object
readonly
Returns the value of attribute svg.
Instance Method Summary collapse
- #base64_string ⇒ Object
- #generate_background ⇒ Object
- #generate_pattern ⇒ Object
-
#initialize(string, opts = {}) ⇒ PatternGenerator
constructor
A new instance of PatternGenerator.
- #svg_string ⇒ Object (also: #to_s)
- #uri_image ⇒ Object
Constructor Details
#initialize(string, opts = {}) ⇒ PatternGenerator
Returns a new instance of PatternGenerator.
39 40 41 42 43 44 45 46 |
# File 'lib/geo_pattern/pattern_generator.rb', line 39 def initialize(string, opts={}) @opts = DEFAULTS.merge(opts) @hash = Digest::SHA1.hexdigest string @svg = SVG.new generate_background generate_pattern end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
37 38 39 |
# File 'lib/geo_pattern/pattern_generator.rb', line 37 def hash @hash end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
37 38 39 |
# File 'lib/geo_pattern/pattern_generator.rb', line 37 def opts @opts end |
#svg ⇒ Object (readonly)
Returns the value of attribute svg.
37 38 39 |
# File 'lib/geo_pattern/pattern_generator.rb', line 37 def svg @svg end |
Instance Method Details
#base64_string ⇒ Object
53 54 55 |
# File 'lib/geo_pattern/pattern_generator.rb', line 53 def base64_string Base64.strict_encode64(svg_string) end |
#generate_background ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/geo_pattern/pattern_generator.rb', line 61 def generate_background if opts[:color] rgb = Color::RGB.from_html(opts[:color]) else hue_offset = PatternHelpers.map(PatternHelpers.hex_val(hash, 14, 3), 0, 4095, 0, 359) sat_offset = PatternHelpers.hex_val(hash, 17, 1) base_color = Color::RGB.from_html(opts[:base_color]).to_hsl base_color.hue = base_color.hue - hue_offset; if (sat_offset % 2 == 0) base_color.saturation = base_color.saturation + sat_offset else base_color.saturation = base_color.saturation - sat_offset end rgb = base_color.to_rgb end r = (rgb.r * 255).round g = (rgb.g * 255).round b = (rgb.b * 255).round svg.rect(0, 0, "100%", "100%", {"fill" => "rgb(#{r}, #{g}, #{b})"}) end |
#generate_pattern ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/geo_pattern/pattern_generator.rb', line 83 def generate_pattern unless opts[:generator].nil? if opts[:generator].is_a? String generator = PATTERNS[opts[:generator]] puts SVG.as_comment("String pattern references are deprecated as of 1.3.0") elsif opts[:generator] < BasePattern if PATTERNS.values.include? opts[:generator] generator = opts[:generator] else abort("Error: the requested generator is invalid") generator = nil end end end generator ||= PATTERNS.values[[PatternHelpers.hex_val(hash, 20, 1), PATTERNS.length - 1].min] # Instantiate the generator with the needed references # and render the pattern to the svg object generator.new(svg, hash).render_to_svg end |
#svg_string ⇒ Object Also known as: to_s
48 49 50 |
# File 'lib/geo_pattern/pattern_generator.rb', line 48 def svg_string svg.to_s end |
#uri_image ⇒ Object
57 58 59 |
# File 'lib/geo_pattern/pattern_generator.rb', line 57 def uri_image "url(data:image/svg+xml;base64,#{base64_string});" end |