Skip to content

Commit

Permalink
Added an rgb to hex color setup and direct addition of ganjascenes
Browse files Browse the repository at this point in the history
  • Loading branch information
hugohadfield committed May 30, 2019
1 parent 75de670 commit cd8de27
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pyganja/GanjaScene.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ def __init__(self):
self.internal_list = []
self.mv_length = 32

def __add__(self, other):
if isinstance(other, GanjaScene):
gs = GanjaScene()
gs.internal_list = self.internal_list + other.internal_list
return gs
else:
raise ValueError('The objects being added are not both GanjaScenes...')

def add_object(self, mv_array, color=int('AA000000', 16), label=None, static=False):
self.mv_length = len(mv_array)
if isinstance(color, enum.Enum):
Expand Down
8 changes: 8 additions & 0 deletions pyganja/color.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

from enum import Enum

def rgb2hex(x):
if len(x) == 3:
return int('%02x%02x%02x' %(int(x[0]),int(x[1]),int(x[2])),16)
elif len(x) == 4:
return int('%02x%02x%02x%02x' %(int(x[0]),int(x[1]),int(x[2]),int(x[3])),16)
else:
raise ValueError('X must be of length 3 or 4, ie. an rgb or argb array')

class Color(Enum):
BLUE = int('000000FF', 16)
RED = int('00FF0000', 16)
Expand Down
5 changes: 4 additions & 1 deletion pyganja/script_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ def render_scene_string_appropriately(scene_string):
render_scene_string_appropriately(str(objects))
else:
try:
render_scene_string_appropriately([i for i in objects])
print('Treating as iterable')
sc = GanjaScene()
sc.add_objects([i for i in objects], color=color, static=static)
render_scene_string_appropriately(str(sc))
except:
raise ValueError('The input cannot be interpreted, it is not a list of objects or ganja scene')
10 changes: 10 additions & 0 deletions test_pyganja.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,15 @@ def test_draw_points(self):
render_cef_script(str(gs), sig=layout.sig, grid=True, scale=1.0, gl=False)


class TestGanjaSceneOps(unittest.TestCase):
def test_scene_addition(self):
from clifford.tools.g3c import random_line, random_circle
a = GanjaScene()
a.add_objects([random_line() for i in range(10)], color=Color.RED)
b = GanjaScene()
b.add_objects([random_circle() for i in range(10)], color=Color.BLUE)
draw(a + b, scale=0.01)


if __name__ == '__main__':
unittest.main()

0 comments on commit cd8de27

Please sign in to comment.