Skip to content

Commit

Permalink
add converting parameterset example
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Nov 27, 2023
1 parent 9057ffa commit acd0519
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Pwsh/Parameters/Cleaner ParameterSets for ConvertTo-Hsv.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

function ConvertTo-HSV {
<#
.synopsis
example to demonstrate another way to pipe array values
.NOTES
from JustinGrote in discord: <https://discord.com/channels/180528040881815552/447476117629304853/1178486715581734993>
.EXAMPLE
Pwsh> $c =
[Pscustomobject]@{
Red = 100
Green = 200
Blue = 128
Alpha = 9
}
Pwsh> $c
Red Green Blue Alpha
--- ----- ---- -----
100 200 128 9
Pwsh> $c | ConvertTo-HSV
Hue Saturation Value
--- ---------- -----
136.80 0.48 0.59
#>
[CmdletBinding(DefaultParameterSetName='ByValue')]
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName='ByValue')]
[ValidateRange(0, 255)]
[int]$Red,

[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'ByValue')]
[ValidateRange(0, 255)]
[int]$Green,

[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'ByValue')]
[ValidateRange(0, 255)]
[int]$Blue,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ByValue')]
[ValidateRange(0, 255)]
[int]$Alpha = 0,

[Parameter(Mandatory, ValueFromPipeline, ParameterSetName='ByColor')]
[System.Drawing.Color]$Color
)

process {

if ($PSCmdlet.ParameterSetName -eq 'ByValue') {
$Color = [Drawing.Color]::FromArgb($Alpha, $Red, $Green, $Blue)
}

[PSCustomObject]@{
Hue = $Color.GetHue()
Saturation = $Color.GetSaturation()
Value = $Color.GetBrightness()
PSTypeName = 'HSV'
}
}
}
$c = [Pscustomobject]@{
Red = 100
Green = 200
Blue = 128
Alpha = 9
}


$c | ft -auto
$c | ConvertTo-HSV | ft -auto

0 comments on commit acd0519

Please sign in to comment.