swatch: Create a colour swatch from a traversal of the RGB colour cube.

This commit is contained in:
Aldo Cortesi 2010-01-26 17:19:48 +13:00
parent 901024ee60
commit 552b91da30
3 changed files with 72 additions and 2 deletions

View File

@ -131,3 +131,32 @@ class Curve:
def save(self, fname):
self.c.save(fname)
class Swatch:
def __init__(self, curve, colorwidth, height):
"""
Color swatches from the RGB color cube.
curve: A curve with dimension 3.
colorwidth: Width of an individual color. Image width will be
len(curve)*colorwidth.
height: Height of the image
"""
self.curve, self.colorwidth, self.height = curve, colorwidth, height
self.c = Canvas(len(self.curve) * colorwidth, self.height)
self.ctx = self.c.ctx()
self.ctx.set_antialias(False)
def save(self, fname):
d = float(self.curve.dimensions()[0])
offset = 0
for r, g, b in self.curve:
self.ctx.set_source_rgb(
r/d, g/d, b/d
)
self.ctx.rectangle(offset, 0, self.colorwidth, self.height)
self.ctx.fill()
offset += self.colorwidth
self.c.save(fname)

View File

@ -87,8 +87,8 @@ class Hilbert:
Size is the total number of points in the curve.
"""
x = math.log(size, 2)
if not x == int(x):
raise ValueError("Size does not fit a square Hilbert curve.")
if not float(x)/dimension == int(x)/dimension:
raise ValueError("Size does not fit Hilbert curve of dimension %s."%dimension)
return Hilbert(dimension, int(x/dimension))
def __len__(self):

41
swatch Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env python
import os.path, math
import scurve
from scurve import draw
def main():
from optparse import OptionParser, OptionGroup
parser = OptionParser(
usage = "%prog [options] output",
version="%prog 0.1",
)
parser.add_option(
"-s", "--height", action="store",
type = "int", dest="height", default=50,
help = "Height of the swatch."
)
parser.add_option(
"-w", "--colorwidth", action="store",
type = "int", dest="colorwidth", default=10,
help = "Width of each color."
)
parser.add_option(
"-p", "--points", action="store",
type="int", dest="points", default=64,
help = "Total number of points on the curve."
)
parser.add_option(
"-c", "--curve", action="store",
type="str", dest="curve", default="hilbert"
)
options, args = parser.parse_args()
if len(args) != 1:
parser.error("Please specify output file.")
c = scurve.fromSize(options.curve, 3, options.points)
d = draw.Swatch(c, options.colorwidth, options.height)
d.save(args[0])
main()