Skip to content

Commit d8b6bf5

Browse files
authored
🎨 Add support for P3 color space (#115)
Adds support for setting the output Sketch document's colour space to either sRGB or P3 based on the fig input
1 parent 8593f4d commit d8b6bf5

File tree

9 files changed

+26
-10
lines changed

9 files changed

+26
-10
lines changed

src/converter/context.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ def find_symbols(node: dict) -> List[Sequence[int]]:
1717

1818

1919
class Context:
20-
def init(self, components_page: Optional[dict], id_map: Dict[Sequence[int], dict]) -> None:
20+
def init(
21+
self, components_page: Optional[dict], id_map: Dict[Sequence[int], dict], color_space: str
22+
) -> None:
23+
self._color_space = color_space
2124
self._sketch_components: Dict[Sequence[int], Swatch] = {}
2225
self.symbols_page = None
2326
self._node_by_id = id_map
@@ -30,6 +33,9 @@ def init(self, components_page: Optional[dict], id_map: Dict[Sequence[int], dict
3033
# width -> (x, y)
3134
self._symbol_position = {0: [0, 0]}
3235

36+
def color_space(self) -> int:
37+
return 2 if self._color_space == "DISPLAY_P3" else 1
38+
3339
def component(self, cid: Sequence[int]) -> Tuple[dict, Optional[Swatch]]:
3440
fig_component = self.fig_node(cid)
3541

src/converter/convert.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ def convert_fig_tree_to_sketch(
1414
# We should either bring the fonts to the same indexed_components to pass
1515
# them as parameter or move the indexed components to the component file
1616
# and store there the components, for consistency purposes
17-
context.init(components_page, id_map)
17+
if "documentColorProfile" in fig["document"]:
18+
color_space = fig["document"]["documentColorProfile"]
19+
else:
20+
color_space = "sRGB"
21+
22+
context.init(components_page, id_map, color_space)
1823

1924
# Convert all normal pages
2025
sketch_pages: List[Page] = convert_pages(fig_pages, output)

src/converter/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def convert(pages: List[Page], output_zip: zipfile.ZipFile) -> dict:
2323
"gradients": [],
2424
"exportPresets": [],
2525
},
26-
"colorSpace": 1,
26+
"colorSpace": context.color_space(),
2727
"currentPageIndex": 0,
2828
"foreignLayerStyles": [],
2929
"foreignSymbols": [],

tests/converter/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_gradient_background(self, warnings):
113113

114114
@pytest.fixture
115115
def style_overrides(monkeypatch):
116-
context.init(None, {(0, 1): FIG_TEXT_STYLE, (0, 2): FIG_COLOR_STYLE})
116+
context.init(None, {(0, 1): FIG_TEXT_STYLE, (0, 2): FIG_COLOR_STYLE}, "DISPLAY_P3")
117117

118118

119119
@pytest.mark.usefixtures("style_overrides")

tests/converter/test_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
def test_corrupted_images(warnings):
1212
figtree, id_map = fig2tree.convert_fig("tests/data/broken_images.fig", None)
13-
context.init(None, id_map)
13+
context.init(None, id_map, "DISPLAY_P3")
1414
figpage = figtree["document"]["children"][0]
1515
page = tree.convert_node(figpage, "DOCUMENT")
1616

tests/converter/test_instance.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@
4949

5050
@pytest.fixture
5151
def symbol(monkeypatch):
52-
context.init(None, {(0, 3): FIG_SYMBOL, (0, 1): FIG_TEXT, (0, 2): FIG_RECT, (1, 9): FIG_TEXT})
52+
context.init(
53+
None,
54+
{(0, 3): FIG_SYMBOL, (0, 1): FIG_TEXT, (0, 2): FIG_RECT, (1, 9): FIG_TEXT},
55+
"DISPLAY_P3",
56+
)
5357
context._component_symbols = {(0, 3): False}
5458

5559

tests/converter/test_prototype.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,18 @@ def canvas(monkeypatch):
7474
(0, 4): FIG_ARTBOARD,
7575
(0, 5): FIG_OVERLAY,
7676
},
77+
"DISPLAY_P3",
7778
)
7879

7980

8081
@pytest.fixture
8182
def overlay(monkeypatch):
82-
context.init(None, {(0, 5): FIG_OVERLAY})
83+
context.init(None, {(0, 5): FIG_OVERLAY}, "DISPLAY_P3")
8384

8485

8586
@pytest.fixture
8687
def manual_overlay(monkeypatch):
87-
context.init(None, {(0, 6): FIG_MANUAL_OVERLAY})
88+
context.init(None, {(0, 6): FIG_MANUAL_OVERLAY}, "DISPLAY_P3")
8889

8990

9091
@pytest.mark.usefixtures("canvas")

tests/converter/test_shape_path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_arrow_override(self):
139139

140140
def test_complex_vector():
141141
figtree, id_map = fig2tree.convert_fig("tests/data/vector.fig", None)
142-
context.init(None, id_map)
142+
context.init(None, id_map, "DISPLAY_P3")
143143
figpage = figtree["document"]["children"][0]
144144
page = tree.convert_node(figpage, "DOCUMENT")
145145
vector = page.layers[0].layers[0]

tests/converter/test_symbol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@pytest.fixture
1515
def empty_context(monkeypatch):
16-
context.init(None, {})
16+
context.init(None, {}, "DISPLAY_P3")
1717

1818

1919
def test_rounded_corners(no_prototyping, empty_context):

0 commit comments

Comments
 (0)