def test_images_extracted_when_just_variants_are_images(self): layer_styles = [ LayerStyle.create_from_sketch_dict(flat_color("*/element/image")), LayerStyle.create_from_sketch_dict( image_fill_fit("*/element[selected]/image")), LayerStyle.create_from_sketch_dict( image_fill_fit("*/element[disabled]/image")), ] group = VariantGroup.create_array_from_base_styles(layer_styles) found_layers = group[0].items_with_layer_style_image() self.assertEqual(len(found_layers), 2, "image layer styles are extracted from variants")
def test_variant_groups_images(self): layer_styles = [ LayerStyle.create_from_sketch_dict( image_fill_fit("*/element/image")), LayerStyle.create_from_sketch_dict( image_fill_fit("*/element[selected]/image")), LayerStyle.create_from_sketch_dict( image_fill_fit("*/element[disabled]/image")), ] group = VariantGroup.create_array_from_base_styles(layer_styles) self.assertEqual( len(group), 1, "array of layer styles with 1 element create 1 variant group")
def test_images_extracted_when_all_images(self): layer_styles = [ LayerStyle.create_from_sketch_dict( image_fill_fit("*/element/image")), LayerStyle.create_from_sketch_dict( image_fill_fit("*/element[selected]/image")), LayerStyle.create_from_sketch_dict( image_fill_fit("*/element[disabled]/image")), ] group = VariantGroup.create_array_from_base_styles(layer_styles) found_layers = group[0].items_with_layer_style_image() self.assertCountEqual(layer_styles, found_layers, "image layer styles are extracted")
def test_create_radial_gradient_layer_style(self): sketch_dict = layer_styles_data.radial_gradient() generated = LayerStyle.create_from_sketch_dict(sketch_dict) self.assertEqual(generated.name, "*/*/radialGradient") self.assertEqual(generated.atom, "radialGradient") self.assertIsNone(generated.element) self.assertIsNone(generated.section) self.assertIsNone(generated.outline) self.assertIsNone(generated.image) self.assertIsNotNone(generated.gradient) self.assertEqual(generated.color.red, 0) self.assertEqual(generated.color.green, 0) self.assertEqual(generated.color.blue, 0) self.assertEqual(generated.color.alpha, 0) gradient = generated.gradient self.assertFalse(gradient['isAxial']) self.assertTrue(gradient['isRadial']) self.assertEqual(gradient['locations'], [0, 1]) self.assertEqual(len(gradient['colors']), 2) self.assertEqual(gradient['colors'][0].red, 0.8916280506641954) self.assertEqual(gradient['colors'][0].green, 0.2812364847457446) self.assertEqual(gradient['colors'][0].blue, 0.2812364847457446) self.assertEqual(gradient['colors'][0].alpha, 1.0) self.assertEqual(gradient['colors'][1].red, 0.3298522534013605) self.assertEqual(gradient['colors'][1].green, 0.02545083970669037) self.assertEqual(gradient['colors'][1].blue, 0.02545083970669037) self.assertEqual(gradient['colors'][1].alpha, 1.0) self.assertEqual(gradient['from'], Point(0.5, 0.53427734375)) self.assertEqual(gradient['to'], Point(0.5, 1.53427734375))
def parse(sketch_json: dict) -> dict: original = [LayerStyle.create_from_sketch_dict(i) for i in sketch_json['layerStyles']['objects']] original.extend([LayerStyle.create_from_sketch_dict(i) for i in sketch_json['foreignLayerStyles']]) simplified = VariantGroup.create_array_from_base_styles(original) buckets = {} buckets["section_element_atom"] = [ag for ag in simplified if ag.grouping == ItemGrouping.SECTION_ELEMENT_ATOM] buckets["section_atom"] = [ag for ag in simplified if ag.grouping == ItemGrouping.SECTION_ATOM] buckets["element_atom"] = [ag for ag in simplified if ag.grouping == ItemGrouping.ELEMENT_ATOM] buckets["atom"] = [ag for ag in simplified if ag.grouping == ItemGrouping.ATOM] buckets["has_section_element_atom"] = len(buckets["section_element_atom"]) > 0 buckets["has_section_atom"] = len(buckets["section_atom"]) > 0 buckets["has_element_atom"] = len(buckets["element_atom"]) > 0 # enum data buckets["atoms"] = unique_entries_dict(simplified, "atom") buckets["elements"] = unique_entries_dict(simplified, "element") buckets["sections"] = unique_entries_dict(simplified, "section") buckets["variants"] = unique_entries_dict(original, "variant") data = { "objects": simplified, "grouped": buckets } return data
def test_create_noise_fill_layer_style(self): sketch_dict = layer_styles_data.noise_fill() generated = LayerStyle.create_from_sketch_dict(sketch_dict) self.assertEqual(generated.name, "*/*/noiseFill") self.assertEqual(generated.atom, "noiseFill") self.assertIsNone(generated.element) self.assertIsNone(generated.section) self.assertIsNone(generated.outline) self.assertIsNotNone(generated.image) self.assertIsNone(generated.gradient) self.assertEqual(generated.color.red, 0) self.assertEqual(generated.color.green, 0) self.assertEqual(generated.color.blue, 0) self.assertEqual(generated.color.alpha, 0)
def test_create_flat_color_layer_styles(self): sketch_dict = layer_styles_data.flat_color() generated = LayerStyle.create_from_sketch_dict(sketch_dict) self.assertEqual(generated.name, "*/*/flatColor") self.assertEqual(generated.atom, "flatColor") self.assertIsNone(generated.element) self.assertIsNone(generated.section) self.assertIsNone(generated.gradient) self.assertIsNone(generated.outline) self.assertIsNone(generated.image) self.assertEqual(generated.color.red, 0.7669536564625851) self.assertEqual(generated.color.green, 0.1353305220841493) self.assertEqual(generated.color.blue, 0.1353305220841493) self.assertEqual(generated.color.alpha, 1.0)
def test_create_dashed_outline(self): sketch_dict = layer_styles_data.dashed_outline() generated = LayerStyle.create_from_sketch_dict(sketch_dict) self.assertEqual(generated.name, "*/*/dashed_outline") self.assertEqual(generated.atom, "dashed_outline") self.assertIsNone(generated.element) self.assertIsNone(generated.section) self.assertIsNotNone(generated.outline) self.assertIsNone(generated.image) self.assertIsNone(generated.gradient) outline = generated.outline self.assertEqual(outline["color"].red, 0) self.assertEqual(outline["color"].green, 0) self.assertEqual(outline["color"].blue, 0) self.assertEqual(outline["color"].alpha, 1.0) self.assertEqual(outline["line_width"], 1) self.assertEqual(outline["dash_pattern"], [5]) self.assertTrue(outline["is_dash"])
def test_create_image_fill_stretch_layer_style(self): sketch_dict = layer_styles_data.image_fill_stretch() generated = LayerStyle.create_from_sketch_dict(sketch_dict) self.assertEqual(generated.name, "*/*/patternFill_Stretch") self.assertEqual(generated.atom, "patternFill_Stretch") self.assertIsNone(generated.element) self.assertIsNone(generated.section) self.assertIsNone(generated.outline) self.assertIsNotNone(generated.image) self.assertIsNone(generated.gradient) self.assertEqual(generated.color.red, 0) self.assertEqual(generated.color.green, 0) self.assertEqual(generated.color.blue, 0) self.assertEqual(generated.color.alpha, 0) image = generated.image self.assertEqual(image["file_name"], "layer_*_*_patternFill_Stretch") self.assertEqual(image["tile_scale"], 1) self.assertEqual(image["fill_type"], 2) self.assertEqual(image["resizingMode"], "stretch") self.assertEqual(image["gravity"], "resize")