WIN2D official article series translation-pre-multiply Alpha

Source: Internet
Author: User
Tags transparent color

This article is a personal blog backup article, the original address:

http://validvoid.net/win2d-premultiplied-alpha/

There are two ways to represent the opacity of a color value in a computer drawing. Both methods are used in win2d. This article aims to explain the differences between the two methods and their respective usage scenarios.

Direct Alpha (straight Alpha)

When using direct, i.e. linear Alpha:

    • RGB value specifies the color of the drawing target
    • Alpha value specifies the level of quality of the drawing target

In this method, the RGB and Alpha channels work independently. They can change individually without affecting each other. To fade an object, simply keep the RGB value constant and gradually decrease the Alpha value.

The source-over pattern blending of two colors is implemented in direct Alpha format:

* 源.A) + (目标.RGB * (1 - 源.A))
Pre-multiply alpha (premultiplied alpha)

When using pre-multiply Alpha:

    • RGB Specifies the amount of color to draw when the target output is drawn
    • Alpha value specifies how much background content is hidden

In this method, the RGB channel is associated with the Alpha channel. To make an object transparent, you must reduce the RGB values (decreasing the color concentration) and the Alpha value (reducing the amount of hiding the background content). A fully transparent object does not have any color, so only one value represents the transparency of 100%: both RGB and Alpha values are zero.

Source-over mode blending of two colors in pre-multiply Alpha format:

结果 = 源.RGB + (目标.RGB * (1 - 源.A))

In the field of graphics rendering, the pre-multiply Alpha method is typically applied when performing image filtering or multi-layer composition because it works better than direct Alpha. More information can be found in:

    • Http://blogs.msdn.com/b/shawnhar/archive/2009/11/06/premultiplied-alpha.aspx
    • Http://blogs.msdn.com/b/shawnhar/archive/2009/11/07/premultiplied-alpha-and-image-composition.aspx
Alpha in the win2d

WIN2D uses direct alpha at the API level, and pre-multiply alpha is used in internal rendering operations.

Windows.UI.ColorThe value is direct Alpha. When you Draw Fill pass a color parameter to the * or * method, set the color of the brush, or clear the screen with a color, the color uses direct Alpha.

The pixel values stored in a bitmap (bitmap) or render target (rendertarget), as well as drawing or blending operations at this level, use the pre-multiply Alpha. When a bitmap is loaded from a file, its contents are automatically converted to a pre-multiply format. When you call a win2d drawing method, its color parameters are also converted directly to the pre-multiply before the drawing actually occurs.

win2d image effects mixed using direct and pre-multiply Alpha. Some effects use one of these formats, other effects use a different format, and some effects provide an attribute to choose from. Each type of special effects document has a description of which Alpha mode is used for the effect. The input data of the effect is always assumed to be a pre-multiplication format, so when an effect is applied directly to Alpha, it performs an inverse pre-multiplication transformation First, then calculates the effect, and finally re-performs the pre-multiply output.

GetPixelBytes, and, SetPixelBytes GetPixelColors and SetPixelColors so on, the bitmap API does not convert any Alpha format. They pass in a bit value directly between the underlying GPU texture and the upper object. You can see how the Alpha format is handled internally by WIN2D in the following ways:

    • Create a drawing session for a RenderTarget
    • CalldrawingSession.Clear(Colors.Tranparent)
    • Colors.TranparentDefined as r=255, g=255, b=255, a=0
    • WIN2D will convert this transparent color value to a pre-multiply value, i.e. r=0, g=0, b=0, a=0
    • Using GetPixelColors read-back rendertarget content
    • The observed content contains the color value of the pre-multiplied rgb=0, rather than the value of the original direct Alpha format Colors.Tranparent rgb=255
Converting between Alpha formats

To convert the color of the direct alpha format to the pre-multiply alpha format, multiply the values of R, G, and B three of the color by the A value, respectively. To convert from pre-multiply alpha to direct alpha, divide the R, G, and B three values by the A value, respectively.

Note Color information is typically represented by a byte value ranging from 0 to 255 (for example, Windows.UI.Color a structure consists of 4 bytes). This representation changes by 255 as the conversion factor, so the byte value 255 means 1, and 128 means half. When you do a format conversion, you must bring in the conversion factor to participate in the calculation, so that a Windows.UI.Color conversion from a direct to a pre-multiply calculation process is:

预乘值.R = (byte)(直接值.R * 直接值.A / 255);预乘值.G = (byte)(直接值.G * 直接值.A / 255);预乘值.B = (byte)(直接值.B * 直接值.A / 255);预乘值.A = 直接值.A;

If the existing image data has an incorrect Alpha format, you can use Premultiplyeffect or unpremultiplyeffect effects for conversion.

WIN2D official article series translation-pre-multiply Alpha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.