Skip to content

Commit 82068ae

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.get/set_Fill gradients ( Fixes PoshWeb#294 )
1 parent 5758d1b commit 82068ae

File tree

1 file changed

+127
-3
lines changed

1 file changed

+127
-3
lines changed

Turtle.types.ps1xml

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,16 +4629,140 @@ if (($this.'.Duration' -is [TimeSpan]) -and $this.PathAnimation) {
46294629
<ScriptProperty>
46304630
<Name>Fill</Name>
46314631
<GetScriptBlock>
4632-
if ($this.'.Fill') {
4632+
&lt;#
4633+
.SYNOPSIS
4634+
Gets a Turtle's fill color
4635+
.DESCRIPTION
4636+
Gets one or more colors used to fill the Turtle.
4637+
4638+
By default, this is transparent.
4639+
4640+
If more than one value is provided, the fill will be a gradient.
4641+
.EXAMPLE
4642+
# Draw a blue square
4643+
turtle square 42 fill blue
4644+
.EXAMPLE
4645+
# Draw a PowerShell blue square
4646+
turtle square 42 fill '#4488ff'
4647+
.EXAMPLE
4648+
# Draw a red, green, blue gradient
4649+
turtle square 42 fill red green blue show
4650+
.EXAMPLE
4651+
# Draw a red, green, blue linear gradient
4652+
turtle square 42 fill red green blue linear show
4653+
.EXAMPLE
4654+
turtle flower fill red green blue fillrule evenodd show
4655+
#&gt;
4656+
if ($this.'.Fill') {
46334657
return $this.'.Fill'
46344658
}
46354659
return 'transparent'
46364660
</GetScriptBlock>
46374661
<SetScriptBlock>
4638-
param(
4639-
[string]$Fill = 'transparent'
4662+
&lt;#
4663+
.SYNOPSIS
4664+
Sets a Turtle's fill color
4665+
.DESCRIPTION
4666+
Sets one or more colors used to fill the Turtle.
4667+
4668+
By default, this is transparent.
4669+
4670+
If more than one value is provided, the fill will be a gradient.
4671+
.EXAMPLE
4672+
# Draw a blue square
4673+
turtle square 42 fill blue
4674+
.EXAMPLE
4675+
# Draw a PowerShell blue square
4676+
turtle square 42 fill '#4488ff'
4677+
.EXAMPLE
4678+
# Draw a red, green, blue gradient
4679+
turtle square 42 fill red green blue show
4680+
.EXAMPLE
4681+
# Draw a red, green, blue linear gradient
4682+
turtle square 42 fill red green blue linear show
4683+
.EXAMPLE
4684+
turtle flower fill red green blue fillrule evenodd show
4685+
#&gt;
4686+
param(
4687+
[PSObject[]]
4688+
$Fill = 'transparent'
46404689
)
46414690

4691+
# If we have no fill information, return
4692+
if (-not $fill) { return }
4693+
4694+
# If the fill count is greater than one, try to make a graidnet
4695+
if ($fill.Count -gt 1) {
4696+
4697+
# Default to a radial gradient
4698+
$gradientTypeHint = 'radial'
4699+
# and create a collection for attributes
4700+
$gradientAttributes = [Ordered]@{
4701+
# default our identifier to the current id plus `fill-gradient`
4702+
# (so we could have multiple gradients without a collision)
4703+
id="$($this.id)-fill-gradient"
4704+
}
4705+
4706+
$fill = @(foreach ($color in $fill) {
4707+
# If the value matches `linear` or `radial`
4708+
if ($color -match '^(linear|radial)') {
4709+
# take the hint and make it the right type of gradient.
4710+
$gradientTypeHint = ($color -replace 'gradient').ToLower()
4711+
}
4712+
# If the color was `pad`, `reflect`, or `repeat`
4713+
elseif ($fillColor -in 'pad', 'reflect', 'repeat') {
4714+
# take the hint and set the spreadMethod
4715+
$gradientAttributes['spreadMethod'] = $color
4716+
}
4717+
# If the fill is a dictionary
4718+
elseif ($color -is [Collections.IDictionary]) {
4719+
# propagate the values into attributes.
4720+
foreach ($gradientAttributeKey in $color.Keys) {
4721+
$gradientAttributes[$gradientAttributeKey] = $color[$gradientAttributeKey]
4722+
}
4723+
}
4724+
# Otherwise output the color
4725+
else {
4726+
$color
4727+
}
4728+
})
4729+
4730+
# If we have no fill colors after filtering, return
4731+
if (-not $fill) { return }
4732+
4733+
# If our count is one
4734+
if ($fill.Count -eq 1) {
4735+
# it's not really going to be a gradient, so just use the one color.
4736+
$this | Add-Member -MemberType NoteProperty -Name '.Fill' -Value $Fill -Force
4737+
return
4738+
}
4739+
4740+
# Now we have at least two colors we want to be a gradient
4741+
# We need to make sure the offset starts at 0% an ends at 100%
4742+
# and so we actually need to divide by one less than our fill color, so we end at 100%.
4743+
$offsetStep = 1 / ($fill.Count - 1)
4744+
$Gradient = @(
4745+
# Construct our gradient element.
4746+
"&lt;${gradientTypeHint}Gradient$(
4747+
# propagate our attributes
4748+
@(foreach ($gradientAttributeKey in $gradientAttributes.Keys) {
4749+
" $gradientAttributeKey='$($gradientAttributes[$gradientAttributeKey])'"
4750+
}) -join ''
4751+
)&gt;"
4752+
@(
4753+
# and put in our stop colors
4754+
for ($fillNumber = 0; $fillNumber -lt $fill.Count; $fillNumber++) {
4755+
"&lt;stop offset='$($offsetStep * $fillNumber * 100)%' stop-color='$($fill[$fillNumber])' /&gt;"
4756+
}
4757+
)
4758+
"&lt;/${gradientTypeHint}Gradient&gt;"
4759+
) -join [Environment]::NewLine
4760+
4761+
# add this gradient to our defines
4762+
$this.Defines += $Gradient
4763+
# and set fill to this gradient.
4764+
$fill = "url(`"#$($gradientAttributes.id)`")"
4765+
}
46424766
if (-not $this.'.Fill') {
46434767
$this | Add-Member -MemberType NoteProperty -Name '.Fill' -Value $Fill -Force
46444768
} else {

0 commit comments

Comments
 (0)