Skip to content

Commit

Permalink
Add a factory method for creating curves through the common fromSize …
Browse files Browse the repository at this point in the history
…interface.
  • Loading branch information
cortesi committed Jan 7, 2010
1 parent 0def355 commit 6c7287b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 46 deletions.
27 changes: 4 additions & 23 deletions colorswatch
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import os.path, math
from scurve import hilbert, progress, zigzag, zorder, natural
import scurve
from scurve import progress
import Image, ImageDraw

def sortedPixels(csource, img, quiet):
Expand Down Expand Up @@ -67,28 +68,8 @@ def main():
if len(args) != 2:
parser.error("Please specify output file.")

if options.colorsource == "hilbert":
csource = hilbert.Hilbert.fromSize(3, 256**3)
elif options.colorsource == "zigzag":
csource = zigzag.ZigZag.fromSize(3, 256**3)
elif options.colorsource == "zorder":
csource = zorder.ZOrder.fromSize(3, 256**3)
elif options.colorsource == "natural":
csource = natural.Natural.fromSize(3, 256**3)
else:
parser.error("Unknown color source.")

if options.map == "hilbert":
map = hilbert.Hilbert.fromSize(2, options.size*options.size)
elif options.map == "zigzag":
map = zigzag.ZigZag.fromSize(2, options.size*options.size)
elif options.map == "zorder":
map = zorder.ZOrder.fromSize(2, options.size*options.size)
elif options.map == "natural":
map = natural.Natural.fromSize(2, options.size*options.size)
else:
parser.error("Unknown map.")

csource = scurve.fromSize(options.colorsource, 3, 256**3)
map = scurve.fromSize(options.map, 2, options.size**2)
p = sortedPixels(csource, args[0], options.quiet)
drawmap(map, p, args[1], options.quiet)

Expand Down
18 changes: 18 additions & 0 deletions scurve/__init__.py
@@ -0,0 +1,18 @@
import hilbert, zigzag, zorder, natural


curveMap = {
"hilbert": hilbert.Hilbert,
"zigzag": zigzag.ZigZag,
"zorder": zorder.ZOrder,
"natural": natural.Natural,
}
curves = curveMap.keys()


def fromSize(curve, dimension, size):
"""
A convenience function for creating a specified curve by specifying
size and dimension. All curves implement this common interface.
"""
return curveMap[curve].fromSize(dimension, size)
2 changes: 1 addition & 1 deletion scurve/natural.py
Expand Up @@ -10,7 +10,7 @@ def __init__(self, dimension, size):
dimension: Number of dimensions
size: The size in each dimension
"""
self.dimension, self.size = dimension, size
self.dimension, self.size = int(dimension), int(size)

@classmethod
def fromSize(self, dimension, size):
Expand Down
2 changes: 1 addition & 1 deletion scurve/zigzag.py
Expand Up @@ -11,7 +11,7 @@ def __init__(self, dimension, size):
dimension: Number of dimensions
size: The size in each dimension
"""
self.dimension, self.size = dimension, size
self.dimension, self.size = int(dimension), int(size)

@classmethod
def fromSize(self, dimension, size):
Expand Down
25 changes: 4 additions & 21 deletions testpattern
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import os.path, math
from scurve import hilbert, progress, zigzag, zorder
import scurve
from scurve import progress
import Image, ImageDraw


Expand Down Expand Up @@ -46,27 +47,9 @@ def main():
if len(args) != 1:
parser.error("Please specify output file.")

if options.colorsource == "hilbert":
csource = hilbert.Hilbert.fromSize(3, 256**3)
elif options.colorsource == "zigzag":
csource = zigzag.ZigZag.fromSize(3, 256**3)
elif options.colorsource == "zorder":
csource = zorder.ZOrder.fromSize(3, 256**3)
else:
parser.error("Unknown color source.")

if options.map == "hilbert":
map = hilbert.Hilbert.fromSize(2, options.size*options.size)
elif options.map == "zigzag":
map = zigzag.ZigZag.fromSize(2, options.size*options.size)
elif options.map == "zorder":
map = zorder.ZOrder.fromSize(2, options.size*options.size)
else:
parser.error("Unknown map.")

csource = scurve.fromSize(options.colorsource, 3, 256**3)
map = scurve.fromSize(options.map, 2, options.size**2)
drawmap(map, csource, args[0], options.quiet)



main()

0 comments on commit 6c7287b

Please sign in to comment.