1#!/usr/local/bin/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 3 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, see <https://www.gnu.org/licenses/>.
18
19import math
20from gimpfu import *
21
22def sphere(radius, light, shadow, foo, bg_colour, sphere_colour):
23    if radius < 1:
24        radius = 1
25
26    width = int(radius * 3.75)
27    height = int(radius * 2.5)
28
29    gimp.context_push()
30
31    img = gimp.Image(width, height, RGB)
32
33    drawable = gimp.Layer(img, "Sphere Layer", width, height,
34                          RGB_IMAGE, 100, NORMAL_MODE)
35
36    radians = light * math.pi / 180
37
38    cx = width / 2
39    cy = height / 2
40
41    light_x = cx + radius * 0.6 * math.cos(radians)
42    light_y = cy - radius * 0.6 * math.sin(radians)
43
44    light_end_x = cx + radius * math.cos(math.pi + radians)
45    light_end_y = cy - radius * math.sin(math.pi + radians)
46
47    offset = radius * 0.1
48
49    img.disable_undo()
50    img.insert_layer(drawable)
51
52    gimp.set_foreground(sphere_colour)
53
54    gimp.set_background(bg_colour)
55    pdb.gimp_edit_fill(drawable, BACKGROUND_FILL)
56
57    gimp.set_background(20, 20, 20)
58
59    if (light >= 45 and light <= 75 or light <= 135 and
60        light >= 105) and shadow:
61        shadow_w = radius * 2.5 * math.cos(math.pi + radians)
62        shadow_h = radius * 0.5
63        shadow_x = cx
64        shadow_y = cy + radius * 0.65
65
66        if shadow_w < 0:
67            shadow_x = cx + shadow_w
68            shadow_w = -shadow_w
69
70        pdb.gimp_ellipse_select(img, shadow_x, shadow_y, shadow_w, shadow_h,
71                                CHANNEL_OP_REPLACE, True, True, 7.5)
72        pdb.gimp_edit_bucket_fill(drawable, BG_BUCKET_FILL,
73                                  MULTIPLY_MODE, 100, 0, False, 0, 0)
74
75    pdb.gimp_ellipse_select(img, cx - radius, cy - radius, 2 * radius,
76                            2 * radius, CHANNEL_OP_REPLACE, True, False, 0)
77    pdb.gimp_edit_blend(drawable, FG_BG_RGB_MODE, NORMAL_MODE, GRADIENT_RADIAL,
78                        100, offset, REPEAT_NONE, False, False, 0, 0, True,
79                        light_x, light_y, light_end_x, light_end_y)
80
81    pdb.gimp_selection_none(img)
82
83    img.enable_undo()
84
85    disp = gimp.Display(img)
86
87    gimp.context_pop()
88
89
90register(
91    "python-fu-sphere",
92    "Simple sphere with drop shadow",
93    "Simple sphere with drop shadow",
94    "James Henstridge",
95    "James Henstridge",
96    "1997-1999, 2007",
97    "_Sphere",
98    "",
99    [
100        (PF_INT, "radius", "Radius for sphere", 100),
101        (PF_SLIDER, "light", "Light angle", 45, (0,360,1)),
102        (PF_TOGGLE, "shadow", "Shadow?", 1),
103        (PF_RADIO, "foo", "Test", "foo", (("Foo", "foo"), ("Bar", "bar"))),
104        (PF_COLOR, "bg-color", "Background", (1.0, 1.0, 1.0)),
105        (PF_COLOR, "sphere-color", "Sphere", "orange")
106    ],
107    [],
108    sphere,
109    menu="<Image>/Filters/Languages/Python-Fu/Test")
110
111main()
112