#!/usr/bin/env python
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#
# Based on squarificate.py by Joao S. O. Bueno Calligaris, and Joao did all
# the real work in this script.  Inspired by Werner Hartnagel for the circular
# selection parts also (from dotimage.py).  Seth Burgess and Manish Singh both helped 
# helped with debugging -- Seth more so.
#


import random
import string
from math import pi
from gimpfu import *


def palette_squarecircleify(image, drawable, style, mean, standard, palette_name):
    pdb.gimp_context_push()
    pdb.gimp_context_set_palette(palette_name)

    num_colors = pdb.gimp_palette_get_info(palette_name)
    resolution = int(image.width/300)
    if (resolution == 0):resolution = 1
    pdb.gimp_image_undo_group_start(image)
    complete = 0.0
    mean_size = int(mean*resolution)
    standard_deviation = int(standard*resolution)

    palette_layer = pdb.gimp_layer_new(image, image.width, image.height, RGBA_IMAGE, 'palette', 100.0, NORMAL_MODE)
    image.add_layer(palette_layer, 0)
    pdb.gimp_drawable_fill(palette_layer, TRANSPARENT_FILL)

    number = int(num_colors)
    for entry_number in range(0,num_colors):
        #tehre are some unknown excetion errors when adding a layer 
        #with certain parameters
        side = random.gauss (mean_size, standard_deviation / 2)
        if (side < 3): side = 3
        width = height = side
        x = random.randrange(int (0 - side), image.width)
        y = random.randrange(int (0 - side), image.height)
        if (x < (0 - mean_size)):x = abs(x)
        if (y < (0 - mean_size)):y = abs(y)

        color = pdb.gimp_palette_entry_get_color(palette_name, entry_number)
        pdb.gimp_context_set_foreground(color)
        if (style == 'CIRCLES'):
            pdb.gimp_ellipse_select(image, x, y, side, side, CHANNEL_OP_REPLACE, True, True, resolution)
        if (style == 'SQUARES'):
            pdb.gimp_rect_select(image, x, y, side, side, CHANNEL_OP_REPLACE, False, 0.0)

        non_empty, check_x1, check_y1, check_x2, check_y2 = pdb.gimp_selection_bounds(image)
        if non_empty:
            pdb.gimp_edit_bucket_fill(palette_layer, FG_BUCKET_FILL, NORMAL_MODE, 100, 255, True, side, side)
            pdb.gimp_selection_none(image)
            complete = 0.9 * (float(entry_number)/number)
        else: entry_number = entry_number - 1
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_context_pop()


register(
	"python_fu_palette_squarecircleify",
	"Use a GIMP palette to generate a beautiful image.",
	"Use a GIMP palette to generate a beautiful image.",
	"Carol Spears and Joao S. O. Bueno Calligaris and Werner Hartnagel",
	"Carol Spears and Joao S. O. Bueno Calligaris and Werner Hartnagel",
	"2005",
	"<Image>/Filters/Render/Randomize Palette...",
	"",
	[
	(PF_RADIO, "style", "Style", "SQUARES", (("circles", "CIRCLES"), ("squares", "SQUARES"))),
        (PF_SPINNER, "mean", "Medium size of the colors", 20, (1,300,1)),
        (PF_SPINNER, "standard", "Variation in the color size", 15, (0,300,1)),
	(PF_PALETTE, "palette_name", "Palette", "Topographic")
	],
	[],
	palette_squarecircleify)




main()
