Code Monkey home page Code Monkey logo

triangle's Introduction

Ruby Gem Version

TrianglePattern Logo

With this gem you can generate SVG background-images for your project. It uses the Delaunay Triangulation to generate a mesh of triangles. The triangles are also colored in a gradient given any set of colors.

screen shot 2017-08-07 at 12 18 15 pm screen shot 2017-08-07 at 12 18 15 pm
screen shot 2017-08-07 at 12 18 15 pm screen shot 2017-08-07 at 12 18 15 pm

Installation

Add this line to your application's Gemfile:

gem 'triangle_pattern'

And then execute:

$ bundle

Or install it yourself as:

$ gem install triangle_pattern

Usage

To create a pattern use:

require 'triangle_pattern'

pattern = TrianglePattern.generate

with specific width and height:

pattern = TrianglePattern.generate(width: 900, height: 500)

with a specific set of colors:

pattern = TrianglePattern.generate(colors: ["#edf8fb", "#b2e2e2", "#66c2a4", "#2ca25f", "#006d2c"])

image

get the pattern in svg:

puts pattern.to_svg
# => <svg xmlns="http://www.w3.org/2000/svg" ...

get the Base64 encoded string:

puts pattern.to_base64
# => PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC...

Options

You can pass several options to TrianglePattern.generate

width

Integer, defaults to 900. Width in pixels of the pattern to generate.

height

Integer, defaults to 500. Height in pixels of the pattern to generate.

cell_size

Integer, defaults to 75. Size of the mesh used to generate triangles. Larger values will result in coarser patterns, smaller values will result in finer patterns.

variance

Decimal value between 0 and 1, defaults to 0.75. Amount of randomness used when generating triangles.

seed

Integer, defaults to 135. Is used to seed the random generator which generates the points for the mesh

colors

Array, defaults to ["#ffffe5","#f7fcb9","#d9f0a3","#addd8e","#78c679","#41ab5d","#238443","#006837","#004529"]. Defines the colors used in the pattern. TrianglePattern interpolates between the given colors to generate a smooth gradient. You can get colors by using the Chroma.js Color Palette Helper for example.

Rake

require 'triangle_pattern/triangle_task'

TrianglePattern::TriangleTask.new(
      name: 'generate',
      description: '', data: {
        'fixtures/BuGn.svg' => { colors: ["#edf8fb", "#b2e2e2", "#66c2a4", "#2ca25f", "#006d2c"], width: 400, height: 400, seed: 80 },
        'fixtures/GnBu.svg' => { colors: ["#edf8fb", "#b3cde3", "#8c96c6", "#8856a7", "#810f7c"], width: 400, height: 400, seed: 90 },
        'fixtures/OrRd.svg' => { colors: ["#fef0d9", "#fdcc8a", "#fc8d59", "#e34a33", "#b30000"], width: 400, height: 400, seed: 100 },
        'fixtures/BrBG.svg' => { colors: ["#a6611a", "#dfc27d", "#f5f5f5", "#80cdc1", "#018571"], width: 400, height: 400, seed: 110 },
      }
    )
  end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hendrixfan/triangle.

They are talking about it

License

The gem is available as open source under the terms of the MIT License.

triangle's People

Contributors

dependabot[bot] avatar fwolfst avatar hendrixfan avatar noraj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

triangle's Issues

Works with ruby 2.6 and not with ruby 2.7

I do not really understand why but it works well with ruby 2.6.0 and crashes with ruby 2.70.

require 'triangle_pattern'

pattern = TrianglePattern.generate(
  colors: ["#edf8fb", "#b2e2e2", "#66c2a4", "#2ca25f", "#006d2c"],
  width: 1920,
  height: 1080,
  cell_size: 75,
  variance: 0.75,
  seed: 135
)
noraj@devarch ~/Pictures/triangle_patterns » rbenv shell 2.6.0
noraj@devarch ~/Pictures/triangle_patterns » ruby generate.rb 
noraj@devarch ~/Pictures/triangle_patterns » rbenv shell 2.7.0                                        
noraj@devarch ~/Pictures/triangle_patterns » gem list | grep triangle      
triangle_pattern (0.2.2)
noraj@devarch ~/Pictures/triangle_patterns » ruby generate.rb              
Traceback (most recent call last):
        2: from generate.rb:1:in `<main>'
        1: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
/home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- triangle_pattern (LoadError)
        12: from generate.rb:1:in `<main>'
        11: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:156:in `require'
        10: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:168:in `rescue in require'
         9: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:168:in `require'
         8: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/triangle_pattern-0.2.2/lib/triangle_pattern.rb:7:in `<top (required)>'
         7: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
         6: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
         5: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/gradient-0.5.1/lib/gradient.rb:8:in `<top (required)>'
         4: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
         3: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
         2: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/gradient-0.5.1/lib/gradient/svg.rb:1:in `<top (required)>'
         1: from /home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
/home/noraj/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- scanf (LoadError)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.