def from_dict(cls, data): """Create a Mesh2D from a dictionary. Args: data: A python dictionary in the following format .. code-block:: python { "type": "Mesh2D", "vertices": [(0, 0), (10, 0), (0, 10)], "faces": [(0, 1, 2)], "colors": [{"r": 255, "g": 0, "b": 0}] } """ colors = None if 'colors' in data and data['colors'] is not None and len(data['colors']) != 0: try: from ladybug.color import Color except ImportError: raise ImportError('Colors are specified in input Mesh2D dictionary ' 'but failed to import ladybug.color') colors = tuple(Color.from_dict(col) for col in data['colors']) fcs = tuple(tuple(f) for f in data['faces']) # cast to immutable type return cls(tuple(Point2D.from_array(pt) for pt in data['vertices']), fcs, colors)
def from_dict(cls, data): """Create a Mesh2D from a dictionary. Args: data: A python dictionary in the following format .. code-block:: json { "type": "Mesh2D", "vertices": [[0, 0], [10, 0], [0, 10]], "faces": [[0, 1, 2]], "colors": [{"r": 255, "g": 0, "b": 0}] } """ colors = None if 'colors' in data and data['colors'] is not None: try: from ladybug.color import Color except ImportError: raise ImportError( 'Colors are specified in input Mesh2D dictionary ' 'but failed to import ladybug.color') colors = tuple(Color.from_dict(col) for col in data['colors']) return cls(tuple(Point2D(pt[0], pt[1]) for pt in data['vertices']), data['faces'], colors)
def test_from_dict(): """Test the from dict method.""" sample_dict = {'r': '255', 'g': '0', 'b': '100'} color = Color.from_dict(sample_dict) assert isinstance(color, Color) assert color.r == 255 assert color.g == 0 assert color.b == 100
def test_to_from_dict(): """Test the to/from dict methods.""" color = Color(255, 0, 100) color_dict = color.to_dict() new_color = Color.from_dict(color_dict) assert new_color.to_dict() == color_dict