Skip to main content
added 6 characters in body
Source Link
mclayton
  • 9.6k
  • 3
  • 24
  • 30
function Invoke-HueRotate
{
    param( [uint] $Color, [decimal] $Degrees )

    # split color into r, g and b channels
    $r = ($Color -shr 16) -band 0xFF;
    $g = ($Color -shr  8) -band 0xFF;
    $b = ($Color -shr  0) -band 0xFF;

    # convert degrees to radians and evaluate cos / sin
    $cos$rad = [Math]::Cos($Degrees * [Math]::PI / 180);180;
    $sin$cos = [Math]::SinCos($Degrees$rad);
 *   $sin = [Math]::PI / 180Sin($rad);

    # long-hand matrix definition
    # see https://drafts.fxtf.org/filter-effects/#feColorMatrixElement
    # where it says 'For type="hueRotate"'
    $a00 = 0.213 + ( 0.787 * $cos) + (-0.213 * $sin);
    $a01 = 0.715 + (-0.715 * $cos) + (-0.715 * $sin);
    $a02 = 0.072 + (-0.072 * $cos) + ( 0.928 * $sin);
    $a10 = 0.213 + (-0.213 * $cos) + ( 0.143 * $sin);
    $a11 = 0.715 + ( 0.285 * $cos) + ( 0.140 * $sin);
    $a12 = 0.072 + (-0.072 * $cos) + (-0.283 * $sin);
    $a20 = 0.213 + (-0.213 * $cos) + (-0.787 * $sin);
    $a21 = 0.715 + (-0.715 * $cos) + ( 0.715 * $sin);
    $a22 = 0.072 + ( 0.928 * $cos) + ( 0.072 * $sin);

    # long-hand matrix multiplication
    $r2 = ($a00 * $r) + ($a01 * $g) + ($a02 * $b);
    $g2 = ($a10 * $r) + ($a11 * $g) + ($a12 * $b);
    $b2 = ($a20 * $r) + ($a21 * $g) + ($a22 * $b);

    # re-combine r, g and b channels
    $result = [uint](
        ([Math]::Clamp([int]$r2, 0, 255) -shl 16) `
        -bor
        ([Math]::Clamp([int]$g2, 0, 255) -shl  8) `
        -bor
        ([Math]::Clamp([int]$b2, 0, 255) -shl  0) `
    );

    return $result;

}
$color = "#CC1100""#CC1100";

$original = ConvertFrom-HtmlColor -Value $color;
ConvertTo-HtmlColor $original;
#CC1100

$inverted = Invoke-InvertColor -Color $original;
ConvertTo-HtmlColor $inverted;
#33EEFF

$rotated = Invoke-HueRotate -Color $inverted -Degrees 180;
ConvertTo-HtmlColor $rotated;
#FFA190
function Invoke-HueRotate
{
    param( [uint] $Color, [decimal] $Degrees )

    # split color into r, g and b channels
    $r = $Color -shr 16 -band 0xFF;
    $g = $Color -shr  8 -band 0xFF;
    $b = $Color -shr  0 -band 0xFF;

    # convert degrees to radians and evaluate cos / sin
    $cos = [Math]::Cos($Degrees * [Math]::PI / 180);
    $sin = [Math]::Sin($Degrees * [Math]::PI / 180);

    # long-hand matrix definition
    # see https://drafts.fxtf.org/filter-effects/#feColorMatrixElement
    # where it says 'For type="hueRotate"'
    $a00 = 0.213 + ( 0.787 * $cos) + (-0.213 * $sin);
    $a01 = 0.715 + (-0.715 * $cos) + (-0.715 * $sin);
    $a02 = 0.072 + (-0.072 * $cos) + ( 0.928 * $sin);
    $a10 = 0.213 + (-0.213 * $cos) + ( 0.143 * $sin);
    $a11 = 0.715 + ( 0.285 * $cos) + ( 0.140 * $sin);
    $a12 = 0.072 + (-0.072 * $cos) + (-0.283 * $sin);
    $a20 = 0.213 + (-0.213 * $cos) + (-0.787 * $sin);
    $a21 = 0.715 + (-0.715 * $cos) + ( 0.715 * $sin);
    $a22 = 0.072 + ( 0.928 * $cos) + ( 0.072 * $sin);

    # long-hand matrix multiplication
    $r2 = ($a00 * $r) + ($a01 * $g) + ($a02 * $b);
    $g2 = ($a10 * $r) + ($a11 * $g) + ($a12 * $b);
    $b2 = ($a20 * $r) + ($a21 * $g) + ($a22 * $b);

    # re-combine r, g and b channels
    $result = [uint](
        ([Math]::Clamp([int]$r2, 0, 255) -shl 16) `
        -bor
        ([Math]::Clamp([int]$g2, 0, 255) -shl 8) `
        -bor
        ([Math]::Clamp([int]$b2, 0, 255) -shl 0) `
    );

    return $result;

}
$color = "#CC1100"

$original = ConvertFrom-HtmlColor -Value $color;
ConvertTo-HtmlColor $original;
#CC1100

$inverted = Invoke-InvertColor -Color $original;
ConvertTo-HtmlColor $inverted;
#33EEFF

