Skip to content

Commit fc53197

Browse files
feat: Prebulding palette files ( Fixes #130 )
1 parent f4e57ea commit fc53197

File tree

2 files changed

+177
-2
lines changed

2 files changed

+177
-2
lines changed

4bitcss.com/buildPage.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ if (-not $site.PagesByUrl) {
2828
}
2929
$pagesByUrl = $site.PagesByUrl
3030

31-
$site.FilesProcessed = $filesProcessed = [Ordered]@{}
31+
if ($site.FilesProcessed -isnot [Collections.IDictionary]) {
32+
$site.FilesProcessed = $filesProcessed = [Ordered]@{}
33+
} else {
34+
$filesProcessed = $site.FilesProcessed
35+
}
36+
3237
$site.FileQueue = $fileQueue = [Collections.Queue]::new()
3338
foreach ($file in $allFiles) { $fileQueue.Enqueue($file) }
3439

4bitcss.com/config.ps1

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,174 @@ $site.HighlightJS = [Ordered]@{Languages=@('powershell')}
148148
$site.AnalyticsID = 'G-27ME7M0HYR' # replace with your Google Analytics ID
149149
#endregion Google Analytics
150150

151-
if ($PSScriptRoot) { Pop-Location }
151+
152+
153+
#region Custom
154+
$site.FilesProcessed = $filesProcessed = [Ordered]@{}
155+
$cssOutputRoot = Join-Path $PSScriptRoot css
156+
if (-not (Test-Path $cssOutputRoot)) {
157+
$null = New-Item -ItemType Directory -Path $cssOutputRoot
158+
}
159+
160+
$allPalettes = ./Palettes.json.ps1
161+
if (-not $site) {
162+
$site = [Ordered]@{}
163+
}
164+
$site.Palettes = $allPalettes
165+
166+
167+
Import-Module ../4bitcss.psd1 -Global
168+
169+
$allFiles = @()
170+
171+
$allFiles += foreach ($paletteKeyValue in $allPalettes.GetEnumerator()) {
172+
$paletteName = $paletteKeyValue.Key
173+
$paletteObject = $paletteKeyValue.Value
174+
$paletteObject | Export-4BitCSS -OutputPath $cssOutputRoot
175+
}
176+
177+
$colorOrder =
178+
'Black','Red','Green','Yellow','Blue','Purple','Cyan','White',
179+
'BrightBlack','BrightRed','BrightGreen','BrightYellow','BrightBlue','BrightPurple','BrightCyan','BrightWhite'
180+
181+
182+
$previewRectangleWidth = 640
183+
$previewRectangleHeight = 240
184+
185+
filter PalettePreviewRectangle {
186+
$palette = $_
187+
$columnWidth = $previewRectangleWidth / ($colorOrder.Count / 2)
188+
$rowHeight = $previewRectangleHeight / 3
189+
$paletteRects = @(for ($index = 0; $index -lt $colorOrder.Count; $index++) {
190+
$row = [Math]::Floor($index / ($colorOrder.Count / 2))
191+
$column = $index % ($colorOrder.Count / 2)
192+
$colorValue = $palette.($colorOrder[$index])
193+
"<rect x='$($column * $columnWidth)' y='$($row * $rowHeight)' width='$($columnWidth)' height='$($rowHeight)' fill='$($colorValue)' class='$($colorOrder[$index])-fill' />"
194+
}) -join [Environment]::NewLine
195+
@("<svg viewBox='0 0 $previewRectangleWidth $previewRectangleHeight' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://www.w3.org/2000/svg'>"
196+
"<defs>"
197+
"</defs>"
198+
"<rect width='$previewRectangleWidth' height='$previewRectangleHeight' x='0' y='0' fill='$($palette.background)' />"
199+
"<text height='33%' x='50%' y='$(5 * 100/6)%' fill='$($palette.foreground)' text-anchor='middle' alignment-baseline='middle'>$([Security.Securityelement]::Escape($palette.Name))</text>"
200+
$paletteRects
201+
"</svg>") -join '' -as [xml]
202+
}
203+
204+
205+
filter paletteToAnsi {
206+
$palette = $_
207+
$colorList = @(foreach ($color in $colorOrder) {
208+
$hexColor = $palette.$color
209+
$hexString = $hexColor -replace '[#;]'
210+
@(for ($hexIndex = 0; $hexIndex -lt 6; $hexIndex+=2) {
211+
$hexString[$hexIndex..($hexIndex + 1)] -join ''
212+
}) -join '/'
213+
})
214+
215+
$e = [char]27
216+
$i = 0
217+
@(
218+
foreach ($color in $colorList) {
219+
"$e]4;$i;rgb:$color$e\"
220+
$i++
221+
}
222+
$hexColor = $palette.foreground -replace ';'
223+
$rgb = ($hexColor -replace '#', '0x') -as [int]
224+
225+
$r = [byte](($rgb -band 0xff0000) -shr 16)
226+
$g = [byte](($rgb -band 0x00ff00) -shr 8)
227+
$b = [byte]($rgb -band 0x0000ff)
228+
"$e[38;2;$r;$g;${b}m"
229+
$hexColor = $palette.background -replace ';'
230+
$rgb = ($hexColor -replace '#', '0x') -as [int]
231+
232+
$r = [byte](($rgb -band 0xff0000) -shr 16)
233+
$g = [byte](($rgb -band 0x00ff00) -shr 8)
234+
$b = [byte]($rgb -band 0x0000ff)
235+
"$e[48;2;$r;$g;${b}m"
236+
) -join ''
237+
}
238+
239+
$allFiles += foreach ($paletteKeyValue in $allPalettes.GetEnumerator()) {
240+
$paletteName = $paletteKeyValue.Key
241+
$paletteObject = $paletteKeyValue.Value
242+
$paletteRoot = Join-Path $PSScriptRoot $paletteName
243+
if (-not (Test-Path $paletteRoot)) {
244+
$null = New-Item -ItemType Directory -Path $paletteRoot
245+
}
246+
$paletteObject | Export-4BitCSS -OutputPath $paletteRoot
247+
$paletteJsonPath = Join-Path $paletteRoot "$paletteName.json"
248+
$paletteObject |
249+
ConvertTo-Json -Depth 4 |
250+
Set-Content -LiteralPath $paletteJsonPath
251+
Get-Item -LiteralPath $paletteJsonPath
252+
253+
$paletteTextPath = Join-Path $paletteRoot "$paletteName.txt"
254+
255+
$distinctColors = @($paletteObject.psobject.Properties.value) -match '^#[0-9a-fA-F]{6}' | Select-Object -Unique
256+
$distinctColors -join ';' | Set-Content -Path $paletteTextPath -Encoding utf8
257+
258+
Get-Item -LiteralPath $paletteTextPath
259+
260+
$ansiTextPath = Join-Path $paletteRoot "$paletteName.ansi.txt"
261+
$paletteObject | paletteToAnsi | Set-Content $ansiTextPath
262+
Get-Item -LiteralPath $ansiTextPath
263+
264+
265+
$palettePreviewSVG = $paletteObject | PalettePreviewRectangle
266+
$palettePreviewPath = Join-Path $paletteRoot "$paletteName.svg"
267+
$palettePreviewSVG.Save("$palettePreviewPath")
268+
Get-Item -LiteralPath $palettePreviewPath
269+
270+
$indexHtmlPs1 = ". `$site.views.palette '$($paletteName -replace "'","''")'"
271+
272+
$paletteIndexHtmlPs1 = Join-Path $paletteRoot "$paletteName.html.ps1"
273+
$indexHtmlPs1 > $paletteIndexHtmlPs1
274+
Get-Item -LiteralPath $paletteIndexHtmlPs1
275+
}
276+
277+
$site.Palettes = $allPalettes
278+
if ($page -isnot [Collections.IDictionary]) {
279+
$page = [Ordered]@{}
280+
}
281+
282+
if ($site.FilesProcessed) {
283+
foreach ($file in $allFiles) {
284+
$site.FilesProcessed[$file.FullName] = $true
285+
}
286+
}
287+
288+
$htmlPs1Files = $allFiles -match '\.html\.ps1$'
289+
290+
$layout = Get-Command ./layout.ps1
291+
292+
$htmlFiles = foreach ($htmlPs1 in $htmlPs1Files) {
293+
$paletteName = $htmlPs1.Directory.Name
294+
$layoutSplat = [Ordered]@{}
295+
if ($layout.Parameters['Title']) {
296+
$layoutSplat['Title'] = $paletteName
297+
}
298+
if ($layout.Parameters['Description']) {
299+
$layoutSplat['Description'] =
300+
"$($htmlPs1.Directory.Name) color palette. $(
301+
if ($allPalettes[$paletteName].credits) {
302+
$allPalettes[$paletteName].credits
303+
}
304+
)"
305+
}
306+
$outputFile = $htmlPs1.FullName -replace "$(
307+
[Regex]::Escape($paletteName)
308+
)\.html\.ps1$",'index.html'
309+
$output = . $htmlPs1.FullName | . $layout @layoutSplat
310+
$output > $outputFile
311+
Get-Item -LiteralPath $outputFile
312+
}
313+
314+
if ($site.FilesProcessed) {
315+
foreach ($htmlFile in $htmlFiles) {
316+
$site.FilesProcessed[$htmlFile.FullName] = $true
317+
}
318+
}
319+
#endregion Custom
320+
321+
if ($PSScriptRoot) { Pop-Location }

0 commit comments

Comments
 (0)