Skip to content

Commit 67f1b13

Browse files
committed
de-circularized imports
1 parent 54f055b commit 67f1b13

3 files changed

Lines changed: 46 additions & 46 deletions

File tree

plotdevice/gfx/atoms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ..util import _copy_attrs, _copy_attr, _flatten, trim_zeroes, numlike
1010
from .colors import Color
1111
from .geometry import Transform, Dimension, Region, Pair
12+
from .effects import Effect
1213

1314
_ctx = None
1415
__all__ = [
@@ -112,7 +113,6 @@ class EffectsMixin(Grob):
112113
opts = ('alpha','blend','shadow')
113114

114115
def __init__(self, **kwargs):
115-
from .effects import Effect
116116
super(EffectsMixin, self).__init__(**kwargs)
117117
for attr in EffectsMixin.opts:
118118
if attr in kwargs:

plotdevice/gfx/effects.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from ..util import _copy_attr, _copy_attrs, numlike
99
from .colors import Color
1010
from .geometry import Point
11-
from .image import ciFilter
1211
from . import _cg_context, _cg_layer, _cg_port
1312

1413
_ctx = None
@@ -344,3 +343,48 @@ def applied(self):
344343

345344
class ClippingPath(Stencil):
346345
pass # NodeBox compat...
346+
347+
348+
### core-image filters for channel separation and inversion ###
349+
350+
def ciFilter(opt, img):
351+
_filt = _inversionFilter if isinstance(opt, bool) else _channelFilter
352+
return _filt(opt, img)
353+
354+
def _channelFilter(channel, img):
355+
"""Generate a greyscale image by isolating a single r/g/b/a channel"""
356+
357+
rgb = ('red', 'green', 'blue')
358+
if channel=='alpha':
359+
transmat = [(0, 0, 0, 1)] * 3
360+
transmat += [ (0,0,0,0), (0,0,0,1) ]
361+
elif channel in rgb:
362+
rgb_row = [0,0,0]
363+
rgb_row.insert(rgb.index(channel), 1.0)
364+
transmat = [tuple(rgb_row)] * 3
365+
transmat += [ (0,0,0,0), (0,0,0,1) ]
366+
elif channel in ('black', 'white'):
367+
transmat = [(.333, .333, .333, 0)] * 3
368+
transmat += [ (0,0,0,0), (0,0,0,1) ]
369+
return _matrixFilter(transmat, img)
370+
371+
def _inversionFilter(identity, img):
372+
"""Conditionally turn black to white and up to down"""
373+
374+
# set up a matrix that's either identity or an r/g/b inversion
375+
polarity = -1.0 if not identity else 1.0
376+
bias = 0 if polarity>0 else 1
377+
transmat = [(polarity, 0, 0, 0), (0, polarity, 0, 0), (0, 0, polarity, 0),
378+
(0, 0, 0, 0), (bias, bias, bias, 1)]
379+
return _matrixFilter(transmat, img)
380+
381+
def _matrixFilter(matrix, img):
382+
"""Apply a color transform to a CIImage and return the filtered result"""
383+
384+
vectors = ("inputRVector", "inputGVector", "inputBVector", "inputAVector", "inputBiasVector")
385+
opts = {k:CIVector.vectorWithX_Y_Z_W_(*v) for k,v in zip(vectors, matrix)}
386+
opts[kCIInputImageKey] = img
387+
remap = CIFilter.filterWithName_("CIColorMatrix")
388+
for k,v in opts.items():
389+
remap.setValue_forKey_(v, k)
390+
return remap.valueForKey_("outputImage")

plotdevice/gfx/image.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -247,50 +247,6 @@ def _draw(self):
247247
# NB: the nodebox source warns about quartz bugs triggered by drawing
248248
# EPSs to other origin points. no clue whether this still applies...
249249

250-
### core-image filters for channel separation and inversion ###
251-
252-
def ciFilter(opt, img):
253-
_filt = _inversionFilter if isinstance(opt, bool) else _channelFilter
254-
return _filt(opt, img)
255-
256-
def _channelFilter(channel, img):
257-
"""Generate a greyscale image by isolating a single r/g/b/a channel"""
258-
259-
rgb = ('red', 'green', 'blue')
260-
if channel=='alpha':
261-
transmat = [(0, 0, 0, 1)] * 3
262-
transmat += [ (0,0,0,0), (0,0,0,1) ]
263-
elif channel in rgb:
264-
rgb_row = [0,0,0]
265-
rgb_row.insert(rgb.index(channel), 1.0)
266-
transmat = [tuple(rgb_row)] * 3
267-
transmat += [ (0,0,0,0), (0,0,0,1) ]
268-
elif channel in ('black', 'white'):
269-
transmat = [(.333, .333, .333, 0)] * 3
270-
transmat += [ (0,0,0,0), (0,0,0,1) ]
271-
return _matrixFilter(transmat, img)
272-
273-
def _inversionFilter(identity, img):
274-
"""Conditionally turn black to white and up to down"""
275-
276-
# set up a matrix that's either identity or an r/g/b inversion
277-
polarity = -1.0 if not identity else 1.0
278-
bias = 0 if polarity>0 else 1
279-
transmat = [(polarity, 0, 0, 0), (0, polarity, 0, 0), (0, 0, polarity, 0),
280-
(0, 0, 0, 0), (bias, bias, bias, 1)]
281-
return _matrixFilter(transmat, img)
282-
283-
def _matrixFilter(matrix, img):
284-
"""Apply a color transform to a CIImage and return the filtered result"""
285-
286-
vectors = ("inputRVector", "inputGVector", "inputBVector", "inputAVector", "inputBiasVector")
287-
opts = {k:CIVector.vectorWithX_Y_Z_W_(*v) for k,v in zip(vectors, matrix)}
288-
opts[kCIInputImageKey] = img
289-
remap = CIFilter.filterWithName_("CIColorMatrix")
290-
for k,v in opts.items():
291-
remap.setValue_forKey_(v, k)
292-
return remap.valueForKey_("outputImage")
293-
294250

295251
### context manager for calls to `with export(...)` ###
296252

0 commit comments

Comments
 (0)