Skip to content

Commit b4fcd18

Browse files
feat: Turtle.get/set_Stroke gradients ( Fixes #295 )
1 parent 82068ae commit b4fcd18

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

Types/Turtle/get_Stroke.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
<#
2+
.SYNOPSIS
3+
Gets a Turtle's stroke color
4+
.DESCRIPTION
5+
Gets one or more colors used to stroke the Turtle.
6+
7+
By default, this is transparent.
8+
9+
If more than one value is provided, the stroke will be a gradient.
10+
.EXAMPLE
11+
# Draw a blue square
12+
turtle square 42 stroke blue
13+
.EXAMPLE
14+
# Draw a PowerShell blue square
15+
turtle square 42 stroke '#4488ff'
16+
.EXAMPLE
17+
# Draw a red, green, blue gradient
18+
turtle square 42 stroke red green blue show
19+
.EXAMPLE
20+
# Draw a red, green, blue linear gradient
21+
turtle square 42 stroke red green blue linear show
22+
.EXAMPLE
23+
turtle flower stroke red green blue strokerule evenodd show
24+
#>
125
if ($this.'.Stroke') {
226
return $this.'.Stroke'
327
} else {

Types/Turtle/set_Stroke.ps1

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,109 @@
1-
param([string]$value)
1+
<#
2+
.SYNOPSIS
3+
Sets a Turtle's stroke color
4+
.DESCRIPTION
5+
Sets one or more colors used to stroke the Turtle.
26
3-
$this | Add-Member -MemberType NoteProperty -Force -Name '.Stroke' -Value $value
7+
By default, this is transparent.
8+
9+
If more than one value is provided, the stroke will be a gradient.
10+
.EXAMPLE
11+
# Draw a blue square
12+
turtle square 42 stroke blue
13+
.EXAMPLE
14+
# Draw a PowerShell blue square
15+
turtle square 42 stroke '#4488ff'
16+
.EXAMPLE
17+
# Draw a red, green, blue gradient
18+
turtle square 42 stroke red green blue show
19+
.EXAMPLE
20+
# Draw a red, green, blue linear gradient
21+
turtle square 42 stroke red green blue linear show
22+
.EXAMPLE
23+
turtle flower stroke red green blue strokerule evenodd show
24+
#>
25+
param(
26+
[PSObject[]]
27+
$stroke = 'transparent'
28+
)
29+
30+
# If we have no stroke information, return
31+
if (-not $stroke) { return }
32+
33+
# If the stroke count is greater than one, try to make a graidnet
34+
if ($stroke.Count -gt 1) {
35+
36+
# Default to a radial gradient
37+
$gradientTypeHint = 'radial'
38+
# and create a collection for attributes
39+
$gradientAttributes = [Ordered]@{
40+
# default our identifier to the current id plus `stroke-gradient`
41+
# (so we could have multiple gradients without a collision)
42+
id="$($this.id)-stroke-gradient"
43+
}
44+
45+
$stroke = @(foreach ($color in $stroke) {
46+
# If the value matches `linear` or `radial`
47+
if ($color -match '^(linear|radial)') {
48+
# take the hint and make it the right type of gradient.
49+
$gradientTypeHint = ($color -replace 'gradient').ToLower()
50+
}
51+
# If the color was `pad`, `reflect`, or `repeat`
52+
elseif ($strokeColor -in 'pad', 'reflect', 'repeat') {
53+
# take the hint and set the spreadMethod
54+
$gradientAttributes['spreadMethod'] = $color
55+
}
56+
# If the stroke is a dictionary
57+
elseif ($color -is [Collections.IDictionary]) {
58+
# propagate the values into attributes.
59+
foreach ($gradientAttributeKey in $color.Keys) {
60+
$gradientAttributes[$gradientAttributeKey] = $color[$gradientAttributeKey]
61+
}
62+
}
63+
# Otherwise output the color
64+
else {
65+
$color
66+
}
67+
})
68+
69+
# If we have no stroke colors after filtering, return
70+
if (-not $stroke) { return }
71+
72+
# If our count is one
73+
if ($stroke.Count -eq 1) {
74+
# it's not really going to be a gradient, so just use the one color.
75+
$this | Add-Member -MemberType NoteProperty -Name '.Stroke' -Value $stroke -Force
76+
return
77+
}
78+
79+
# Now we have at least two colors we want to be a gradient
80+
# We need to make sure the offset starts at 0% an ends at 100%
81+
# and so we actually need to divide by one less than our stroke color, so we end at 100%.
82+
$offsetStep = 1 / ($stroke.Count - 1)
83+
$Gradient = @(
84+
# Construct our gradient element.
85+
"<${gradientTypeHint}Gradient$(
86+
# propagate our attributes
87+
@(foreach ($gradientAttributeKey in $gradientAttributes.Keys) {
88+
" $gradientAttributeKey='$($gradientAttributes[$gradientAttributeKey])'"
89+
}) -join ''
90+
)>"
91+
@(
92+
# and put in our stop colors
93+
for ($strokeNumber = 0; $strokeNumber -lt $stroke.Count; $strokeNumber++) {
94+
"<stop offset='$($offsetStep * $strokeNumber * 100)%' stop-color='$($stroke[$strokeNumber])' />"
95+
}
96+
)
97+
"</${gradientTypeHint}Gradient>"
98+
) -join [Environment]::NewLine
99+
100+
# add this gradient to our defines
101+
$this.Defines += $Gradient
102+
# and set stroke to this gradient.
103+
$stroke = "url(`"#$($gradientAttributes.id)`")"
104+
}
105+
if (-not $this.'.stroke') {
106+
$this | Add-Member -MemberType NoteProperty -Name '.Stroke' -Value $stroke -Force
107+
} else {
108+
$this.'.stroke' = $stroke
109+
}

0 commit comments

Comments
 (0)