|
| 1 | +# Define a function to determine the nearest approximate ConsoleColor based on RGB values. |
| 2 | +function Get-NearestConsoleColor { |
| 3 | + param ( |
| 4 | + [int]$r, |
| 5 | + [int]$g, |
| 6 | + [int]$b |
| 7 | + ) |
| 8 | + |
| 9 | + # Define approximate RGB values for each ConsoleColor. |
| 10 | + $colors = @{ |
| 11 | + 'Black' = @{ r = 0; g = 0; b = 0 } |
| 12 | + 'DarkBlue' = @{ r = 0; g = 0; b = 128 } |
| 13 | + 'DarkGreen' = @{ r = 0; g = 128; b = 0 } |
| 14 | + 'DarkCyan' = @{ r = 0; g = 128; b = 128 } |
| 15 | + 'DarkRed' = @{ r = 128; g = 0; b = 0 } |
| 16 | + 'DarkMagenta' = @{ r = 128; g = 0; b = 128 } |
| 17 | + 'DarkYellow' = @{ r = 128; g = 128; b = 0 } |
| 18 | + 'Gray' = @{ r = 192; g = 192; b = 192 } |
| 19 | + 'DarkGray' = @{ r = 128; g = 128; b = 128 } |
| 20 | + 'Blue' = @{ r = 0; g = 0; b = 255 } |
| 21 | + 'Green' = @{ r = 0; g = 255; b = 0 } |
| 22 | + 'Cyan' = @{ r = 0; g = 255; b = 255 } |
| 23 | + 'Red' = @{ r = 255; g = 0; b = 0 } |
| 24 | + 'Magenta' = @{ r = 255; g = 0; b = 255 } |
| 25 | + 'Yellow' = @{ r = 255; g = 255; b = 0 } |
| 26 | + 'White' = @{ r = 255; g = 255; b = 255 } |
| 27 | + } |
| 28 | + |
| 29 | + $bestColor = $null |
| 30 | + $bestDistance = [double]::MaxValue |
| 31 | + |
| 32 | + # Loop through each color to find the nearest match using Euclidean distance in RGB space. |
| 33 | + foreach ($color in $colors.Keys) { |
| 34 | + $cr = $colors[$color].r |
| 35 | + $cg = $colors[$color].g |
| 36 | + $cb = $colors[$color].b |
| 37 | + # Calculate the Euclidean distance in RGB space. |
| 38 | + $distance = [math]::Sqrt( ($r - $cr) * ($r - $cr) + ($g - $cg) * ($g - $cg) + ($b - $cb) * ($b - $cb) ) |
| 39 | + if ($distance -lt $bestDistance) { |
| 40 | + $bestDistance = $distance |
| 41 | + $bestColor = $color |
| 42 | + } |
| 43 | + } |
| 44 | + return $bestColor |
| 45 | +} |
| 46 | + |
| 47 | +# Define the height of the triangle (number of rows). |
| 48 | +$height = 10 |
| 49 | + |
| 50 | +# Output each row of the triangle. |
| 51 | +for ($i = 0; $i -lt $height; $i++) { |
| 52 | + # Add left padding spaces to center-align the triangle. |
| 53 | + $padding = " " * ($height - $i) |
| 54 | + Write-Host -NoNewline $padding |
| 55 | + |
| 56 | + # Each row will have (2*$i + 1) characters. |
| 57 | + for ($j = 0; $j -lt (2 * $i + 1); $j++) { |
| 58 | + # Calculate the horizontal ratio 's' for the current cell (range: 0 to 1). |
| 59 | + if ($i -eq 0) { |
| 60 | + $s = 0.5 # The first row has only one character, so set s to 0.5. |
| 61 | + } else { |
| 62 | + $s = $j / (2 * $i) |
| 63 | + } |
| 64 | + # Calculate the vertical ratio 't' (0 at the top, 1 at the bottom). |
| 65 | + $t = $i / ($height - 1) |
| 66 | + |
| 67 | + # Calculate each color component using simple linear interpolation: |
| 68 | + # Top vertex (t=0) is Red: (255, 0, 0). |
| 69 | + # Bottom-left vertex (s=0, t=1) is Blue: (0, 0, 255). |
| 70 | + # Bottom-right vertex (s=1, t=1) is Green: (0, 255, 0). |
| 71 | + $R = [int](255 * (1 - $t)) |
| 72 | + $B = [int](255 * $t * (1 - $s)) |
| 73 | + $G = [int](255 * $t * $s) |
| 74 | + |
| 75 | + # Determine the nearest ConsoleColor for the calculated RGB value. |
| 76 | + $consoleColor = Get-NearestConsoleColor -r $R -g $G -b $B |
| 77 | + |
| 78 | + Write-Host "*" -ForegroundColor $consoleColor -NoNewline |
| 79 | + } |
| 80 | + Write-Host "" |
| 81 | +} |
0 commit comments