Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ³β€πŸŒˆ More accurate conversion of gradient in borders #26

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion converter/shape_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def get_or_create_point(
fig_point.get("style", {}).get("handleMirroring", "STRAIGHT")
]
point.cornerRadius = fig_point.get("style", {}).get(
"cornerRadius", fig_vector["cornerRadius"]
"cornerRadius", fig_vector.get("cornerRadius", 0)
)

return point
Expand Down
9 changes: 8 additions & 1 deletion converter/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ def convert_gradient(fig_node: dict, fig_fill: dict) -> Gradient:
# Sketch defines the ratio between axis in the item reference point (not the 1x1 square)
# So we scale the 1x1 square coordinates to fit the ratio of the item frame before
# calculating the ellipse's ratio
x_scale = fig_node["size"]["x"] / fig_node["size"]["y"]
stroke = fig_node.get("strokeWeight", 0)
try:
x_scale = (fig_node["size"]["x"] + 2 * stroke) / (
fig_node["size"]["y"] + 2 * stroke
)
except:
x_scale = 1

ellipse_ratio = scaled_distance(
point_from, point_ellipse, x_scale
) / scaled_distance(point_from, point_to, x_scale)
Expand Down
1 change: 1 addition & 0 deletions sketchformat/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def from_fill(fill: Fill, position: BorderPosition, thickness: int) -> "Border":
contextSettings=fill.contextSettings,
position=position,
thickness=thickness,
isEnabled=fill.isEnabled,
)


Expand Down
43 changes: 43 additions & 0 deletions tests/converter/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def test_solid(self):
assert fill.color == SKETCH_COLOR[0]
assert fill.fillType == FillType.COLOR

def test_disabled(self):
fill = convert_fill(
{},
{"type": "SOLID", "color": FIG_COLOR[0], "visible": False, "opacity": 0.9},
)
assert fill.isEnabled == False

def test_image(self):
fill = convert_fill(
{},
Expand Down Expand Up @@ -131,6 +138,42 @@ def test_convert_border(self):
assert border.thickness == 5
assert border.position == BorderPosition.CENTER

def test_disabled_border(self):
border = convert_border(
{
"strokeAlign": "CENTER",
"strokeWeight": 5,
},
{"type": "SOLID", "color": FIG_COLOR[0], "visible": False, "opacity": 0.9},
)
assert border.isEnabled == False

def test_border_gradient(self):
border = convert_border(
{
"size": {"x": 50, "y": 0},
"strokeAlign": "CENTER",
"strokeWeight": 1,
},
{
"type": "GRADIENT_RADIAL",
"transform": Matrix([[1, 0, 0.5], [0, 1, 2]]),
"stops": [
{"color": FIG_COLOR[0], "position": 0},
{"color": FIG_COLOR[1], "position": 1},
],
"visible": True,
},
)
assert border.fillType == FillType.GRADIENT
assert border.isEnabled
assert border.gradient.gradientType == GradientType.RADIAL
assert border.gradient.to == Point(0.5, -1.5)
assert getattr(border.gradient, "from") == Point(0, -1.5)

# (width+2*stroke) / (height+2*stroke)
assert border.gradient.elipseLength == 2 / 52


class TestConvertContextSettings:
def test_blend(self):
Expand Down