Skip to content

Instantly share code, notes, and snippets.

@ConnorGriffin
Last active July 26, 2018 18:08
Show Gist options
  • Save ConnorGriffin/ac21c25ecd7ef5e918cbd28e5cb6ed0d to your computer and use it in GitHub Desktop.
Save ConnorGriffin/ac21c25ecd7ef5e918cbd28e5cb6ed0d to your computer and use it in GitHub Desktop.
Powershell script to convert HSL to RGB
function ConvertFrom-Hsl {
param(
$Hue,
$Saturation,
$Lightness,
# Return in ConEmu.xml ABGR hex format
[Switch]$ABGR
)
function ToHex ($c) {
$hex = [Convert]::ToString([Math]::Round($c * 255), 16).ToUpper()
if ($hex.Length -eq 1) {
"0$hex"
} else {
"$hex"
}
}
$Hue = [double]($Hue / 360)
if ($Saturation -gt 1) {
$Saturation = [double]($Saturation / 100)
}
if ($Lightness -gt 1) {
$Lightness = [double]($Lightness / 100)
}
if ($Saturation -eq 0) {
# No color
$red = $green = $blue = $Lightness
} else {
function HueToRgb ($p,$q,$t) {
if ($t -lt 0) {
$t++
}
if ($t -gt 1) {
$t--
}
if ($t -lt 1/6) {
return $p + ($q - $p) * 6 * $t
}
if ($t -lt 1/2) {
return $q
}
if ($t -lt 2/3) {
return $p + ($q - $p) * (2/3 - $t) * 6
}
return $p
}
$q = if ($Lightness -lt .5) {
$Lightness * (1 + $Saturation)
} else {
$Lightness + $Saturation - $Lightness * $Saturation
}
$p = 2 * $Lightness - $q
$red = HueToRgb $p $q ($Hue + 1/3)
$green = HueToRgb $p $q $Hue
$blue = HueToRgb $p $q ($Hue - 1/3)
}
if ($ABGR) {
$b = ToHex $blue
$g = ToHex $green
$r = Tohex $red
"$b$g$r"
} else {
[Ordered]@{
Red = [Math]::Round($red * 255)
Green = [Math]::Round($green * 255)
Blue = [Math]::Round($blue * 255)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment