Blog Jaune

Des cercles pour l'Atelier Cod'Art


Pour l'atelier Cod'Art organisé cet été sur le site du zéro, j'avais réalisé quelques petits scripts sans prétentions, pour faire des jolies images. :-)

Après relecture, le code est lourd, mais bon, ça a été codé pour faire ensuite des choses plus compliquées, mais j'ai arrêté.


Cercles PNG


#!/usr/bin/env ruby
#
# Petit script sans prétentions, pour faire des jolies images.
#
# Copyright (c) 2007 Antoine Pultier (http://www.blogjaune.fr/)
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Comme la commande clear
STDOUT.print "[H[2J"

puts "Lancement du script"

begin
        require 'RMagick'
        puts "Chargement de Rmagick"
rescue LoadError
        puts "Chargement des gems"
        require 'rubygems/package'
        retry
rescue => e
        puts e
end

include Magick

class Points
        attr_accessor :draw, :largeur, :largeur_bord, :couleur, :couleur_bord, :transparence, :transparence_bord
        
        def initialize(draw, largeur=5, largeur_bord=1, couleur='blue', couleur_bord='aqua',transparence=0.25, transparence_bord=0.75)
                @draw = draw
                @largeur = largeur
                @largeur_bord = largeur_bord
                @couleur = couleur
                @couleur_bord = couleur_bord
                @transparence = transparence
                @transparence_bord = transparence_bord
        end
        
        def tracer(x,y)
                gc = @draw
                gc.fill = @couleur
                gc.fill_opacity(@transparence)
                gc.stroke(@couleur_bord)
                gc.stroke_opacity(@transparence_bord)
                # Pour la beauté sur les bords (quand le bord est plus large que le cercle, il y a un bug de pixels)
                if @largeur>=@largeur_bord
                        largeur = @largeur
                        largeur_bord = @largeur_bord
                else
                        largeur = @largeur_bord
                        largeur_bord = @largeur
                end
                gc.stroke_width(largeur_bord)
                op = gc.dup
                op.circle(x,y,x+largeur,y)
                return op
        end
        
        def pt(x,y)
                gc = @draw
                gc.fill = @couleur
                op = gc.dup
                op.point(x,y)
                return op
        end
end

def couleur
        rouge = rand(6)*51
        vert = rand(6)*51
        bleu = rand(6)*51
        return "rgb(#{rouge},#{vert},#{bleu})"
end

puts "Méthodes chargées"

# Création du dessin vectoriel
gc = Draw.new

# Initialisation de la class pour faire des points
p = Points.new(gc)

puts "Création de la zone vectorielle réussie"

# Création du dessin bitmap
puts "Création de l'image png"
largeur = 800
hauteur = 600
image = Magick::Image.new(largeur, hauteur) do
        self.background_color = 'transparent'
end

# Partie important
puts "Création des cercles"

30.times do
        # Largeur        
        p.largeur+=(9-rand(10))
        .largeur_bord=(rand(20)+1)
        # Couleur
        p.couleur = couleur
        p.couleur_bord = couleur
        # Ajout du point
        gg = p.tracer(rand(largeur+1),rand(hauteur+1))
        # Tracage du point dans l'image
        gg.draw(image)
        # Notification
        STDOUT.flush
        STDOUT.print '.'
end

puts "\nCréation des cercles terminée"

# Choix du format de l'image
image.format = "PNG"

puts "Création de l'image png terminée"

puts "Écriture de l'image"
# Écriture de l'image
image.write('image.png')

puts "Script terminé"

Commentaire

Ajouter un commentaire