~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to plug-ins/pygimp/plug-ins/sphere.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
#   Gimp-Python - allows the writing of Gimp plugins in Python.
 
4
#   Copyright (C) 1997  James Henstridge <james@daa.com.au>
 
5
#
 
6
#    This program is free software; you can redistribute it and/or modify
 
7
#   it under the terms of the GNU General Public License as published by
 
8
#   the Free Software Foundation; either version 2 of the License, or
 
9
#   (at your option) any later version.
 
10
#
 
11
#   This program is distributed in the hope that it will be useful,
 
12
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#   GNU General Public License for more details.
 
15
#
 
16
#   You should have received a copy of the GNU General Public License
 
17
#   along with this program; if not, write to the Free Software
 
18
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 
 
20
import math
 
21
from gimpfu import *
 
22
 
 
23
def python_sphere(radius, light, shadow, bg_colour, sphere_colour):
 
24
    if radius < 1:
 
25
        radius = 1
 
26
    width = int(radius * 3.75)
 
27
    height = int(radius * 2.5)
 
28
    img = gimp.Image(width, height, RGB)
 
29
    drawable = gimp.Layer(img, "Sphere Layer", width, height,
 
30
                          RGB_IMAGE, 100, NORMAL_MODE)
 
31
    radians = light * math.pi / 180
 
32
    cx = width / 2
 
33
    cy = height / 2
 
34
    light_x = cx + radius * 0.6 * math.cos(radians)
 
35
    light_y = cy - radius * 0.6 * math.sin(radians)
 
36
    light_end_x = cx + radius * math.cos(math.pi + radians)
 
37
    light_end_y = cy - radius * math.sin(math.pi + radians)
 
38
    offset = radius * 0.1
 
39
    old_fg = gimp.get_foreground()
 
40
    old_bg = gimp.get_background()
 
41
    img.disable_undo()
 
42
    img.add_layer(drawable, 0)
 
43
    gimp.set_foreground(sphere_colour)
 
44
    gimp.set_background(bg_colour)
 
45
    pdb.gimp_edit_fill(drawable, BACKGROUND_FILL)
 
46
    gimp.set_background(20, 20, 20)
 
47
    if (light >= 45 and light <= 75 or light <= 135 and
 
48
        light >= 105) and shadow:
 
49
        shadow_w = radius * 2.5 * math.cos(math.pi + radians)
 
50
        shadow_h = radius * 0.5
 
51
        shadow_x = cx
 
52
        shadow_y = cy + radius * 0.65
 
53
        if shadow_w < 0:
 
54
            shadow_x = cx + shadow_w
 
55
            shadow_w = -shadow_w
 
56
        pdb.gimp_ellipse_select(img, shadow_x, shadow_y, shadow_w, shadow_h,
 
57
                                CHANNEL_OP_REPLACE, TRUE, TRUE, 7.5)
 
58
        pdb.gimp_edit_bucket_fill(drawable, BG_BUCKET_FILL,
 
59
                                  MULTIPLY_MODE, 100, 0, FALSE, 0, 0)
 
60
    pdb.gimp_ellipse_select(img, cx - radius, cy - radius, 2 * radius,
 
61
                            2 * radius, CHANNEL_OP_REPLACE, TRUE, FALSE, 0)
 
62
    pdb.gimp_edit_blend(drawable, FG_BG_RGB_MODE, NORMAL_MODE, GRADIENT_RADIAL,
 
63
                        100, offset, REPEAT_NONE, FALSE, FALSE, 0, 0, TRUE,
 
64
                        light_x, light_y, light_end_x, light_end_y)
 
65
    pdb.gimp_selection_none(img)
 
66
    gimp.set_background(old_bg)
 
67
    gimp.set_foreground(old_fg)
 
68
    img.enable_undo()
 
69
    disp = gimp.Display(img)
 
70
 
 
71
register(
 
72
    "python_fu_sphere",
 
73
    "Simple spheres with drop shadows",
 
74
    "Simple spheres with drop shadows (based on script-fu version)",
 
75
    "James Henstridge",
 
76
    "James Henstridge",
 
77
    "1997-1999",
 
78
    "<Toolbox>/Xtns/Python-Fu/Test/_Sphere",
 
79
    "RGB*, GRAY*, INDEXED*",
 
80
    [
 
81
        (PF_INT, "radius", "Radius for sphere", 100),
 
82
        (PF_SLIDER, "light", "light angle", 45, (0,360,1)),
 
83
        (PF_TOGGLE, "shadow", "shadow?", 1),
 
84
        (PF_COLOR, "bg_colour", "background", (255,255,255)),
 
85
        (PF_COLOR, "sphere_colour", "sphere", (255,0,0))
 
86
    ],
 
87
    [],
 
88
    python_sphere)
 
89
 
 
90
main()