Skip to content

Instantly share code, notes, and snippets.

@LettError
Created March 3, 2015 14:18
Show Gist options
  • Save LettError/6565405279547124e992 to your computer and use it in GitHub Desktop.
Save LettError/6565405279547124e992 to your computer and use it in GitHub Desktop.
Example for MutatorMath in Drawbot. It shows how to make math objects for a color and a shape and how to process them with MutatorMath.
from __future__ import division
from mutatorMath.objects.location import Location
from mutatorMath.objects.mutator import buildMutator
"""
Simple demo that can be run in Drawbot
http://www.drawbot.com
Colorful example for MutatorMath in Drawbot.
This shows 2 objects, a color and a rectangle,
that both respond to the basic math that MutatorMath
needs.
You will see the colors interpolate, and the swatches draw.
"""
class MathColor(object):
""" An object that can do basic math with color factors """
def __init__(self, *args):
self.values = list(args)
def __add__(self, other):
if len(self.values)!=len(other.values):
raise ValueError
new = self.__class__(*self.values)
for i in range(len(self.values)):
new.values[i] = self.values[i]+other.values[i]
return new
def __sub__(self, other):
if len(self.values)!=len(other.values):
raise ValueError
new = self.__class__(*self.values)
for i in range(len(self.values)):
new.values[i] = self.values[i]-other.values[i]
return new
def __mul__(self, factor):
new = self.__class__(*self.values)
for i in range(len(self.values)):
new.values[i] = factor * self.values[i]
return new
__rmul__ = __mul__
def __div__(self, factor):
if factor == 0:
raise ZeroDivisionError
return self.__mul__(1.0/factor)
__rdiv__ = __div__
def setFill(self):
# drawbot specific
gamutWarning = False
for v in self.values:
if v < 0 or v > 1:
gamutWarning = True
break
if not gamutWarning:
if len(self.values)==0:
fill(None)
else:
fill(*self.values[:5])
else:
stroke(0.5)
def __repr__(self):
return "<MathColor %s>"%" ".join(["%2.1f"%v for v in self.values])
class Swatch(object):
def __init__(self, (x, y), (width, height), color):
self.pos = x,y
self.size = width, height
self.color = MathColor(*color)
def draw(self):
# this works only if we're in drawbot of course.
save()
self.color.setFill()
oval(self.pos[0], self.pos[1], self.size[0], self.size[1])
restore()
def __add__(self, other):
pos = (self.pos[0]+other.pos[0], self.pos[1]+other.pos[1])
sz = (self.size[0]+other.size[0], self.size[1]+other.size[1])
clr = self.color + other.color
return self.__class__(pos, sz, clr.values)
def __sub__(self, other):
pos = (self.pos[0]-other.pos[0], self.pos[1]-other.pos[1])
sz = (self.size[0]-other.size[0], self.size[1]-other.size[1])
clr = self.color - other.color
return self.__class__(pos, sz, clr.values)
def __div__(self, factor):
if factor == 0:
raise ZeroDivisionError
factor = 1/factor
return self.__mul__(1/factor)
__rdiv__ = __div__
def __mul__(self, factor):
pos = (self.pos[0]*factor, self.pos[1]*factor)
sz = (self.size[0]*factor, self.size[1]*factor)
clr = self.color * factor
return self.__class__(pos, sz, clr.values)
__rmul__ = __mul__
def __repr__(self):
return "<Swatch %3.1f %3.1fs %s>"%(self.pos[0], self.pos[1], self.color)
b = 150
s1 = Swatch((10, 100), (b*0.25, b*0.25), (1, 0, 0))
s2 = Swatch((800, 0), (b, b), (0, 0, 1))
s3 = Swatch((100, 900), (b, b), (1, 1, 0))
s4 = Swatch((1000, 700), (b*0.25, b*0.25), (1, 0, 1))
items = [
# experiment by commenting out one of these items
# and see how this influences the designspace.
(Location(blues=0), s1),
(Location(blues=1), s2),
(Location(reds=1), s3),
(Location(reds=1, blues=1), s4),
]
# build the mutator with these swatches
# note that the swatches have their own size and position
# in the drawing. Their positions interpolate as well.
ok, m = buildMutator(items)
for i in range(0, 10):
for j in range(0, 10):
s = m.makeInstance(Location(blues=i*0.1, reds=j*0.1))
s.draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment