Skip to content

Commit cd8de27

Browse files
committed
Added an rgb to hex color setup and direct addition of ganjascenes
1 parent 75de670 commit cd8de27

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

pyganja/GanjaScene.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ def __init__(self):
99
self.internal_list = []
1010
self.mv_length = 32
1111

12+
def __add__(self, other):
13+
if isinstance(other, GanjaScene):
14+
gs = GanjaScene()
15+
gs.internal_list = self.internal_list + other.internal_list
16+
return gs
17+
else:
18+
raise ValueError('The objects being added are not both GanjaScenes...')
19+
1220
def add_object(self, mv_array, color=int('AA000000', 16), label=None, static=False):
1321
self.mv_length = len(mv_array)
1422
if isinstance(color, enum.Enum):

pyganja/color.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11

22
from enum import Enum
33

4+
def rgb2hex(x):
5+
if len(x) == 3:
6+
return int('%02x%02x%02x' %(int(x[0]),int(x[1]),int(x[2])),16)
7+
elif len(x) == 4:
8+
return int('%02x%02x%02x%02x' %(int(x[0]),int(x[1]),int(x[2]),int(x[3])),16)
9+
else:
10+
raise ValueError('X must be of length 3 or 4, ie. an rgb or argb array')
11+
412
class Color(Enum):
513
BLUE = int('000000FF', 16)
614
RED = int('00FF0000', 16)

pyganja/script_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ def render_scene_string_appropriately(scene_string):
241241
render_scene_string_appropriately(str(objects))
242242
else:
243243
try:
244-
render_scene_string_appropriately([i for i in objects])
244+
print('Treating as iterable')
245+
sc = GanjaScene()
246+
sc.add_objects([i for i in objects], color=color, static=static)
247+
render_scene_string_appropriately(str(sc))
245248
except:
246249
raise ValueError('The input cannot be interpreted, it is not a list of objects or ganja scene')

test_pyganja.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,15 @@ def test_draw_points(self):
4242
render_cef_script(str(gs), sig=layout.sig, grid=True, scale=1.0, gl=False)
4343

4444

45+
class TestGanjaSceneOps(unittest.TestCase):
46+
def test_scene_addition(self):
47+
from clifford.tools.g3c import random_line, random_circle
48+
a = GanjaScene()
49+
a.add_objects([random_line() for i in range(10)], color=Color.RED)
50+
b = GanjaScene()
51+
b.add_objects([random_circle() for i in range(10)], color=Color.BLUE)
52+
draw(a + b, scale=0.01)
53+
54+
4555
if __name__ == '__main__':
4656
unittest.main()

0 commit comments

Comments
 (0)