Giter Site home page Giter Site logo

rubyart's Introduction

rubyart

Generate interesting PNG and animated GIFs using Ruby.

See http://www.rubydoc.info/gems/png/1.1.0/PNG/Canvas for information on PNG.

You will need to install the following dependencies. On Ubuntu you can install imagemagick and libmagickwand-dev in order to install rmagick

gem install png
gem install RubyInline
gem install rmagick

render.rb is a sample script that demonstrates usaged of rubyart.rb.

Example usage and image produced

To render an image it is important to understand how the engine works. We loop over the coordinates of an image. The x and y points, and for each point we generate an rgb color. A color for red, green, and blue. First we generate r. To do this we have some numbers available. The x and y coordinates, and a gif_factor which is some number we choose. Mostly used when generating gifs so we can ignore it here, except for the fact that it is the first parameter of render_img.

The colors are generated in the following order: r, g, b. First r is generated and we only can create a mathematical expression with x, y, and gif_factor. The second color is g. We have the same variables to work with plus the additional r value. The third and last color is b. Again we have all of the previous variables to work with, plus the additional g value.

At each stage here are the values we have available. r: x, y, gif_factor, g: x, y, r, gif_factor, b: x, y, r, g, gif_factor.

First we need to require the rubyart library.

require_relative 'rubyart'

Next create a new RubyArt object.

art = RubyArt.new

Now we set three lambda expressions on the art object. r_fn sets the red color, g_fn sets the green color, and b_fn sets the blue color. Above we mentioned the available values so we can return some value based on some mathematical operations.

art.r_fn = lambda { |x, y, gif_factor| return x + y + gif_factor }
art.g_fn = lambda { |x, y, r, gif_factor| return r + x + y + gif_factor }
art.b_fn = lambda { |x, y, r, g, gif_factor| return g + r + x + y + gif_factor }

Finally render the image.

art.render_img(0, 'image.png')

And here is the output.

sample rendered image

Example GIFs

To generate an animated GIF we start out as described in the image section above, but this time we call art.render_gif('example.gif') instead of render_img.

art.render_gif('example.gif')

This is where the gif_factor comes in to play. Currently it is the GIF frame number. The current number of frames is 256 and cannot currently be changed. e.g. 0 <= gif_factor <= 255

Due to the size of each GIF (~4-6mb) I created a gallery on imgur.

http://imgur.com/a/h8Yot

rubyart's People

Contributors

wsams avatar

Watchers

 avatar James Cloos avatar

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.