Skip to content

Commit

Permalink
🎨 Add support for P3 color space (#115)
Browse files Browse the repository at this point in the history
Adds support for setting the output Sketch document's colour space to either sRGB or P3 based on the fig input
  • Loading branch information
tmdvs committed May 16, 2024
1 parent 8593f4d commit d8b6bf5
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/converter/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def find_symbols(node: dict) -> List[Sequence[int]]:


class Context:
def init(self, components_page: Optional[dict], id_map: Dict[Sequence[int], dict]) -> None:
def init(
self, components_page: Optional[dict], id_map: Dict[Sequence[int], dict], color_space: str
) -> None:
self._color_space = color_space
self._sketch_components: Dict[Sequence[int], Swatch] = {}
self.symbols_page = None
self._node_by_id = id_map
Expand All @@ -30,6 +33,9 @@ def init(self, components_page: Optional[dict], id_map: Dict[Sequence[int], dict
# width -> (x, y)
self._symbol_position = {0: [0, 0]}

def color_space(self) -> int:
return 2 if self._color_space == "DISPLAY_P3" else 1

def component(self, cid: Sequence[int]) -> Tuple[dict, Optional[Swatch]]:
fig_component = self.fig_node(cid)

Expand Down
7 changes: 6 additions & 1 deletion src/converter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def convert_fig_tree_to_sketch(
# We should either bring the fonts to the same indexed_components to pass
# them as parameter or move the indexed components to the component file
# and store there the components, for consistency purposes
context.init(components_page, id_map)
if "documentColorProfile" in fig["document"]:
color_space = fig["document"]["documentColorProfile"]
else:
color_space = "sRGB"

context.init(components_page, id_map, color_space)

# Convert all normal pages
sketch_pages: List[Page] = convert_pages(fig_pages, output)
Expand Down
2 changes: 1 addition & 1 deletion src/converter/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def convert(pages: List[Page], output_zip: zipfile.ZipFile) -> dict:
"gradients": [],
"exportPresets": [],
},
"colorSpace": 1,
"colorSpace": context.color_space(),
"currentPageIndex": 0,
"foreignLayerStyles": [],
"foreignSymbols": [],
Expand Down
2 changes: 1 addition & 1 deletion tests/converter/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_gradient_background(self, warnings):

@pytest.fixture
def style_overrides(monkeypatch):
context.init(None, {(0, 1): FIG_TEXT_STYLE, (0, 2): FIG_COLOR_STYLE})
context.init(None, {(0, 1): FIG_TEXT_STYLE, (0, 2): FIG_COLOR_STYLE}, "DISPLAY_P3")


@pytest.mark.usefixtures("style_overrides")
Expand Down
2 changes: 1 addition & 1 deletion tests/converter/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

Expand Down
6 changes: 5 additions & 1 deletion tests/converter/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@

@pytest.fixture
def symbol(monkeypatch):
context.init(None, {(0, 3): FIG_SYMBOL, (0, 1): FIG_TEXT, (0, 2): FIG_RECT, (1, 9): FIG_TEXT})
context.init(
None,
{(0, 3): FIG_SYMBOL, (0, 1): FIG_TEXT, (0, 2): FIG_RECT, (1, 9): FIG_TEXT},
"DISPLAY_P3",
)
context._component_symbols = {(0, 3): False}


Expand Down
5 changes: 3 additions & 2 deletions tests/converter/test_prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ def canvas(monkeypatch):
(0, 4): FIG_ARTBOARD,
(0, 5): FIG_OVERLAY,
},
"DISPLAY_P3",
)


@pytest.fixture
def overlay(monkeypatch):
context.init(None, {(0, 5): FIG_OVERLAY})
context.init(None, {(0, 5): FIG_OVERLAY}, "DISPLAY_P3")


@pytest.fixture
def manual_overlay(monkeypatch):
context.init(None, {(0, 6): FIG_MANUAL_OVERLAY})
context.init(None, {(0, 6): FIG_MANUAL_OVERLAY}, "DISPLAY_P3")


@pytest.mark.usefixtures("canvas")
Expand Down
2 changes: 1 addition & 1 deletion tests/converter/test_shape_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_arrow_override(self):

def test_complex_vector():
figtree, id_map = fig2tree.convert_fig("tests/data/vector.fig", None)
context.init(None, id_map)
context.init(None, id_map, "DISPLAY_P3")
figpage = figtree["document"]["children"][0]
page = tree.convert_node(figpage, "DOCUMENT")
vector = page.layers[0].layers[0]
Expand Down
2 changes: 1 addition & 1 deletion tests/converter/test_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@pytest.fixture
def empty_context(monkeypatch):
context.init(None, {})
context.init(None, {}, "DISPLAY_P3")


def test_rounded_corners(no_prototyping, empty_context):
Expand Down

0 comments on commit d8b6bf5

Please sign in to comment.