dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

What is the "W" in this RGBAW color space matrix

Open Joachim-Otahal opened this issue 1 year ago • 5 comments

Type of issue

Missing information

Description

That article helped me a lot to massively speed up an image invert and color change in powershell. But what is the "W" in the color space here? I tried to find anything, but all search results link back to this article.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.colormatrix?view=windowsdesktop-9.0

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Drawing.Imaging/ColorMatrix.xml

Document Version Independent Id

965b4c7a-083d-b594-f75d-e4f63b71e2e0

Article author

@dotnet-bot

Joachim-Otahal avatar Dec 23 '24 18:12 Joachim-Otahal

White.

gfoidl avatar Dec 24 '24 10:12 gfoidl

White.

Thank you for your response in the 24th of December! I suspected too, but... How or when is it applied? I changed the fifth value to 0, 0.5, 1, 1000, -1000 in each row. And the outcome is no different, pixel exact the same transformation as the rest of that colormatrix says. Is it applied only when a specific colorspace is used? The source and destination picture it is applied to is: [System.Drawing.Imaging.PixelFormat]::Format24bppRgb. Or is there something else how this value is used?

The whole powershell code used is this, to invert a white background diagram into dark color scheme and then gamma up the lower values to be better visible as a non-intrusive regularly updated windows wallpaper. $bmp is the original to be transformed. (Code to update the currently shown wallpaper is left out)

# https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.colormatrix
$ColorMatrix = [System.Drawing.Imaging.ColorMatrix]::new( [float[][]] @(
        [float[]](-0.2, 0, 0, 0, 0), # Invert R
        [float[]](0, -0.2, 0, 0, 0), # Invert G
        [float[]](0, 0, -0.2, 0, 0), # Invert B
        [float[]](0, 0, 0, 1, 0),  # Alpha: Do nothing
        [float[]](0.2, 0.2, 0.2, 0, 1)   # Translate/Offset RGBA
    )
)
# https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.imageattributes
$ImageAttributes = [System.Drawing.Imaging.ImageAttributes]::new()
$ImageAttributes.SetColorMatrix($ColorMatrix)
# https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.imageattributes.setgamma
$ImageAttributes.SetGamma(0.6)
$bmpinv = [System.Drawing.Bitmap]::new($bmp.Width,$bmp.Height,[System.Drawing.Imaging.PixelFormat]::Format24bppRgb)
$pictinv = [System.Drawing.Graphics]::FromImage($bmpinv)
$pictinv.DrawImage($bmp, # Source image
    [System.Drawing.Rectangle]::new(0,0,$bmp.Width,$bmp.Height), # Destination rectangle, here same as source
    0,0,$bmp.Width,$bmp.Height, # Source rectangle.
    [System.Drawing.GraphicsUnit]::Pixel, # We want 1:1 not inch or whatever
    $ImageAttributes # Finally the matrix
)
$bmpinv.Save("$DataBaseDir\today-inv.png",[System.Drawing.Imaging.ImageFormat]::Png)
$pictinv.Dispose()
$bmpinv.Dispose()

Joachim-Otahal avatar Dec 24 '24 11:12 Joachim-Otahal

@JeremyKuhne

carlossanlop avatar Mar 03 '25 18:03 carlossanlop

Can't offer a lot here, other than this is GDI+ under the covers. You might find something useful here: https://learn.microsoft.com/windows/win32/gdiplus/-gdiplus-recoloring-use.

JeremyKuhne avatar Mar 03 '25 18:03 JeremyKuhne

Can't offer a lot here, other than this is GDI+ under the covers. You might find something useful here: https://learn.microsoft.com/windows/win32/gdiplus/-gdiplus-recoloring-use.

Oh! That explains it. It is actually a 4x5 matrix, with the fifth row being the non-matrix translation (or offset, if you like). But since a 4x5 matrix in "unwanted" in math, and probably unwanted when using hardware acceleration, the fifth column is added but unused. The article states what the fifth column must always be 0/0/0/0/1 for consistency reasons, but either it gets completely ignored by the math inside by design (unused), gets deliberately ignored by the code (not read the column and use correct value) or somewhere it gets force-set to 0/0/0/0/1 (read that column and correct). This means the DOTNET-Article is wrong, or unclear. It is not RGBAW space it is RGBA[dummy] space. My suggestion would be to add the fourth paragraph to the DOTNET article and state that "W" is that placeholder to get a clean 5x5 matrix. So: What does the code behind it do? Not use, ignore, or enforce?

Joachim-Otahal avatar Mar 03 '25 19:03 Joachim-Otahal