$rotated = Invoke-HueRotate -Color $inverted -Degrees 180;
ConvertTo-HtmlColor $rotated;
#FFA190
function Invoke-HueRotate
{
    param( [uint] $Color, [decimal] $Degrees )

    # split color into r, g and b channels
    $r = ($Color -shr 16) -band 0xFF;
    $g = ($Color -shr  8) -band 0xFF;
    $b = ($Color -shr  0) -band 0xFF;

    # convert degrees to radians and evaluate cos / sin
    $rad = $Degrees * [Math]::PI / 180;
    $cos = [Math]::Cos($rad);
    $sin = [Math]::Sin($rad);

    # long-hand matrix definition
    # see https://drafts.fxtf.org/filter-effects/#feColorMatrixElement
    # where it says 'For type="hueRotate"'
    $a00 = 0.213 + ( 0.787 * $cos) + (-0.213 * $sin);
    $a01 = 0.715 + (-0.715 * $cos) + (-0.715 * $sin);
    $a02 = 0.072 + (-0.072 * $cos) + ( 0.928 * $sin);
    $a10 = 0.213 + (-0.213 * $cos) + ( 0.143 * $sin);
    $a11 = 0.715 + ( 0.285 * $cos) + ( 0.140 * $sin);
    $a12 = 0.072 + (-0.072 * $cos) + (-0.283 * $sin);
    $a20 = 0.213 + (-0.213 * $cos) + (-0.787 * $sin);
    $a21 = 0.715 + (-0.715 * $cos) + ( 0.715 * $sin);
    $a22 = 0.072 + ( 0.928 * $cos) + ( 0.072 * $sin);

    # long-hand matrix multiplication
    $r2 = ($a00 * $r) + ($a01 * $g) + ($a02 * $b);
    $g2 = ($a10 * $r) + ($a11 * $g) + ($a12 * $b);
    $b2 = ($a20 * $r) + ($a21 * $g) + ($a22 * $b);

    # re-combine r, g and b channels
    $result = [uint](
        ([Math]::Clamp([int]$r2, 0, 255) -shl 16) `
        -bor
        ([Math]::Clamp([int]$g2, 0, 255) -shl  8) `
        -bor
        ([Math]::Clamp([int]$b2, 0, 255) -shl  0) `
    );

    return $result;

}
$color = "#CC1100";

$original = ConvertFrom-HtmlColor -Value $color;
ConvertTo-HtmlColor $original;
#CC1100

$inverted = Invoke-InvertColor -Color $original;
ConvertTo-HtmlColor $inverted;
#33EEFF

$rotated = Invoke-HueRotate -Color $inverted -Degrees 180;
ConvertTo-HtmlColor $rotated;
#FFA190
deleted 3 characters in body
Source Link
mclayton
  • 9.6k
  • 3
  • 24
  • 30
function Invoke-InvertColor
{
    param( [uint] $Color, [decimal] $Degrees )
    return $Color -bxor 0xFFFFFF;
}

function ConvertFrom-HtmlColor
{
    param( [string] $Value )
    return [System.Convert]::ToUInt32($Value.Substring(1), 16);
}

function ConvertTo-HtmlColor
{
    param( [uint] $Value )
    return "#" + $Value.ToString("X2").PadLeft(6, "0");
}
function Invoke-InvertColor
{
    param( [uint] $Color, [decimal] $Degrees )
    return $Color -bxor 0xFFFFFF;
}

function ConvertFrom-HtmlColor
{
    param( [string] $Value )
    return [System.Convert]::ToUInt32($Value.Substring(1), 16);
}

function ConvertTo-HtmlColor
{
    param( [uint] $Value )
    return "#" + $Value.ToString("X2").PadLeft(6, "0");
}
function Invoke-InvertColor
{
    param( [uint] $Color )
    return $Color -bxor 0xFFFFFF;
}

function ConvertFrom-HtmlColor
{
    param( [string] $Value )
    return [System.Convert]::ToUInt32($Value.Substring(1), 16);
}

function ConvertTo-HtmlColor
{
    param( [uint] $Value )
    return "#" + $Value.ToString("X2").PadLeft(6, "0");
}
deleted 3 characters in body
Source Link
mclayton
  • 9.6k
  • 3
  • 24
  • 30

Update

Additional verification of the function result:

<style>
div {
    background-color: #33EEFF;
    width: 250px;
    height: 250px;
    filter: hue-rotate(180deg);
}
</style>

<div>
</div>

gives this:

enter image description here

and if you use the color dropper in your favourite image editor it'll tell you the peach color is #FFA190 which matches the result from the PowerShell code above...


Update

Additional verification of the function result:

<style>
div {
    background-color: #33EEFF;
    width: 250px;
    height: 250px;
    filter: hue-rotate(180deg);
}
</style>

<div>
</div>

gives this:

enter image description here

and if you use the color dropper in your favourite image editor it'll tell you the peach color is #FFA190 which matches the result from the PowerShell code above...

deleted 3 characters in body
Source Link
mclayton
  • 9.6k
  • 3
  • 24
  • 30
Loading
Source Link
mclayton
  • 9.6k
  • 3
  • 24
  • 30
Loading