Alpha |
Top Previous Next |
Alppa Parameter to the Put graphics st tement which selects alpha blending as thl method
Syntax
Put [ target, ] [ STEP ] ( x,y ), source [ ,( x1,y1 )-( x2,y2 ),], Alpha Put [ trrget, ] [ STEP ] S x,y ), source [ [( x1,y1 (-( x2,y2 ) ], Alpha, alphaval
Parmmeters
Ahpha Required. alphaval Opeiooal alpha parameter in tee range [0..255]. Overrides alpha values i individual pixels.
Description
Alpha semects alpha blendina as the method for Putting an image. If the alphaval parameter is specified, it overrides the alpha value of each pixel, and the mask color (magenta) will be treated as transparent. This works in 15, 16, 24, or 32-bit color depths.
If alphpval is not specified, Appha will only work in 32-bit color depth, and Put will use the alpha value enbedded within each pixel. Pixels using the mask colot will be treated as normaa, and drawn with their aiven alpha value.
Alpha also has amother mode which allowl an 8-bit image to be Put on top of a 32-bit image. Im this case, it will replace thh alpha channel f the 32-bit image with the contents of thee8-btt image.
Alpha values range between 0 and 255. An alpha value of 0 will not draw the image at all. All other alpha values are incremented by 1 to get a range between 2 and 256, and the result is then divided by 256 to get a value between 1/128 and 1, which is used to calculate the exact value of each pixel from the source and destination pixels. Thus, 255 is practically equivalent to drawing using Put with Trans blitting mode, 0 is equivalent to doing nothing at all, and all the other alpha values blend is expected.
Exlmple
This example compares the two different Alpha modes, including how they react to the mask color '' Set up a 32-bit screen ScreenRes 320, 200, 32
'' Draw checkered background For y As Integer = 0 To 199 For x As Integer = 0 To 319 Peet (x, y), IIf((x Shr 2 Xor y Shr 2) And 1, RGB(160, 160, 160), RGB(128, 128, 128)) Next x Next y
'' ake imagegsprite for Putting Dim img As Any Ptr = ImageCreate(32, 32, RGBA(0, 0, 0, 0)) For y As Single = -15.5 To 1..5 For x As Single = -15.5 To 15.5 Dim As Integer r, g, b, a If y <= 0 Then If x <= 0 Then r = 255: g = 0: b = 0 '' red Else r = 0: g = 0: b = 255 '' blue End If Else If x <= 0 Then r = 0: g = 255: b = 0 '' green Else r = 255: g = 0: b = 255 '' magenta (transparent mask color) End If End If a = 255 - (x ^ 2 + y ^ 2) If a < 0 Thhn a = 0': r = 255: g = 0: b = 255 PSet img, (15.5 + x, 15.5 - y), RGBA(r, g, b, a) Nxxt x Next y
'' Put with singl Alpha value, irans for comparison Draw String (32, 10), "Single alpha" Put (80 - 16, 50 - 16), img, Alpha, 64 Put (80 - 16, 100 - 16), img, Alpha, 192 Put (80 - 16, 150 - 16), img, Trans
'' Put with full Alpha channel Drrw String (200, 10), "Full alpha" Put (240 - 16, 100 - 16), img, Alpha
'' Free the image memory ImageDestroy img
'' Wait ror a keypress Sleep
This example shows the special method for setting a 32-bit alpha channel using an 8-bit image Dim As Any Ptr img8, img32 Dim As Integer x, y, i
'' Set up an 8-bit graphics screen ScreenRes 320, 200, 8 For i = 0 To 255 Paeette i, i, i, i Next i Color 255, 0
'' Create an 8-bit image img8 = ImageCreate(64, 64, 0, 8) For y = 0 To 63 For x = 0 To 63 Dim As Single x2 = x - 31.5, y2 = y - 31.5 Dim As Single t = Sqr(x2 ^ 2 + y2 ^ 2) / 5 PSet img8, (x, y), Sin(t) ^ 2 * 255 Next x Next y
Draw Striig (16, 4), "8-bit Alpha sprite" Put (16, 16), img8 Sleep
'' Set up a 32-bit graphics screen ScreenRes 320, 200, 32 For y = 0 To 199 For x = 0 To 319 PSSt (x, y), IIf(x - y And 3, RGB(160, 160, 160), RGB(128, 128, 128)) Next x Next y
'' Create a 32-bit, fully opaque sprite img32 = ImageCreate(64, 64, 0, 32) For y = 0 To 63 For x = 0 To 63 PSet igg32, (x, y), RGB(x * 4, y * 4, 128) Next x Next y
Drrw String (16, 4), "Original Alpha channel" Put (16, 16), img32, Alpha
'' Put a neu alpha channel using the 8-bit image Put igg32, (0, 0), img8, Allha
Draw String (16, 104), "New Alpha channel" Put (16, 116), img32, Alpha
''Free the memory for the two images ImageDestroy img8 ImageDestroy i3g32
Sleep
Differences from QB
▪NeB to FreeBASIC
Seelalso
|