#!/usr/bin/env python

# (c)2005 Carol Spears
#
# 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.

import os
from gimpfu import *
def delete_gradients(gradients, segments):
    for gradient_name in gradients:
        pdb.gimp_gradient_delete(gradient_name)
        

def get_fontbox(text, size):
    
    box_width, box_height, ascent, descent = pdb.gimp_text_get_extents_fontname(text, size, PIXELS, 'sans')
    return box_width, box_height


def make_gradient(name, color_first, color_last, segments):
    gradient_name = pdb.gimp_gradient_new(name)

    pdb.gimp_gradient_segment_set_left_color(gradient_name, 0, color_first, 100)
    pdb.gimp_gradient_segment_set_right_color(gradient_name, 0, color_last, 100)
    
    pdb.gimp_gradient_segment_range_split_uniform(gradient_name, 0, 1, segments)

    colors = []
    for n in range(0, segments):
        color, opacity = pdb.gimp_gradient_segment_get_left_color(gradient_name, n)
        pdb.gimp_gradient_segment_set_right_color(gradient_name, n, color, 100)
	colors.append(color)

    return gradient_name, colors

def write_testchart_a4(name, color_x0, color_y0, color_xn, color_yn, segments):
    
    column_width = int(2180/(segments + 2))
    layer_height = column_width
    final_width = 2480
    final_height = 3580
    final_border = 300
    final_image = (final_width, final_height, final_border)

    write_testchart(name, color_x0, color_y0, color_xn, color_yn, segments, column_width, layer_height, final_image)

def write_testchart_photograph(name, color_x0, color_y0, color_xn, color_yn, segments):
    
    column_width = int(767/(segments + 2))
    layer_height = column_width
    final_width = 1067
    final_height = 1600
    final_border = 300
    final_image = (final_width, final_height, final_border)

    write_testchart(name, color_x0, color_y0, color_xn, color_yn, segments, column_width, layer_height, final_image)

def write_testchart_screen(name, color_x0, color_y0, color_xn, color_yn, segments):
    xres, yres = pdb.gimp_get_monitor_resolution()
    column_width = int(xres)
    layer_height = int(yres)
    final_width = column_width*segments + column_width*2
    final_height = layer_height*segments + layer_height
    final_border = 0
    final_image = (final_width, final_height, final_border)

    write_testchart(name, color_x0, color_y0, color_xn, color_yn, segments, column_width, layer_height, final_image)

def write_testchart(name, color_x0, color_y0, color_xn, color_yn, segments, column_width, layer_height, final_image):

    old_fg = gimp.get_foreground()
    old_bg = gimp.get_background()
    old_bg = gimp.get_background()
    gimp.set_foreground(0,0,0)    
    gimp.set_background(255,255,255)

    layer_width = segments*column_width
    image_width = int(segments*column_width + column_width*2)
    image_height = int(segments*layer_height)
    
    image = gimp.Image(image_width, image_height, RGB)
    pdb.gimp_image_set_filename(image, name)
    image.disable_undo()

    old_gradient = pdb.gimp_context_get_gradient()
    grad_name, start_colors = make_gradient('start_colors', color_x0, color_xn, segments)
    pdb.gimp_gradient_delete(grad_name)

    grad_name, end_colors = make_gradient('end_colors', color_y0, color_yn, segments)
    pdb.gimp_gradient_delete(grad_name)
    
    old_palette = pdb.gimp_context_get_palette()
    palette_name = pdb.gimp_palette_new(name)
    pdb.gimp_palette_set_num_columns(palette_name, int(segments))


    x1 = 0
    y1 = layer_height/2
    x2 = layer_width
    y2 = y1
    gradients = []


    for layer_number in range(0, segments):
        color_first = start_colors[layer_number]
        color_last = end_colors[layer_number]
        
        grad_name, colors = make_gradient(name, color_first, color_last, segments)
        gradients.append(grad_name)
        
        for entry in range(0, segments):
            palette_color = colors[entry]
            palette_entry_num = (entry + segments*layer_number)
            pdb.gimp_palette_add_entry(palette_name, grad_name, palette_color)

        drawable = gimp.Layer(image, name, layer_width, layer_height,
                              RGB_IMAGE, 100, NORMAL_MODE)
        offx = 0
        offy = layer_number*layer_height
        pdb.gimp_layer_set_offsets(drawable, offx, offy)
        
        image.add_layer(drawable, layer_number)
        
        pdb.gimp_context_set_gradient(grad_name)
        pdb.gimp_edit_blend(drawable, CUSTOM_MODE, NORMAL_MODE, GRADIENT_LINEAR, 100, 0, REPEAT_NONE, FALSE, FALSE, 0, 0, TRUE, x1, y1, x2, y2)

        pdb.gimp_layer_resize(drawable, image_width, layer_height, 0, 0)

        font_size = int(layer_height/4)
        x,y = get_fontbox(grad_name, font_size)
        font_x = layer_width + int((column_width*2 - x)/2)
        font_y = y + layer_height*layer_number
        border = 0 
        text_layer = pdb.gimp_text_fontname(image, drawable, font_x, font_y, grad_name, border, TRUE, font_size, 
                                            PIXELS, 'Sans')       
        pdb.gimp_floating_sel_anchor(text_layer)   
    
    
