Skip to content

Commit fd790c1

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Gradient.SVG ( Fixes #5 )
1 parent bedb2c5 commit fd790c1

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Gradient.types.ps1xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,60 @@ return $foundTypes
9191
'(?:repeating-)?(?>conic|linear|radial)(?:-gradient)?$'
9292
</GetScriptBlock>
9393
</ScriptProperty>
94+
<ScriptProperty>
95+
<Name>SVG</Name>
96+
<GetScriptBlock>
97+
&lt;#
98+
.SYNOPSIS
99+
Gets a SVG gradient
100+
.DESCRIPTION
101+
Gets the gradient as SVG.
102+
103+
SVG Gradients are a bit more picky than their CSS counterpart,
104+
and do not support conic gradients.
105+
106+
#&gt;
107+
$gradientTypeHint = $this.GradientType
108+
if (-not $gradientTypeHint) {
109+
$gradientTypeHint = 'radial'
110+
}
111+
if ($gradientTypeHint) {
112+
$gradientTypeHint = $gradientTypeHint -replace '-gradient', 'Gradient'
113+
$gradientTypeHint = $gradientTypeHint.Substring(0,1).ToLower() + $gradientTypeHint.Substring(1)
114+
}
115+
# AFAIK, SVG does not support conic gradients.
116+
if ($gradientTypeHint -match 'conic') { return }
117+
$gradientValues = @(foreach ($in in $this.input) {
118+
if ($in -match '\#[a-f0-9]{6}') {
119+
$matches.0
120+
}
121+
})
122+
if (-not $gradientValues) { return }
123+
if ($gradientValues.Count -eq 1) {
124+
$gradientValues = @($gradientValues) * 2
125+
}
126+
127+
# Now we have at least two colors we want to be a gradient
128+
# We need to make sure the offset starts at 0% an ends at 100%
129+
# and so we actually need to divide by one less than our fill color, so we end at 100%.
130+
$offsetStep = 1 / ($gradientValues.Count - 1)
131+
132+
@(
133+
# Construct our gradient element.
134+
"&lt;${gradientTypeHint}$(
135+
# propagate our attributes
136+
" id='${gradientTypeHint}-$($gradientValues -replace '#' -join '-')'"
137+
)&gt;"
138+
@(
139+
# and put in our stop colors
140+
for ($StopNumber = 0; $StopNumber -lt $gradientValues.Count; $StopNumber++) {
141+
"&lt;stop offset='$($offsetStep * $StopNumber * 100)%' stop-color='$($gradientValues[$StopNumber])' /&gt;"
142+
}
143+
)
144+
"&lt;/${gradientTypeHint}&gt;"
145+
) -join [Environment]::NewLine
146+
</GetScriptBlock>
147+
</ScriptProperty>
94148
</Members>
95149
</Type>
96150
</Types>

0 commit comments

Comments
 (0)