Skip to content

Commit bedb2c5

Browse files
feat: Gradient.SVG ( Fixes #5 )
1 parent 44370e3 commit bedb2c5

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Types/Gradient/get_SVG.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<#
2+
.SYNOPSIS
3+
Gets a SVG gradient
4+
.DESCRIPTION
5+
Gets the gradient as SVG.
6+
7+
SVG Gradients are a bit more picky than their CSS counterpart,
8+
and do not support conic gradients.
9+
10+
#>
11+
$gradientTypeHint = $this.GradientType
12+
if (-not $gradientTypeHint) {
13+
$gradientTypeHint = 'radial'
14+
}
15+
if ($gradientTypeHint) {
16+
$gradientTypeHint = $gradientTypeHint -replace '-gradient', 'Gradient'
17+
$gradientTypeHint = $gradientTypeHint.Substring(0,1).ToLower() + $gradientTypeHint.Substring(1)
18+
}
19+
# AFAIK, SVG does not support conic gradients.
20+
if ($gradientTypeHint -match 'conic') { return }
21+
$gradientValues = @(foreach ($in in $this.input) {
22+
if ($in -match '\#[a-f0-9]{6}') {
23+
$matches.0
24+
}
25+
})
26+
if (-not $gradientValues) { return }
27+
if ($gradientValues.Count -eq 1) {
28+
$gradientValues = @($gradientValues) * 2
29+
}
30+
31+
# Now we have at least two colors we want to be a gradient
32+
# We need to make sure the offset starts at 0% an ends at 100%
33+
# and so we actually need to divide by one less than our fill color, so we end at 100%.
34+
$offsetStep = 1 / ($gradientValues.Count - 1)
35+
36+
@(
37+
# Construct our gradient element.
38+
"<${gradientTypeHint}$(
39+
# propagate our attributes
40+
" id='${gradientTypeHint}-$($gradientValues -replace '#' -join '-')'"
41+
)>"
42+
@(
43+
# and put in our stop colors
44+
for ($StopNumber = 0; $StopNumber -lt $gradientValues.Count; $StopNumber++) {
45+
"<stop offset='$($offsetStep * $StopNumber * 100)%' stop-color='$($gradientValues[$StopNumber])' />"
46+
}
47+
)
48+
"</${gradientTypeHint}>"
49+
) -join [Environment]::NewLine

0 commit comments

Comments
 (0)