Skip to content

Commit

Permalink
feat: Export-4BitSvg ( Fixes #34 )
Browse files Browse the repository at this point in the history
Also updating build
  • Loading branch information
James Brundage committed Feb 1, 2024
1 parent c9a563b commit bb8cb32
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
14 changes: 13 additions & 1 deletion 4bitcss.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@ $4bitJS = Export-4BitJS -ColorSchemeName $allColorSchemes -DarkColorSchemeName $
$4bitJSDocsPath = Join-Path $docsPath "js" | Join-Path -ChildPath "4bit.js"
New-Item -ItemType File -Path $4bitJSDocsPath -Force -Value $4bitJS

New-Item -ItemType File -Path ".\4bit.js" -Force -Value $4bitJS
New-Item -ItemType File -Path ".\4bit.js" -Force -Value $4bitJS


#region Icons
$IncludesPath = Join-Path $docsPath "_includes"
if (-not (Test-Path $IncludesPath)) {
$null = New-Item -ItemType Directory -Path $IncludesPath
}

Export-4BitSVG -SVG https://raw.githubusercontent.com/feathericons/feather/master/icons/download.svg -Stroke "ansi6" -OutputPath (Join-Path $IncludesPath "download-icon.svg")
Export-4BitSVG -SVG https://raw.githubusercontent.com/feathericons/feather/master/icons/download-cloud.svg -Stroke "ansi6" -OutputPath (Join-Path $IncludesPath "download-cloud-icon.svg")
Export-4BitSVG -SVG https://raw.githubusercontent.com/feathericons/feather/master/icons/shuffle.svg -Stroke "ansi6" -OutputPath (Join-Path $IncludesPath "shuffle-icon.svg")
#endregion Icons
82 changes: 82 additions & 0 deletions Export-4BitSVG.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function Export-4BitSVG
{
<#
.SYNOPSIS
Exports an SVG that uses a 4 bit palette
.DESCRIPTION
Exports an SVG with a slight modifications that make it use a specific color palette.
.NOTES
SVGs can use CSS styles to dynamically change color.
#>
param(
# The SVG content, or a URL to the SVG
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$SVG,

# The color used for strokes within the SVG.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$Stroke,

# The color used for fill within the SVG.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$Fill,

# If provided, will output the SVG to a file
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$OutputPath
)

process {
$svgXML = $null
if ($svg -match "^https?://") {
$svgRest = Invoke-RestMethod -Uri $svg
if ($svgRest -isnot [xml]) {
Write-Error "$Svg was not XML"
return
}
$svgXML = $svgRest
$svg = $svgXML.OuterXML
} elseif ($svg -notmatch '[\n\r]' -and (Test-path $svg)) {
$svg = Get-Content -Path $svg -Raw
}


if (-not ($svg -as [xml])) {
try {
$svgXML = [xml]$svg
} catch {
$ex = $_
Write-Error -Exception $ex.Exception -Message "Could not convert to SVG: $($ex.Exception)" -TargetObject $svg
}
}

if (-not $svgXML) { return }

$svgClass = @(
if ($stroke) {
$stroke -replace "(?:-stroke)?$", '-stroke'
}
if ($Fill) {
$fill -replace "(?:-fill)?$", '-fill'
}
) -join ' '

if ($svgClass) {
$svgXML.svg.SetAttribute("class", "$svgClass")
}

if ($OutputPath) {
$svgXML.svg.OuterXML | Set-Content -Path $OutputPath
if ($?) {
Get-Item -Path $OutputPath
}
} else {
$svgXML.svg.OuterXML
}

}
}

0 comments on commit bb8cb32

Please sign in to comment.