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) # 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 { function Get-NormalizedColorCode {
param([string]$FileName) param(
[string]$FileName,
[string]$Extension = ""
)
# Remove extension # Remove extension if not provided
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($FileName) $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.) # For TGA files, preserve 8-digit codes (RGBA with alpha channel)
# Pattern matches: #RRGGBB, #RRGGBBAA, or just RRGGBB (without #) if ($Extension -eq ".tga") {
# Also handles suffixes like _1, _2, etc. # Match 8-digit hex: #RRGGBBAA
if ($baseName -match '^(#?)([0-9A-Fa-f]{6})([0-9A-Fa-f]{2})?') { if ($baseName -match '^(#?)([0-9A-Fa-f]{8})') {
$hasHash = $Matches[1] -eq "#" $hexCode = $Matches[2].ToUpper()
$hexCode = $Matches[2].ToUpper() return "#$hexCode"
# Return normalized format: #RRGGBB (always with #, always uppercase, no suffix) }
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 return $null
@@ -512,16 +530,20 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
continue 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) { if ($null -eq $colorCode) {
Write-Warning "Could not extract color code from: $($file.Name)" Write-Warning "Could not extract color code from: $($file.Name)"
continue continue
} }
if (-not $filesByColor.ContainsKey($colorCode)) { # Create composite key: colorCode_extension to keep TGA and JPG separate
$filesByColor[$colorCode] = @() $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 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 $flatColorsResized = 0
$flatColorsMoveLog = [System.Collections.ArrayList]::new() $flatColorsMoveLog = [System.Collections.ArrayList]::new()
foreach ($colorCode in $filesByColor.Keys) { foreach ($groupKey in $filesByColor.Keys) {
$files = $filesByColor[$colorCode] $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 $fileToKeep = $null
$targetExtension = ".jpg"
$targetFileName = "$colorCode.jpg"
# First, check if there's already a correct file in target location # First, check if there's already a correct file in target location
$existingTarget = Join-Path -Path $flatColorsPath -ChildPath $targetFileName $existingTarget = Join-Path -Path $flatColorsPath -ChildPath $targetFileName
if (Test-Path -Path $existingTarget) { if (Test-Path -Path $existingTarget) {
$existingFile = Get-Item -Path $existingTarget $existingFile = Get-Item -Path $existingTarget
# Check if it's already correct # 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 $fileToKeep = $existingFile
} }
} }
# If no existing correct file, find best source file # If no existing correct file, find best source file
if ($null -eq $fileToKeep) { if ($null -eq $fileToKeep) {
# Prefer existing JPG file # Prefer TGA file first (preserves transparency)
$jpgFile = $files | Where-Object { $tgaFile = $files | Where-Object {
($_.Extension -eq ".jpg" -or $_.Extension -eq ".jpeg") -and $_.Extension -eq ".tga" -and
(Test-Path -Path $_.FullName) (Test-Path -Path $_.FullName)
} | Select-Object -First 1 } | Select-Object -First 1
if ($jpgFile) { if ($tgaFile) {
$fileToKeep = $jpgFile $fileToKeep = $tgaFile
} else { } else {
# If no JPG, prefer TGA # If no TGA, prefer existing JPG file
$tgaFile = $files | Where-Object { $jpgFile = $files | Where-Object {
$_.Extension -eq ".tga" -and ($_.Extension -eq ".jpg" -or $_.Extension -eq ".jpeg") -and
(Test-Path -Path $_.FullName) (Test-Path -Path $_.FullName)
} | Select-Object -First 1 } | Select-Object -First 1
if ($tgaFile) { if ($jpgFile) {
$fileToKeep = $tgaFile $fileToKeep = $jpgFile
$targetExtension = ".tga"
$targetFileName = "$colorCode.tga"
} else { } else {
# Otherwise, use the first available file # Otherwise, use the first available file
$fileToKeep = $files | Where-Object { Test-Path -Path $_.FullName } | Select-Object -First 1 $fileToKeep = $files | Where-Object { Test-Path -Path $_.FullName } | Select-Object -First 1