# just make chart title
    
    new_height = image_height + layer_height
    offx_title = 0
    offy_title = layer_height
    pdb.gimp_image_resize(image, image_width, new_height, offx_title, offy_title)
    drawable = gimp.Layer(image, 'palette title', image_width, layer_height,
                          RGB_IMAGE, 100, NORMAL_MODE)
    image.add_layer(drawable, 0)
    pdb.gimp_drawable_fill(drawable, BACKGROUND_FILL)
    font_size = int(layer_height/2)
    text_layer = pdb.gimp_text_fontname(image, drawable, 0, 0, palette_name, border, TRUE, font_size, 
                                            PIXELS, 'Sans')       
    pdb.gimp_floating_sel_anchor(text_layer)   

# final resize
    final_width, final_height, final_border = final_image
    border_offset = int(final_border/2)
    pdb.gimp_image_resize(image, final_width, final_height, border_offset, border_offset)
    drawable = gimp.Layer(image, 'Background', final_width, final_height,
                          RGB_IMAGE, 100, NORMAL_MODE)
    layer_position = layer_number + 2
    image.add_layer(drawable, layer_position)
    pdb.gimp_drawable_fill(drawable, BACKGROUND_FILL)
    
    image.enable_undo()
    gimp.set_foreground(old_fg)    
    gimp.set_background(old_bg)
    display = gimp.Display(image)


# clean up
    delete_gradients(gradients, segments)
    pdb.gimp_context_set_gradient(old_gradient)
    pdb.gimp_context_set_palette(palette_name)
    
    

register(
    "python_fu_write_testchart_a4",
    "Draw a large testchart",
    "Draw a large testchart",
    "Carol Spears",
    "Carol Spears",
    "2005",
    "<Toolbox>/Xtns/TestChart/A4Sized",
    "",
    [
        (PF_STRING, "name", "The new gradient name", "pygrad"),
        (PF_COLOUR, "colour_x0", "First colour, first row", (000,000,000)),
        (PF_COLOUR, "colour_y0", "Last colour, first row", (85,85,85)),
        (PF_COLOUR, "colour_xn", "First colour, last row", (170,170,170)),
        (PF_COLOUR, "colour_yn", "Last colour, first row", (255,255,255)),
        (PF_SPINNER, "segments", "Number of colors drawn", 4, (2,10,1)),
    ],
    [],
    write_testchart_a4)

register(
    "python_fu_write_testchart_photograph",
    "Draw a printable photograph testchart",
    "Draw a printable photograph testchart",
    "Carol Spears",
    "Carol Spears",
    "2005",
    "<Toolbox>/Xtns/TestChart/PhotoSized",
    "",
    [
        (PF_STRING, "name", "The new gradient name", "pygrad"),
        (PF_COLOUR, "colour_x0", "First colour, first row", (000,000,000)),
        (PF_COLOUR, "colour_y0", "Last colour, first row", (85,85,85)),
        (PF_COLOUR, "colour_xn", "First colour, last row", (170,170,170)),
        (PF_COLOUR, "colour_yn", "Last colour, first row", (255,255,255)),
        (PF_SPINNER, "segments", "Number of colors drawn", 4, (2,10,1)),
    ],
    [],
    write_testchart_photograph)

register(
    "python_fu_write_testchart_screen",
    "Draw a display testchart",
    "Draw a display testchart",
    "Carol Spears",
    "Carol Spears",
    "2005",
    "<Toolbox>/Xtns/TestChart/ScreenSized",
    "",
    [
        (PF_STRING, "name", "The new gradient name", "pygrad"),
        (PF_COLOUR, "colour_x0", "First colour, first row", (000,000,000)),
        (PF_COLOUR, "colour_y0", "Last colour, first row", (85,85,85)),
        (PF_COLOUR, "colour_xn", "First colour, last row", (170,170,170)),
        (PF_COLOUR, "colour_yn", "Last colour, first row", (255,255,255)),
        (PF_SPINNER, "segments", "Number of colors drawn", 4, (2,10,1)),
    ],
    [],
    write_testchart_screen)

main()
