reconfigure targa behavior

This commit is contained in:
Nathan
2026-01-09 16:17:10 -07:00
parent abc668e1d3
commit 1c498e2ff4
2 changed files with 5184 additions and 41 deletions

View File

@@ -270,20 +270,38 @@ function Process-DuplicateGroup {
}
# Function to extract and normalize hex color code from filename (for FlatColors)
# For TGA files, preserves 8-digit codes (RGBA). For others, uses 6-digit codes (RGB).
function Get-NormalizedColorCode {
param([string]$FileName)
param(
[string]$FileName,
[string]$Extension = ""
)
# Remove extension
# Remove extension if not provided
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($FileName)
if ([string]::IsNullOrWhiteSpace($Extension)) {
$Extension = [System.IO.Path]::GetExtension($FileName)
}
# Match hex color pattern: #RRGGBB or #RRGGBBAA (with optional suffix like _1, _2, etc.)
# Pattern matches: #RRGGBB, #RRGGBBAA, or just RRGGBB (without #)
# Also handles suffixes like _1, _2, etc.
if ($baseName -match '^(#?)([0-9A-Fa-f]{6})([0-9A-Fa-f]{2})?') {
$hasHash = $Matches[1] -eq "#"
$hexCode = $Matches[2].ToUpper()
# Return normalized format: #RRGGBB (always with #, always uppercase, no suffix)
return "#$hexCode"
# For TGA files, preserve 8-digit codes (RGBA with alpha channel)
if ($Extension -eq ".tga") {
# Match 8-digit hex: #RRGGBBAA
if ($baseName -match '^(#?)([0-9A-Fa-f]{8})') {
$hexCode = $Matches[2].ToUpper()
return "#$hexCode"
}
# Fallback to 6-digit if 8-digit not found
if ($baseName -match '^(#?)([0-9A-Fa-f]{6})') {
$hexCode = $Matches[2].ToUpper()
return "#$hexCode"
}
} else {
# For non-TGA files, use 6-digit codes (RGB)
if ($baseName -match '^(#?)([0-9A-Fa-f]{6})([0-9A-Fa-f]{2})?') {
$hexCode = $Matches[2].ToUpper()
# Return normalized format: #RRGGBB (always with #, always uppercase, no suffix)
return "#$hexCode"
}
}
return $null
@@ -512,16 +530,20 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
continue
}
$colorCode = Get-NormalizedColorCode -FileName $file.Name
# Use composite key: color code + extension to separate TGA (8-digit) from JPG (6-digit)
$colorCode = Get-NormalizedColorCode -FileName $file.Name -Extension $file.Extension
if ($null -eq $colorCode) {
Write-Warning "Could not extract color code from: $($file.Name)"
continue
}
if (-not $filesByColor.ContainsKey($colorCode)) {
$filesByColor[$colorCode] = @()
# Create composite key: colorCode_extension to keep TGA and JPG separate
$groupKey = "$colorCode$($file.Extension)"
if (-not $filesByColor.ContainsKey($groupKey)) {
$filesByColor[$groupKey] = @()
}
$filesByColor[$colorCode] += $file
$filesByColor[$groupKey] += $file
}
Write-Host "Found $($filesByColor.Count) unique color code(s)" -ForegroundColor Gray
@@ -541,45 +563,55 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
$flatColorsResized = 0
$flatColorsMoveLog = [System.Collections.ArrayList]::new()
foreach ($colorCode in $filesByColor.Keys) {
$files = $filesByColor[$colorCode]
foreach ($groupKey in $filesByColor.Keys) {
$files = $filesByColor[$groupKey]
# Find the best file to keep (prefer existing JPG, then TGA, then others)
# Extract color code and extension from group key
# Group key format: #RRGGBB.ext or #RRGGBBAA.ext
if ($groupKey -match '^(#[0-9A-F]{6,8})(\.[^.]+)$') {
$colorCode = $Matches[1]
$targetExtension = $Matches[2]
} else {
Write-Warning "Invalid group key format: $groupKey"
continue
}
$targetFileName = "$colorCode$targetExtension"
# Find the best file to keep
$fileToKeep = $null
$targetExtension = ".jpg"
$targetFileName = "$colorCode.jpg"
# First, check if there's already a correct file in target location
$existingTarget = Join-Path -Path $flatColorsPath -ChildPath $targetFileName
if (Test-Path -Path $existingTarget) {
$existingFile = Get-Item -Path $existingTarget
# Check if it's already correct
if ($existingFile.Extension -eq ".jpg" -and (Test-ImageSize -ImagePath $existingFile.FullName)) {
if ($existingFile.Extension -eq ".tga") {
$fileToKeep = $existingFile
} elseif ($existingFile.Extension -eq ".jpg" -and (Test-ImageSize -ImagePath $existingFile.FullName)) {
$fileToKeep = $existingFile
}
}
# If no existing correct file, find best source file
if ($null -eq $fileToKeep) {
# Prefer existing JPG file
$jpgFile = $files | Where-Object {
($_.Extension -eq ".jpg" -or $_.Extension -eq ".jpeg") -and
# Prefer TGA file first (preserves transparency)
$tgaFile = $files | Where-Object {
$_.Extension -eq ".tga" -and
(Test-Path -Path $_.FullName)
} | Select-Object -First 1
if ($jpgFile) {
$fileToKeep = $jpgFile
if ($tgaFile) {
$fileToKeep = $tgaFile
} else {
# If no JPG, prefer TGA
$tgaFile = $files | Where-Object {
$_.Extension -eq ".tga" -and
# If no TGA, prefer existing JPG file
$jpgFile = $files | Where-Object {
($_.Extension -eq ".jpg" -or $_.Extension -eq ".jpeg") -and
(Test-Path -Path $_.FullName)
} | Select-Object -First 1
if ($tgaFile) {
$fileToKeep = $tgaFile
$targetExtension = ".tga"
$targetFileName = "$colorCode.tga"
if ($jpgFile) {
$fileToKeep = $jpgFile
} else {
# Otherwise, use the first available file
$fileToKeep = $files | Where-Object { Test-Path -Path $_.FullName } | Select-Object -First 1