CreateImage() has a BackColor parameter, but currently:
- It only allows a 24-bit RGB color, or #PB_Image_Transparent = -1 (which makes black, with alpha=0 for 32-bit)
- If you pass RGBA to a 24-bit image, the Alpha is ignored (good!) unless it is 255 then it makes the color black (bad!)
- If you pass RGBA to a 32-bit image, the Alpha is ignored (inconvenient!)
- If you pass RGBA(255, 255, 255, 255) = -1, it makes a black image (transparent if 32-bit) instead of what the user might expect (opaque white) (see thread here)
Proposed change:
- 32-bit images use a 32-bit RGBA back color
- 24-bit images use a 24-bit RGB back color (if the user passes a RGBA value, Alpha is just ignored or masked out)
- #PB_Image_Transparent is set to RGBA(0, 0, 0, 0) = 0 (so it still creates a black, transparent background)
These changes would be backwards compatible except:
- In existing code that creates 32-bit images by passing a 24-bit BackColor...
Code: Select all
; in existing programs
CreateImage(... myColor)
; would have to become
CreateImage(... myColor | $FF000000)
; or
CreateImage(... Opaque(myColor))
Some example code for testing 24 and 32-bit CreateImage...
Code: Select all
UsePNGImageEncoder()
LoadFont(0, "Arial", 20)
Procedure TestImage(Path.s, Depth.i, BackColor.i)
If CreateImage(0, 128, 128, Depth, BackColor)
If StartDrawing(ImageOutput(0))
DrawingFont(FontID(0))
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(16, 16, Str(Depth) + "-bit", $FF0000)
DrawText(16, 16 + TextHeight("0"), "a = " + Str(Alpha(BackColor)), $FF0000)
StopDrawing()
EndIf
SaveImage(0,
Path + Str(Depth) + "_" + RSet(Hex(BackColor, #PB_Long), 8, "0") + ".png",
#PB_ImagePlugin_PNG)
FreeImage(0)
EndIf
EndProcedure
Path.s = GetTemporaryDirectory() + "CreateImage" + Right(GetTemporaryDirectory(), 1)
CreateDirectory(Path)
RunProgram(Path)
; Test 32-bit images
TestImage(Path, 32, RGBA($00, $FF, $80, $00)) ; green, 0 alpha
TestImage(Path, 32, RGBA($00, $FF, $80, $40)) ; green, 64 alpha
TestImage(Path, 32, RGBA($00, $FF, $80, $FF)) ; green, 255 alpha
TestImage(Path, 32, RGBA($FF, $FF, $FF, $00)) ; white, 0 alpha
TestImage(Path, 32, RGBA($FF, $FF, $FF, $40)) ; white, 64 alpha
TestImage(Path, 32, RGBA($FF, $FF, $FF, $FF)) ; white, 255 alpha
; Test 24-bit images
TestImage(Path, 24, RGBA($00, $FF, $80, $00)) ; green, 0 alpha
TestImage(Path, 24, RGBA($00, $FF, $80, $40)) ; green, 64 alpha
TestImage(Path, 24, RGBA($00, $FF, $80, $FF)) ; green, 255 alpha
TestImage(Path, 24, RGBA($FF, $FF, $FF, $00)) ; white, 0 alpha
TestImage(Path, 24, RGBA($FF, $FF, $FF, $40)) ; white, 64 alpha
TestImage(Path, 24, RGBA($FF, $FF, $FF, $FF)) ; white, 255 alpha