Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hendrixfan/triangle

Dynamically generate beautiful triangle background patterns using delaunay triangulation
https://github.com/hendrixfan/triangle

hacktoberfest pattern ruby triangle visualization

Last synced: 7 days ago
JSON representation

Dynamically generate beautiful triangle background patterns using delaunay triangulation

Awesome Lists containing this project

README

        

![Ruby](https://github.com/hendrixfan/triangle/workflows/Ruby/badge.svg)
[![Gem Version](https://badge.fury.io/rb/triangle_pattern.svg)](https://badge.fury.io/rb/triangle_pattern)


TrianglePattern Logo

With this gem you can generate SVG background-images for your project. It uses the [Delaunay Triangulation](https://www.mathworks.com/help/matlab/math/delaunay-triangulation.html) 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 | screen shot 2017-08-07 at 12 18 15 pm |

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'triangle_pattern'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install triangle_pattern

## Usage

To create a pattern use:
```ruby
require 'triangle_pattern'

pattern = TrianglePattern.generate
```

with specific width and height:
```ruby
pattern = TrianglePattern.generate(width: 900, height: 500)
```

with a specific set of colors:
```ruby
pattern = TrianglePattern.generate(colors: ["#edf8fb", "#b2e2e2", "#66c2a4", "#2ca25f", "#006d2c"])
```

![image](https://user-images.githubusercontent.com/19203626/75806052-43558b00-5d83-11ea-8ae0-5f76d59127ed.png)

get the pattern in svg:
```ruby
puts pattern.to_svg
# => 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](https://gka.github.io/palettes/#/9|s|00429d,96ffea,ffffe0|ffffe0,ff005e,93003a|1|1) for example.

## Rake

```ruby
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](https://rubygems.org).

## Contributing

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

## They are talking about it

- [Rawsec - Generate background pattern in Ruby](https://rawsec.ml/en/generate-background-pattern-in-ruby/)

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).