Added wrapper for gradients Added context submodule which provides a

2008-06-09  Lars-Peter Clausen <lars@metafoo.de>

	* plug-ins/pygimp/__init.py__: Added wrapper for gradients
	* plug-ins/pygimp/context.py: Added context submodule which provides a
	  wrapper around all gimp_context_[get|set]* functions.

svn path=/branches/soc-2008-python/; revision=25903
This commit is contained in:
Lars-Peter Clausen 2008-06-09 17:26:55 +00:00 committed by Lars-Peter Clausen
parent a7f80d0313
commit d9d1a1905d
3 changed files with 488 additions and 4 deletions

View File

@ -1,3 +1,9 @@
2008-06-09 Lars-Peter Clausen <lars@metafoo.de>
* plug-ins/pygimp/__init.py__: Added wrapper for gradients
* plug-ins/pygimp/context.py: Added context submodule which provides a
wrapper around all gimp_context_[get|set]* functions.
2008-06-03 Lars-Peter Clausen <lars@metafoo.de>
Added wrapper for GimpNumberPairEntry
@ -10,13 +16,14 @@
2008-05-29 Lars-Peter Clausen <lars@metafoo.de>
* plug-ins/pygimp/__init__.py: Added wrapper objects for Brushes and Patterns
* plug-ins/pygimp/__init__.py: Added wrapper objects for Brushes and
Patterns
* plug-ins/pygimp/gimpshelf.py: Forgot that one in last commit
2008-05-29 Lars-Peter Clausen <lars@metafoo.de>
Move gimpenums, gimpshelf, gimpui, gimpcolor to the gimp package and remove
the leading gimp. So they are now accessed as gimp.enums.
Move gimpenums, gimpshelf, gimpui, gimpcolor to the gimp package and
remove the leading gimp. So they are now accessed as gimp.enums.
The old modules import everything from the new ones.
* plug-ins/pygimp/gimpcolor.py
@ -59,7 +66,8 @@
2008-05-26 Lars-Peter Clausen <lars@metafoo.de>
Changed the 'gimp' module to folder with a __init__.py so that it is
able to contain submodules. Renamed the C based 'gimp' module to '_gimp'.
able to contain submodules. Renamed the C based 'gimp' module to
'_gimp'.
__init__.py imports all symbols from _gimp.
* plug-ins/pygimp/Makefile.am

View File

