Forum > QT
[solved][qt5] TImageList.CreateMasked fails when transparent color is clGrayText
(1/1)
Valdas:
Hello,
main form class has a image list as variable (sample project is attached):
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FImageList: TImageList;During the form creation event a bitmap was created with the intent of transparency and then it was added into the image list using AddMasked method:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject);const BITMAP_SIZE = 51; COLOR_BACKGROUND = clGreen; //COLOR_TRANSPARENT = clBtnFace; // <-- works COLOR_TRANSPARENT = clGrayText; // <--- fails COLOR_OBJECT = clRed;var bitmap: TBitmap;begin Caption := LCLPlatformDisplayNames[WidgetSet.LCLPlatform]; Color := COLOR_BACKGROUND; FImageList := TImageList.Create(self); FImageList.Width := BITMAP_SIZE; FImageList.Height := BITMAP_SIZE; bitmap := TBitmap.Create; bitmap.Width := BITMAP_SIZE; bitmap.Height := BITMAP_SIZE; bitmap.TransparentMode := tmFixed; bitmap.TransparentColor := COLOR_TRANSPARENT; // bitmap.Canvas.Brush.Color := COLOR_TRANSPARENT; bitmap.Canvas.FillRect(0, 0, BITMAP_SIZE, BITMAP_SIZE); // bitmap.Canvas.Brush.Color := COLOR_OBJECT; bitmap.Canvas.Pen.Color := COLOR_OBJECT; bitmap.Canvas.Ellipse(0, 0, BITMAP_SIZE, BITMAP_SIZE); FImageList.AddMasked(bitmap, COLOR_TRANSPARENT); bitmap.Free;end; In the form's OnPaint event the image is drawn from the image list:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormPaint(Sender: TObject);begin FImageList.Draw(Canvas, 0, 0, 0);end; If I use a color other than clGrayText for transparent parts of the image then I get transparency when I draw on the canvas (first image).
But clGrayText as transparency color removes transparency (second image).
When using the GTK2 widgetset I don't see this strange effect.
Is this a bug?
zeljko:
Maybe, should be tested with pure Qt to see if that's true.
wp:
I would not work with that ancient color transparency - I always had lots of issues with it. It's better to go to pf32Bit Pixelformat and introduce a true alpha channel. However, the painting routines in the Graphics unit cannot handle this usually, and therefore it is recommended to switch to a TLazIntfImage and the TLazCanvas. Have a look at the wiki, there are several code samples: https://wiki.freepascal.org/Developing_with_Graphics#Working_with_TLazIntfImage.2C_TRawImage_and_TLazCanvas
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses FpImage, IntfGraphics, LazCanvas; procedure TForm1.FormCreate(Sender: TObject);const BITMAP_SIZE = 51; COLOR_BACKGROUND = clGreen; COLOR_OBJECT = clRed;var bitmap: TBitmap; img: TLazIntfImage; cnv: TLazCanvas;begin// Caption := LCLPlatformDisplayNames[WidgetSet.LCLPlatform]; Color := COLOR_BACKGROUND; FImageList := TImageList.Create(self); FImageList.Width := BITMAP_SIZE; FImageList.Height := BITMAP_SIZE; bitmap := TBitmap.Create; bitmap.PixelFormat := pf32bit; bitmap.SetSize(BITMAP_SIZE, BITMAP_SIZE); img := bitmap.CreateIntfImage; cnv := TLazCanvas.Create(img); // Fill the image with transparent "color" cnv.Brush.FPColor := colTransparent; cnv.FillRect(0, 0, img.Width, Img.Height); // Draw the ellipse in opaque color cnv.Brush.FPColor := TColorToFPColor(COLOR_OBJECT); // or declare COLOR_OBJECT as colRed which is the 64-bit color red in FCL-image) cnv.Pen.FPColor := TColorToFPColor(COLOR_OBJECT); cnv.Ellipse(0, 0, BITMAP_SIZE, BITMAP_SIZE); bitmap.LoadFromIntfImage(img); FImageList.Add(bitmap, nil); cnv.Free; img.Free; bitmap.Free;end; procedure TForm1.FormPaint(Sender: TObject);begin FImageList.Draw(Canvas, 0, 0, 0);end;
Valdas:
Yes, a much better approach. Thank you!
Navigation
[0] Message Index