Advertisement

A new release of the wonderful geo_pattern-gem is available. Examples for those patterns can be found here.

The new release extends the API of previous versions:

  • New API for inspecting a pattern
  • Added a rake-task to generate patterns

And behind the scenes:

  • Extensive refactoring to simplify further changes

Inspecting a generated pattern

Generate pattern

With the new release the user API does not change: You can generate a pattern as before.

require 'geo_pattern'
pattern = GeoPattern.generate('Mastering Markdown')

Background color

By using the pattern.background-method you get access to information about the background of the generated pattern. To get the background color just call pattern.background.color. You can get that color in “HTML” and “SVG” notation.

# The color of the background in html notation
pattern.background.color.to_html
# => "#3f904d"

# The color of the background in svg notation
pattern.background.color.to_svg
# => "rgb(63, 144, 77)"

Input for color generation

If you need access to the colors, which have been used to generate the background-color, you can use pattern.background.preset.*.

# Color
pattern.background.preset.color
# => nil

# Base color
pattern.background.preset.base_color
# => "#933c3c"

Name of the background generator

The background is generated by a background-generator. Using the following code snippet you get access to the generator used.

# The generator-class used to generate the background
pattern.background.generator
# => GeoPattern::BackgroundGenerators::SolidGenerator

Generated structure

Every structure of a pattern has a name. With the new API you get access to the name of the generated structure. Normally you do not tell GeoPattern which pattern generator to use, so this can be quite handy, e.g. in tests.

# The name of the structure
pattern.structure.name
# => :hexagons

Name of the structure generator

The background is generated by a structure-generator. Using the following code snippet you get access to the generator used.

# The generator-class used to generate the structure
pattern.structure.generator
# => GeoPattern::StructureGenerators::HexagonsGenerator

Generating patterns with rake

rake is the make for the ruby-world. With the new release geo_pattern comes with a rake task to generate patterns: All you need to do is to add the following code snippet to your Rakefile.

require 'geo_pattern/geo_pattern_task'

string = 'Mastering markdown'

GeoPattern::GeoPatternTask.new(
  name: 'patterns:generate',
  description: 'Generate patterns to make them available as fixtures',
  data: {
    'fixtures/generated_patterns/diamonds_with_color.svg'      => { input: string, patterns: [:diamonds], color: '#00ff00' },
    'fixtures/generated_patterns/diamonds_with_base_color.svg' => { input: string, patterns: [:diamonds], base_color: '#00ff00' }
  }
)

This creates a task with the given name.

$ rake -T
rake patterns:generate  # Generate patterns to make them available as fixtures

Running this rake-task will create the files.

rake patterns:generate
# I, [2015-03-02T10:09:50.284595 #7563]  INFO -- : Creating pattern at "/home/user/tmp/fixtures/generated_patterns/diamonds_with_color.svg".
# I, [2015-03-02T10:09:50.288890 #7563]  INFO -- : Creating pattern at "/home/user/tmp/fixtures/generated_patterns/diamonds_with_base_color.svg".

Discussion

If you found a mistake in this article or would like to contribute some content to this article, please file an issue in this Git Repository

Disclaimer

The contents of this article are put together to the best of the authors' knowledge, but it cannot be guaranteed that it's always accurate in any environment. It is up to the reader to make sure, that all information found in this article, does not do any damage to the readers's working environment or whereever this information is applied to. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, arising from, out of or in connection with this article. Please also note the information given on the Licenses' page.

Related Articles