Skip to content

Commit

Permalink
Convert provided colors properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie authored Nov 10, 2023
1 parent 2c9275d commit 12d55ef
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions PwshSpectreConsole/public/PwshSpectreConsole.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ function Convert-ToSpectreColor {
[ValidateSpectreColor()]
[string] $Color
)
# Already validated in validation attribute
if($Color.StartsWith("#")) {
$hexString = $Color -replace '^#', ''
$hexBytes = [System.Convert]::FromHexString($hexString)
return [Spectre.Console.Color]::new($hexBytes[0], $hexBytes[1], $hexBytes[2])
}
try {
# Already validated in validation attribute
if($Color.StartsWith("#")) {
$hexString = $Color -replace '^#', ''
$hexBytes = [System.Convert]::FromHexString($hexString)
return [Spectre.Console.Color]::new($hexBytes[0], $hexBytes[1], $hexBytes[2])
}

# Validated in attribute as a real color already
return [Spectre.Console.Color]::$Color
# Validated in attribute as a real color already
return [Spectre.Console.Color]::$Color
} catch {
return $script:AccentColor
}
}

function Set-SpectreColors {
Expand Down Expand Up @@ -729,9 +733,9 @@ function Format-SpectreBarChart {
.EXAMPLE
# This example displays a bar chart with the title "Fruit Sales" and a width of 50 characters.
$data = @(
@{ Label = "Apples"; Value = 10; Color = [Spectre.Console.Color]::Green },
@{ Label = "Oranges"; Value = 5; Color = [Spectre.Console.Color]::Yellow },
@{ Label = "Bananas"; Value = 3; Color = [Spectre.Console.Color]::Red }
@{ Label = "Apples"; Value = 10; Color = "Green" },
@{ Label = "Oranges"; Value = 5; Color = "Yellow" },
@{ Label = "Bananas"; Value = 3; Color = "Red" }
)
Format-SpectreBarChart -Data $data -Title "Fruit Sales" -Width 50
#>
Expand All @@ -752,10 +756,10 @@ function Format-SpectreBarChart {
process {
if($Data -is [array]) {
foreach($dataItem in $Data) {
$barChart = [Spectre.Console.BarChartExtensions]::AddItem($barChart, $dataItem.Label, $dataItem.Value, $dataItem.Color)
$barChart = [Spectre.Console.BarChartExtensions]::AddItem($barChart, $dataItem.Label, $dataItem.Value, ($dataItem.Color | Convert-ToSpectreColor))
}
} else {
$barChart = [Spectre.Console.BarChartExtensions]::AddItem($barChart, $Data.Label, $Data.Value, $Data.Color)
$barChart = [Spectre.Console.BarChartExtensions]::AddItem($barChart, $Data.Label, $Data.Value, ($Data.Color | Convert-ToSpectreColor))
}
}
end {
Expand Down Expand Up @@ -806,9 +810,9 @@ function Format-SpectreBreakdownChart {
.EXAMPLE
# This example displays a breakdown chart with the title "Fruit Sales" and a width of 50 characters.
$data = @(
@{ Label = "Apples"; Value = 10; Color = [Spectre.Console.Color]::Red },
@{ Label = "Oranges"; Value = 20; Color = [Spectre.Console.Color]::Orange1 },
@{ Label = "Bananas"; Value = 15; Color = [Spectre.Console.Color]::Yellow }
@{ Label = "Apples"; Value = 10; Color = "Red" },
@{ Label = "Oranges"; Value = 20; Color = "Orange1" },
@{ Label = "Bananas"; Value = 15; Color = "Yellow" }
)
Format-SpectreBreakdownChart -Data $data -Width 50
#>
Expand All @@ -825,10 +829,10 @@ function Format-SpectreBreakdownChart {
process {
if($Data -is [array]) {
foreach($dataItem in $Data) {
[Spectre.Console.BreakdownChartExtensions]::AddItem($chart, $dataItem.Label, $dataItem.Value, $dataItem.Color) | Out-Null
[Spectre.Console.BreakdownChartExtensions]::AddItem($chart, $dataItem.Label, $dataItem.Value, ($dataItem.Color | Convert-ToSpectreColor)) | Out-Null
}
} else {
[Spectre.Console.BreakdownChartExtensions]::AddItem($chart, $Data.Label, $Data.Value, $Data.Color) | Out-Null
[Spectre.Console.BreakdownChartExtensions]::AddItem($chart, $Data.Label, $Data.Value, ($Data.Color | Convert-ToSpectreColor)) | Out-Null
}
}
end {
Expand Down

0 comments on commit 12d55ef

Please sign in to comment.