Skip to content

Commit

Permalink
Merge 37e19bf into 91838b0
Browse files Browse the repository at this point in the history
  • Loading branch information
undertherain committed Sep 11, 2018
2 parents 91838b0 + 37e19bf commit 8ede905
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 26 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ before_script: # configure a headless display to test plot generation

script:
- python -m coverage run --source . -m unittest discover --verbose
- PYTHONPATH=./ python -m coverage run ./examples/Ancient\ Map/map.py

after_success:
- python -m coverage report --show-missing
Expand Down
2 changes: 1 addition & 1 deletion contextfree/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version of contextfree package."""

VERSION = "0.2.0"
VERSION = "0.2.1"
48 changes: 43 additions & 5 deletions contextfree/contextfree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import math
import colorsys
import logging
import sys
import numpy as np
import cairocffi as cairo

Expand All @@ -19,6 +20,7 @@
_state = {}
_ctx = None
_background_color = None
_rules = {}


def _init__state():
Expand Down Expand Up @@ -93,6 +95,31 @@ def write_to_png(*args, **kwargs):
return image_surface.write_to_png(*args, **kwargs)


def register_rule(name, proba):
def real_decorator(function):
def wrapper(*args, **kwargs):
raise RuntimeError("This function had been registered as a rule and can not be called directly")
logger.info("registering rule " + name)
if name not in _rules:
_rules[name] = []
last_proba = 0
else:
last_proba = _rules[name][-1][0]
_rules[name].append((last_proba + proba, function))
return wrapper

return real_decorator


def call_rule(name):
rules = _rules[name]
die_roll = prnd(rules[-1][0])
for i in range(len(rules)):
if die_roll < rules[i][0]:
rules[i][1]()
break


def check_limits(some_function):
"""Stop recursion if resolution is too low on number of components is too high """

Expand Down Expand Up @@ -137,6 +164,7 @@ def init(canvas_size=(512, 512), max_depth=12, face_color=None, background_color
global WIDTH
global HEIGHT
_init__state()
sys.setrecursionlimit(20000)
MAX_DEPTH = max_depth
WIDTH, HEIGHT = canvas_size
# surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
Expand Down Expand Up @@ -227,7 +255,7 @@ class color:
TODO: describe which one is additive and which one is multiplicative
"""

def __init__(self, hue=0, lightness=1, saturation=1, alpha=1):
def __init__(self, hue=0, lightness=0, saturation=0, alpha=1):
self.hue = hue
self.lightness = lightness
self.saturation = saturation
Expand All @@ -238,17 +266,27 @@ def __enter__(self):
global _ctx
self.source_old = _ctx.get_source()
r, g, b, a = self.source_old.get_rgba()
hue, lightness, saturation = colorsys.rgb_to_hls(r / 255, g / 255, b / 255)
# print("rgb old:", r, g, b)
hue, lightness, saturation = colorsys.rgb_to_hls(r, g, b)
# print("hls old:", hue, lightness, saturation)
hue = math.modf(hue + self.hue)[0]
lightness = lightness * self.lightness
lightness = lightness + self.lightness
if lightness > 1:
lightness = 1
saturation = saturation * self.saturation
if lightness < 0:
lightness = 0
saturation = saturation + self.saturation
if saturation > 1:
saturation = 1
if saturation < 0:
saturation = 0
r, g, b = colorsys.hls_to_rgb(hue, lightness, saturation)
# print("hls new:", hue, lightness, saturation)
# print("rgb new:", r, g, b)
a = min((a * self.alpha), 255)
rgba = [r * 255, g * 255, b * 255, a]
# rgba = [int(r * 255), int(g * 255), int(b * 255), a]
rgba = [r, g, b, a]
# print(rgba)
_ctx.set_source_rgba(* rgba)

def __exit__(self, type, value, traceback):
Expand Down
Binary file added examples/Ancient Map/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions examples/Ancient Map/map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import logging
import math
from contextfree.contextfree import *


# logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)


@register_rule("wall", 1)
def wall_1():
with translate(0.95, 0):
with rotate(0.01):
with scale(0.975):
ancient_map()


@register_rule("wall", 1)
def wall_2():
box()
with translate(0.95, 0):
with rotate(-0.0027):
with scale(0.975):
with color(hue=0.001, saturation=0.1, lightness=0.003):
ancient_map()


@register_rule("wall", 0.09)
def wall_4():
box()
with scale(0.975):
with translate(0.95, 0):
with rotate(math.pi / 2):
ancient_map()
with rotate(- math.pi / 2):
ancient_map()


@register_rule("wall", 0.05)
def wall_5():
with translate(0.97, 0):
with scale(1.5):
with rotate(math.pi / 2):
ancient_map()
with rotate(- math.pi / 2):
ancient_map()


@check_limits
def ancient_map():
call_rule("wall")
#box()
#with translate(1, 0):
# with scale(0.97):
# with rotate(0.01):
# with color(hue=0.001, saturation=0.1, lightness=0.003):
# ancient_map()


def main():
init(canvas_size=(600, 600), background_color="#e5d5ac", face_color="#0a0707", max_depth=120)

ancient_map()
with rotate(math.pi / 2):
ancient_map()

write_to_png("/tmp/map.png")


if __name__ == "__main__":
main()
12 changes: 5 additions & 7 deletions examples/branches_random.ipynb

Large diffs are not rendered by default.

19 changes: 6 additions & 13 deletions examples/paper.ipynb

Large diffs are not rendered by default.

0 comments on commit 8ede905

Please sign in to comment.