Skip to content

Commit 536c302

Browse files
feat: Palettes.json.ps1 ( Fixes #118 )
1 parent 21e94b9 commit 536c302

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

4bitcss.com/Palettes.json.ps1

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
if ($PSScriptRoot) { Push-Location $PSScriptRoot }
2+
$sparseClone =
3+
git.sparse https://github.com/mbadolato/iTerm2-Color-Schemes.git ../iTerm2-Color-Schemes -Pattern @(
4+
'/windowsterminal/**/**.json'
5+
'/CREDITS.md'
6+
)
7+
8+
$creditLines = Get-Content ../iTerm2-Color-Schemes/CREDITS.md
9+
10+
filter GetCredits {
11+
$colorSchemeName = $_
12+
$colorSchemePattern = [Regex]::Escape($colorSchemeName) -replace '\\ ', '[\s_]'
13+
$markdownLinkPattern = '\[(?<text>.+?)\]\((?<link>.+?)\)'
14+
foreach ($line in $creditLines) {
15+
if (-not $line ) { continue }
16+
if ($line -notmatch $colorSchemePattern) {
17+
continue
18+
}
19+
if ($line -notmatch $markdownLinkPattern) {
20+
continue
21+
}
22+
[PSCustomObject][Ordered]@{credit=$Matches.text; link=$Matches.link}
23+
}
24+
}
25+
26+
$jsonOutputRoot = Join-Path $PSScriptRoot json
27+
if (-not (Test-Path $jsonOutputRoot)) {
28+
$null = New-Item -ItemType Directory -Path $jsonOutputRoot
29+
}
30+
31+
filter GetLuma {
32+
$colorString = $_
33+
# Convert the background color to a uint32
34+
$rgb = ($colorString -replace "#", "0x" -replace ';') -as [UInt32]
35+
# then make it into a percentage red, green, and blue.
36+
$r, $g, $b = ([float][byte](($rgb -band 0xff0000) -shr 16)/255),
37+
([float][byte](($rgb -band 0x00ff00) -shr 8)/255),
38+
([float][byte]($rgb -band 0x0000ff)/255)
39+
40+
# Calculate the luma of the background color
41+
0.2126 * $R + 0.7152 * $G + 0.0722 * $B
42+
}
43+
44+
filter GetHSL {
45+
$colorString = $_
46+
# Convert the background color to a uint32
47+
$rgb = ($colorString -replace "#", "0x" -replace ';') -as [UInt32]
48+
# then make it into a percentage red, green, and blue.
49+
$r, $g, $b = ([float][byte](($rgb -band 0xff0000) -shr 16)/255),
50+
([float][byte](($rgb -band 0x00ff00) -shr 8)/255),
51+
([float][byte]($rgb -band 0x0000ff)/255)
52+
53+
[float]$PercentRed = $R
54+
[float]$PercentGreen = $G
55+
[float]$PercentBlue = $B
56+
57+
$min = $max = $PercentRed
58+
foreach ($_ in $PercentGreen, $PercentBlue) {
59+
if ($_ -lt $min) { $min = $_ }
60+
if ($_ -ge $max) { $max = $_ }
61+
}
62+
63+
$Luminance = ($min + $max) / 2
64+
$delta = $max - $min
65+
66+
if (-not $delta) {
67+
$hue = $saturation = 0
68+
} else {
69+
$saturation = $delta
70+
$saturation /= (1 - [Math]::Abs(((2 * $Luminance) -1)))
71+
72+
$hue =
73+
if ($Max -eq $PercentRed){
74+
($PercentGreen - $PercentBlue)/$delta % 6
75+
} elseif ($max -eq $PercentGreen) {
76+
(($PercentBlue - $PercentRed)/ $delta) + 2
77+
} else {
78+
(($PercentRed - $PercentGreen)/ $delta) + 4
79+
}
80+
}
81+
$hue*=60
82+
if ($hue -gt 360) { $hue -= 360 }
83+
if ($hue -lt 0) { $hue = 360 + $hue }
84+
[PSCustomObject][Ordered]@{
85+
PSTypeName = 'Color'
86+
R = $R
87+
G = $G
88+
B = $B
89+
RGB = $colorString
90+
Hue = $hue
91+
Saturation = $saturation
92+
Luminance = $Luminance
93+
}
94+
}
95+
96+
$paletteJsonFiles = $sparseClone | Where-Object Extension -eq '.json'
97+
$allPalettes = [Ordered]@{}
98+
99+
$createdFiles = foreach ($paletteJsonFile in $paletteJsonFiles) {
100+
$paletteContent = [IO.File]::ReadAllText($paletteJsonFile.FullName)
101+
$paletteObject = $paletteContent | ConvertFrom-Json
102+
$paletteObject.pstypenames.insert(0, 'Palette')
103+
# and determine the name of the scheme and it's files.
104+
$paletteName = $colorSchemeName = $paletteObject.Name
105+
$colorSchemeFileName =
106+
$paletteObject.Name | Convert-4BitName
107+
108+
$creditInfo = @($colorSchemeName | GetCredits)[0]
109+
$paletteObject |
110+
Add-Member NoteProperty creditTo -Force -PassThru -Value $creditInfo.credit |
111+
Add-Member NoteProperty creditToLink -Force -Value $creditInfo.link
112+
113+
if ($paletteObject.background) {
114+
$paletteObject |
115+
Add-Member NoteProperty luma -Force -Value $($paletteObject.Background | GetLuma)
116+
}
117+
118+
if ($paletteObject.background) {
119+
$paletteObject |
120+
Add-Member NoteProperty hue -Force -Value $($paletteObject.Background | GetHSL | Select-Object -ExpandProperty Hue)
121+
}
122+
123+
$IsBright = $paletteObject.luma -ge .4
124+
125+
$paletteObject | Add-Member NoteProperty IsBright $IsBright -Force
126+
$paletteObject | Add-Member NoteProperty IsDark (-not $IsBright) -Force
127+
128+
if ($paletteObject.foreground) {
129+
$paletteObject |
130+
Add-Member NoteProperty foregroundLuma -Force -Value $($paletteObject.foreground | GetLuma)
131+
}
132+
133+
if ($paletteObject.background) {
134+
$paletteObject |
135+
Add-Member NoteProperty foregroundHue -Force -Value $($paletteObject.foreground | GetHSL | Select-Object -ExpandProperty Hue)
136+
}
137+
138+
if ($paletteObject.foreground -and $paletteObject.background) {
139+
$contrastLevel = [Math]::Abs(
140+
($paletteObject.background | GetLuma) - ($paletteObject.foreground | GetLuma)
141+
)
142+
$paletteObject |
143+
Add-Member NoteProperty contrast -Force -Value $contrastLevel
144+
}
145+
146+
if (-not $colorSchemeFileName) { continue }
147+
$paletteObject | Add-Member NoteProperty FileName $colorSchemeFileName -Force
148+
$jsonOutputPath = Join-Path $jsonOutputRoot "$colorSchemeFileName.json"
149+
$paletteObject | ConvertTo-Json -Depth 4 | Set-Content -Path $jsonOutputPath
150+
Get-Item $jsonOutputPath
151+
152+
153+
$allPalettes[$colorSchemeFileName] = $paletteObject
154+
}
155+
156+
if ($site.FilesProcessed) {
157+
foreach ($file in $createdFiles) {
158+
$site.FilesProcessed[$file.FullName] = $true
159+
}
160+
}
161+
162+
163+
# $allPalettes | ConvertTo-Json -Depth 10 | Set-Content -Path "./Palettes.json"
164+
# Get-Item -Path ./Palettes.json
165+
$allPalettes
166+
167+
if ($PSScriptRoot) { Pop-Location}

0 commit comments

Comments
 (0)