-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradient.types.ps1xml
More file actions
256 lines (237 loc) · 6.94 KB
/
Gradient.types.ps1xml
File metadata and controls
256 lines (237 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!-- Generated with EZOut 2.0.6: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Types>
<Type>
<Name>Gradient</Name>
<Members>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<PropertySet>
<Name>DefaultDisplayPropertySet</Name>
<ReferencedProperties>
<Name>Input</Name>
<Name>CSS</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</MemberSet>
<AliasProperty>
<Name>GradientTypes</Name>
<ReferencedMemberName>GradientType</ReferencedMemberName>
</AliasProperty>
<ScriptMethod>
<Name>ToString</Name>
<Script>
<#
.SYNOPSIS
Stringifies the gradient
.DESCRIPTION
Gets the gradient as a string. By default, returns the CSS gradient.
If any arguments are passed and are properties of this object,
they will be returned as strings.
.EXAMPLE
"$(gradient '#4488ff' '#224488')"
.EXAMPLE
(gradient '#4488ff' '#224488').ToString("html")
#>
param()
if ($args) {
$anymatching = @(foreach ($arg in $args) {
$thisArg = $this.$arg
if ($thisArg) {
if ($thisArg -is [xml]) {
$thisArg.Outerxml
} else {
$thisArg
}
}
})
if ($anymatching) {
return ($anymatching -as 'string[]' -join [Environment]::NewLine)
}
}
return "$($this.CSS)"
</Script>
</ScriptMethod>
<ScriptProperty>
<Name>CSS</Name>
<GetScriptBlock>
<#
.SYNOPSIS
Gets Gradient CSS
.DESCRIPTION
Gets the Gradient as CSS.
.EXAMPLE
gradient '#4488ff', '#224488' |
Select-Object -Expand CSS
#>
param()
# Get our gradient type
$gradientTypes = $this.GradientTypes
$gradientStack = @()
$gradientValues = @(foreach ($in in $this.input) {
if ($in.pstypenames -eq 'gradient') {
$gradientStack += $in
continue
}
if ($in -notmatch $this.GradientTypePattern) {
$in
}
})
$ShallowJoiner = (', ' + [Environment]::NewLine + (' ' * 2))
if ($gradientValues) {
if (-not $gradientTypes) { $gradientTypes = 'radial-gradient'}
$gradientCss = @(foreach ($gradientType in $gradientTypes) {
"$gradientType($(
[Environment]::NewLine + (' ' * 2) +
$(
@(
$gradientValues
) -join $ShallowJoiner
)
))"
}) -join ', '
}
if ($gradientStack) {
$deepJoiner = (', ' + [Environment]::NewLine)
if (-not $gradientValues) {
$gradientStack -join $deepJoiner
} else {
@($gradientStack;$gradientCss) -join $deepJoiner
}
} elseif ($gradientValues) {
$gradientCss
}
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>GradientType</Name>
<GetScriptBlock>
<#
.SYNOPSIS
Gets the Gradient Type
.DESCRIPTION
Gets the Gradient Types found in the Gradient's input.
If no type is found, radial-gradient is the default.
#>
$foundTypes = @(foreach ($in in $this.input) {
if ($in -match $this.GradientTypePattern) {
$in -replace '(?:-gradient)?$', '-gradient'
}
})
if (-not $foundTypes) {
$foundTypes = @('radial-gradient')
}
return $foundTypes
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>GradientTypePattern</Name>
<GetScriptBlock>
<#
.SYNOPSIS
Gets the gradient type pattern
.DESCRIPTION
Gets the regular expression used to identify a gradient type
.EXAMPLE
Get-Gradiant '#4488ff' ,'#224488' |
Select-Object -Expand GradientTypePattern
#>
'(?:repeating-)?(?>conic|linear|radial)(?:-gradient)?$'
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>HTML</Name>
<GetScriptBlock>
<#
.SYNOPSIS
Gets the gradient as HTML
.DESCRIPTION
Gets the gradient as a `<div>` element with a style attribute.
#>
$styleAttribute = [Web.HttpUtility]::HtmlAttributeEncode(@(
"width:100%"
"height:100%"
"background-image:$($this.CSS)"
) -join ';')
"<div style='$styleAttribute'></div>"
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>JSON</Name>
<GetScriptBlock>
<#
.SYNOPSIS
Gets a json gradient
.DESCRIPTION
Gets the gradient in JavaScript Object Notation (JSON)
This describes the gradient as a background-image with the css inline in a string.
This should work seamlessly with
[Element.animate](https://developer.mozilla.org/en-US/docs/Web/API/Element/animate)
to allow you to animate a gradient change.
.LINK
https://developer.mozilla.org/en-US/docs/Web/API/Element/animate
#>
@{
'background-image' = "$($this.CSS)"
} | ConvertTo-Json
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>SVG</Name>
<GetScriptBlock>
<#
.SYNOPSIS
Gets a SVG gradient
.DESCRIPTION
Gets the gradient as SVG.
SVG Gradients are a bit more picky than their CSS counterpart,
and do not support conic gradients.
#>
$gradientTypeHint = $this.GradientType
if (-not $gradientTypeHint) {
$gradientTypeHint = 'radial'
}
if ($gradientTypeHint) {
$gradientTypeHint = $gradientTypeHint -replace '-gradient', 'Gradient'
$gradientTypeHint = $gradientTypeHint.Substring(0,1).ToLower() + $gradientTypeHint.Substring(1)
}
# AFAIK, SVG does not support conic gradients.
if ($gradientTypeHint -match 'conic') { return }
$gradientValues = @(foreach ($in in $this.input) {
if ($in -match '\#[a-f0-9]{6}') {
$matches.0
}
})
if (-not $gradientValues) { return }
if ($gradientValues.Count -eq 1) {
$gradientValues = @($gradientValues) * 2
}
# Now we have at least two colors we want to be a gradient
# We need to make sure the offset starts at 0% an ends at 100%
# and so we actually need to divide by one less than our fill color, so we end at 100%.
$offsetStep = 1 / ($gradientValues.Count - 1)
@(
# Construct our gradient element.
"<${gradientTypeHint}$(
# propagate our attributes
" id='${gradientTypeHint}-$($gradientValues -replace '#' -join '-')'"
)>"
@(
# and put in our stop colors
for ($StopNumber = 0; $StopNumber -lt $gradientValues.Count; $StopNumber++) {
"<stop offset='$($offsetStep * $StopNumber * 100)%' stop-color='$($gradientValues[$StopNumber])' />"
}
)
"</${gradientTypeHint}>"
) -join [Environment]::NewLine
</GetScriptBlock>
</ScriptProperty>
<NoteProperty>
<Name>DefaultDisplay</Name>
<Value>Input
CSS</Value>
</NoteProperty>
</Members>
</Type>
</Types>