@ -31,6 +31,11 @@ try:
except:
pass
def _prop(func):
"""Helper function for creating properties"""
return property(doc=func.__doc__, *func())
class GimpNamedObject(object):
"""
GimpNamedObject is a bases class for wrappers around gimp objetcs.
@ -344,3 +349,264 @@ class PaletteEntry(object):
doc="""Name of the entry.""")
color = property(fget=lambda self:self.__color, fset=__set_color,
doc="""Color of the entry.""")
class Gradient(GimpNamedObject):
"""A gimp Gradient object."""
__metaclass__ = GimpNamedObjectMeta("gimp_gradient_rename",
"gimp_gradient_delete",
"gimp_gradient_duplicate")
def __init__(self, name):
"""Creates a new Gradient.
name - The name for the gradient"""
GimpNamedObject.__init__(self, pdb.gimp_gradient_new(name))
def get_custom_samples(self, positions, reverse=False):
"""Samples the gradient in custom positions.
Returns a list of samples as gimp.color.RGB
positions - A list of positions to sample along the gradient.
reverse - Whether to use the reverse gradient or not."""
samples = pdb.gimp_gradient_get_custom_samples(self, len(positions),
positions, reverse)[1]
return map(color.RGB, *(iter(samples),)*4)
def get_uniform_samples(self, num_samples, reverse=False):
"""Samples the specfied uniform parts.
Returns a list of samples as gimp.color.RGB.
num_samples - Number of samples to return. 2 <= num_samples.
reverse - Whether to use the reverse gradient or not."""
samples = pdb.gimp_gradient_get_uniform_samples(self, num_samples,
reverse)[1]
return map(color.RGB,*(iter(samples),)*4)
segments = property(
fget=lambda self: GradientSegmentRange(self, 0,
pdb.gimp_gradient_get_number_of_segments(self)),
fdel=lambda self:pdb.gimp_gradient_segment_range_delete(self, 0, -1),
doc="""A gimp.GradientSegmentRange used to access the segemnts of the
gradient.""")
editable = property(
fget=lambda self: pdb.gimp_gradient_is_editable(self) == 1,
doc="""True if the segment is editable, otherwise False.""")
class GradientSegment(object):
def __init__(self, gradient, index):
self.gradient = gradient
self.index = index
def __repr__(self):
return "<gimp.GradientSegment (%s, %d)>" % (str(self.gradient),
self.index)
@_prop
def blending_function():
"""The segments blending function.
A enum value of gimp.enums.GimpGradientSegmentType."""
def get(self):
return enums.GimpGradientSegmentType(
pdb.gimp_gradient_segment_get_blending_function(
self.gradient, self.index))
def set(self, value):
pdb.gimp_gradient_segment_range_set_blending_function(self.gradient,
self.index, self.index, value)
return (get,set)
@_prop
def coloring_type():
"""The segments coloring type.
A enum value of gimp.enums.GimpGradientSegmentColor"""
def get(self):
return enums.GimpGradientSegmentColor(
pdb.gimp_gradient_segment_get_coloring_type(self.gradient,
self.index))
def set(self, value):
pdb.gimp_gradient_segment_range_set_coloring_type(self.gradient,
self.index, self.index, value)
return (get,set)
def get_left_color(self):
return pdb.gimp_gradient_segment_get_left_color(self.gradient,
self.index)[0]
def set_left_color(self, color):
# Get opacity from color
if hasattr(color, "a"):
opacity = color.a
elif len(color) < 4:
opacity = 100
elif isinstance(color[3], float):
opacity = int(color[3] * 100)
else:
opacity = int(int(color[3]) / 2.55)
opacity = min(max(opacity, 0), 100)
pdb.gimp_gradient_segment_set_left_color(self.gradient, self.index,
color, opacity)
def get_right_color(self):
return pdb.gimp_gradient_segment_get_right_color(self.gradient,
self.index)[0]
def set_right_color(self, color):
# Get opacity from color
if hasattr(color, "a"):
opacity = color.a
elif len(color) < 4:
opacity = 100
elif isinstance(color[3], float):
opacity = int(color[3] * 100)
else:
opacity = int(int(color[3]) / 2.55)
opacity = min(max(opacity, 0), 100)
pdb.gimp_gradient_segment_set_right_color(self.gradient, self.index,
color, opacity)
@_prop
def left_pos():
"""The left endpoint position of the segment."""
def get(self):
return pdb.gimp_gradient_segment_get_left_pos(self.gradient,
self.index)
def set(self, value):
pdb.gimp_gradient_segment_set_left_pos(self.gradient, self.index,
min(max(value), 0.0, 1.0))
return (get, set)
@_prop
def middle_pos():
"""The middle point position of the segment."""
def get(self):
return pdb.gimp_gradient_segment_get_middle_pos(self.gradient,
self.index)
def set(self, value):
pdb.gimp_gradient_segment_set_middle_pos(self.gradient, self.index,
min(max(value, 0.0), 1.0))
return (get, set)
@_prop
def right_pos():
"""The right endpoint position of the segment."""
def get(self):
return pdb.gimp_gradient_segment_get_right_pos(self.gradient,
self.index)
def set(self, value):
pdb.gimp_gradient_segment_set_right_pos(self.gradient, self.index,
min(max(value, 0.0), 1.0))
return (get, set)
class GradientSegmentRange(object):
def __init__(self, gradient, start, end):
self.gradient = gradient
self.start = start
self.end = end
def __repr__(self):
return "<gimp.GradientSegmentRange (%s, %d, %d)>"% (str(self.gradient),
self.start, self.end)
def __len__(self):
return self.end - self.start
def __getitem__(self, key):
if isinstance(key, slice):
start, end, step = key.indices(len(self))
if step != 1:
raise IndexError
if start > end:
start = end
return GradientSegmentRange(self.gradient, start+self.start,
end+self.start)
elif isinstance(key, int):
if key < 0:
key += len(self)
if key < 0 or key >= len(self):
raise IndexError("index out of range")
return GradientSegment(self.gradient, self.start + key)
else:
raise TypeError
def __delitem__(self, key):
if isinstance(key, slice):
start, end, step = key.indices(len(self))
if step != 1:
raise IndexError
if start > end:
start = end
elif isinstance(key, int):
if key < 0:
key += len(self)
if key < 0 or key >= len(self):
raise IndexError("index out of range")
start = key
end = start + 1
else:
raise TypeError
pdb.gimp_gradient_segment_range_delete(self.gradient,
self.start + start, self.start + end - 1)
def blend_colors(self):
"""Blends the colors (but not the opacity) of the segment range. Using
it, the colors' transition will be uniform across the range."""
return pdb.gimp_gradient_segment_range_blend_colors(self.gradient,
self.start, self.end)
def blend_opacity(self):
"""Blends the opacity (but not the colors) of the segment range. Using
it, the opacity's transition will be uniform across the range."""
return pdb.gimp_gradient_segment_range_blend_opacity(self.gradient,
self.start, self.end)
def flip(self):
"""Flips the segment range."""
return pdb.gimp_gradient_segment_range_flip(self.gradient, self.start,
self.end)
def move(self, offset, compress_neighbors = True):
"""Move the position of an entire segment range.
offset - The offset to move the segment range (-1 <= delta <= 1)
compress_neighbores - Whether or not to compress the neighboring
segments."""
return pdb.gimp_gradient_segment_range_move(self.gradient, self.start,
self.end, offset, compress_neighbors)
def redistribute_handles(self):
"""Uniformly redistribute the segment range's handles."""
return pdb.gimp_gradient_segment_range_redistribute_handles(
self.gradient, self.start, self.end)
def replicate(self, num_replications):
"""Replicates the segment range.
num_replications - The number of replications (2 <= num_replications
<= 20)"""
return pdb.gimp_gradient_segment_range_replicate(self.gradient,
self.start, self.end, num_replications)
def set_blending_function(self, blending_function):
"""Sets the blending function for each segment in the segment range.
blending_function - A enum value of
gimp.enums.GimpGradientSegmentType."""
return pdb.gimp_gradient_segment_range_set_blending_function(
self.gradient, self.start, self.end, blending_function)
def set_coloring_type(self, coloring_type):
"""Sets the coloring type for each segment in the segment range.
coloring_type - A enum value of gimp.enums.GimpGradientSegmentColor.
"""
return pdb.gimp_gradient_segment_range_set_coloring_type(self.gradient,
self.start, self.end, coloring_type)
def split_midpoint(self):
"""Splits each segment in the segment range at the segment midpoint."""
return pdb.gimp_gradient_segment_range_split_midpoint(self.gradient,
self.start, self.end)
def split_uniform(self, num_splits = 1):
"""Splits each segment in the segment range uniformly.
num_splits - Number of splits per segment."""
return pdb.gimp_gradient_segment_range_split_uniform(self.gradient,
self.start, self.end, num_splits)

