Skip to content

Commit

Permalink
reduce code in color class
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Piotr Przeradowski committed Aug 18, 2013
1 parent 3016bd7 commit 60f5754
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 101 deletions.
94 changes: 22 additions & 72 deletions pystacia/color/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ def from_hsl(h, s, l, factory=None):
return color

from pystacia.common import Resource
from pystacia.compat import formattable


def _make_component(name):
doc = formattable("""Set or get {0} channel information.
The value ought to be a float between 0 and 1.
:rtype: ``float`` or ``int``
""").format(name)

def fget(self):
return getattr(impl, 'get_' + name)(self)

def fset(self, value):
getattr(impl, 'set_' + name)(self, value)

return property(fget, fset, doc=doc)


class Color(Resource):
Expand All @@ -155,90 +173,22 @@ def _free(self):
def _clone(self):
return impl.clone(self)

def __red(): # @NoSelf
doc = ( # NOQA
"""Set or get red channel information.
The value ought to be a float between 0 and 1.
:rtype: ``float`` or ``int``
""")

def fget(self):
return impl.get_red(self)

def fset(self, value):
impl.set_red(self, value)

return property(**locals())

red = __red()
red = _make_component('red')

r = red
"""Convenience synonym for :attr:`red`."""

def __green(): # @NoSelf
doc = ( # NOQA
"""Set or get green channel information.
The value ought to be a float between 0 and 1.
:rtype: ``float`` or ``int``
""")

def fget(self):
return impl.get_green(self)

def fset(self, value):
impl.set_green(self, value)

return property(**locals())

green = __green()
green = _make_component('green')

g = green
"""Convenience synonym for :attr:`green`."""

def __blue(): # @NoSelf
doc = ( # NOQA
"""Set or get blue channel information.
The value ought to be a float between 0 and 1.
:rtype: ``float`` or ``int``
""")

def fget(self):
return impl.get_blue(self)

def fset(self, value):
impl.set_blue(self, value)

return property(**locals())

blue = __blue()
blue = _make_component('blue')

b = blue
"""Convenience synonym for :attr:`blue`."""

def __alpha(): # @NoSelf
doc = ( # NOQA
"""Set or get alpha channel information.
The value ought to be a float between 0 and 1.
:rtype: ``float`` or ``int``
""")

def fget(self):
return impl.get_alpha(self)

def fset(self, value):
impl.set_alpha(self, value)

return property(**locals())

alpha = __alpha()
alpha = _make_component('alpha')

a = alpha
"""Convenience synonym for :attr:`alpha`."""
Expand Down
35 changes: 6 additions & 29 deletions pystacia/image/_impl/blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@
# the MIT License: http://www.opensource.org/licenses/mit-license.php


def blur(image, radius, strength):
if strength is None:
strength = radius

c_call(image, 'blur', radius, strength)


def _make_radius_strength_bias(c_name, names, order=None):
def function(image, *args):
kwargs = dict(zip(names, args))

if kwargs['strength'] is None:
kwargs['strength'] = kwargs['radius']

if kwargs['bias'] is None:
if 'bias' in kwargs and kwargs['bias'] is None:
kwargs['bias'] = 0

order_ = order or names
Expand All @@ -33,6 +26,9 @@ def function(image, *args):
return function


blur = _make_radius_strength_bias('blur', ['radius', 'strength'])


gaussian_blur = _make_radius_strength_bias(
'gaussian_blur', ['radius', 'strength', 'bias'])

Expand All @@ -54,11 +50,7 @@ def function(image, *args):
'adaptive_sharpen', ['radius', 'strength', 'bias'])


def detect_edges(image, radius, strength):
if strength is None:
strength = radius

c_call(image, 'edge', radius, strength)
detect_edges = _make_radius_strength_bias('edge', ['radius', 'strength'])


#TODO: moving center here
Expand Down Expand Up @@ -97,22 +89,7 @@ def despeckle(image):
c_call(image, 'despeckle')


def emboss(image, radius, strength):
"""Apply edge detecting algorithm.
:param radius: filter radius
:type radius: ``int``
:param stregth: filter strength (sigma)
:type strength: ``int``
On a typical photo creates effect of raised edges.
This method can be chained.
"""
if strength is None:
strength = radius

c_call(image, 'emboss', radius, strength)
emboss = _make_radius_strength_bias('emboss', ['radius', 'strength'])


from pystacia.api.func import c_call

0 comments on commit 60f5754

Please sign in to comment.