210
plug-ins/pygimp/context.py Normal file
View File

@ -0,0 +1,210 @@
# *- Mode: Python; py-indent-offset: 4 -*-
# Gimp-Python - allows the writing of Gimp plugins in Python.
# Copyright (C) 2008 Lars-Peter Clausen <lars@metafoo.de>
#
# 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.
"""
This module provides functions for retriving and manipulating the status of the
current GIMP context.
"""
from gimp import pdb as _pdb
def get_background():
"""
Returns the current background color. The background color is used in a
variety of tools such as blending, erasing (with non-alpha images), and
image filling.
"""
return _pdb.gimp_context_get_background()
def get_brush():
"""
Returns a gimp.Brush instance for the active brush. All paint operations
and stroke operations use this brush to control the application of paint to
the image.
"""
from gimp import Brush
return Brush._id2obj(_pdb.gimp_context_get_brush())
def get_font():
"""
Returns a sting containing the name of the active font.
"""
return _pdb.gimp_context_get_font()
def get_foreground():
"""
Returns the current foreground color. The foregroung color is used in a
variety of tools such as paint tools, blending, and bucket fill.
"""
return _pdb.gimp_context_get_foreground()
def get_gradient():
"""
Returns a gimp.Gradient instance for the active gradient.
"""
from gimp import Gradient
return Gradient._id2obj(_pdb.gimp_context_get_gradient())
def get_opacity():
"""
Returns the opacity setting. The return value is a floating
point number between 0 and 100.
"""
return _pdb.gimp_context_get_opacity()
def get_paint_method():
"""
Returns the name of the active paint method.
"""
return _pdb.gimp_context_get_paint_method()
def get_paint_mode():
"""
Returns a enum value of gimp.enums.GimpLayerModeEffects for the active paint
mode.
"""
from gimp.enums import GimpLayerModeEffects
return GimpLayerModeEffects(_pdb.gimp_context_get_paint_mode())
def get_palette():
"""
Returns a gimp.Palette instance for the active Palette.
"""
from gimp import Palette
return Palette._id2obj(_pdb.gimp_context_get_palette())
def get_pattern():
"""
Returns a gimp.Pattern instance for the active Pattern.
"""
from gimp import Pattern
return Pattern._id2obj(_pdb.gimp_context_get_pattern())
def list_paint_methods():
"""
Returns a tuple of available paint methods. Any of those can be used for
'set_paint_method'.
"""
return _pdb.gimp_context_list_paint_methods()[1]
def pop():
"""
Removes the topmost context from the plug-in's context stack. The context
that was active before the corresponding call to 'push' becomes the new
current context of the plug-in.
"""
return _pdb.gimp_context_pop()
def push():
"""
Creates a new context by copying the current context. This copy becomes the
new context for the calling plug-in until it is popped again using 'pop'.
"""
return _pdb.gimp_context_push()
def set_background(color):
"""
Sets the current background color. The background color is used in a
variety of tools suchs as blending, erasing (with non-alpha images), and
image filling.
color - A valid color instance ot a 3-tuple containing RGB values.
"""
_pdb.gimp_context_set_background(color)
def set_brush(brush):
"""
Sets the active brush.
brush - Either a instance of gimp.Brush or a string containing the name of
a valid brush.
"""
_pdb.gimp_context_set_brush(brush)
def set_default_colors():
"""
Sets the foreground and background colors to black and white.
"""
_pdb.gimp_context_set_default_colors()
def set_font(font):
"""
Sets the active font.
font - A String containing the name of a valid font.
"""
_pdb.gimp_context_set_font(font)
def set_foreground(color):
"""
Sets the active foreground color.
color - A valid color instance or a 3-tuple containg RGB values.
"""
_pdb.gimp_context_set_foreground(color)
def set_gradient(gradient):
"""
Sets the active gradient.
gradient - Either a instance of gimp.Gradient or a string containing the name
of a valid gradient.
"""
_pdb.gimp_context_set_gradient(gradient)
def set_opacity(opacity):
"""
Changes the opacity setting.
opacity - A floating point value between 0.0 and 100.0.
"""
_pdb.gimp_context_set_opacity(opacity)
def set_paint_method(paint_method):
"""
Sets active paint method.
paint_method - The name of a valid painnt method. For valid values see
'list_paint_methods'.
"""
_pdb.gimp_context_set_paint_method(paint_method)
def set_paint_mode(paint_mode):
"""
Sets active paint mode.
paint_mode - A enum value of gimp.enums.GimpLayerModeEffects.
"""
_pdb.gimp_context_set_paint_mode(paint_mode)
def set_palette(palette):
"""
Sets the active palette.
palette - Either a instance of gimp.Palette or a string containing the name
of a valid gradient.
"""
_pdb.gimp_context_set_palette(palette)
def set_pattern(pattern):
"""
Sets the active pattern.
pattern - Either a instance of gimp.Pattern or a string containing the name
of a valid pattern.
"""
_pdb.gimp_context_set_pattern(pattern)
def swap_colors():
"""
Swaps the current foreground and background colors, so that the new
foreground color becomes the old background color and vice versa.
"""
_pdb.gimp_context_swap_colors()