- Thank you very much
As in most cases it matters what are you trying to do. Well, getting the specific problem “in the vacuum” there is a “metadata” in a PNG file that allows to treat a certain color as transparent. I know Castle Engine treats this flag correctly but I’m not sure if it’s possible to set it without significant modifications to the source PNG. So, the most “reliable” way to is by scanning the pixels of the image and assigning them into transparent color. However, note, that this may not be the desired solution as it takes time - and if you have a lot of those images, especially if the images are large, you would get much better results by providing “game-ready” images (e.g. by performing this operation in GIMP or other graphic editor and saving the result) in your game data, without the necessity for additional processing.
If you still want to process them on the Engine side, then something like that should do:
type
TBackgroundColorToTransparent = class helper for TRgbAlphaImage
public
procedure BackgroundColorToTransparent(const BackgroundColor: TVector3Byte);
end;
procedure TBackgroundColorToTransparent .BackgroundColorToTransparent(const BackgroundColor: TVector3Byte);
var
Src: PVector4Byte;
I: SizeInt;
begin
Src := Pixels;
for I := 0 to Pred(Width * Height) do
begin
if (Src^.X = BackgroundColor.X) and (Src^.Y = BackgroundColor.Y) and (Src^.Z = BackgroundColor.Z) then
Src^.W := 0; // if matches the BackgroundColor, make transparent
Inc(Src);
end;
end;
And then use it something like:
MyImage := LoadImage('castle-data:/myimage.png', [TRgbAlphaImage]) as TRgbAlphaImage;
MyImage.BackgroundColorToTransparent(Vector3Byte(0, 0, 0));
- Your method above is very useful but I have another problem and let me describe the problem
- Here’s my code:
Image:=LoadImage(MemoryStream,‘image/bmp’, [TRGBAlphaImage]);
TRgbAlphaImage(Image).BackgroundColorToTransparent(Vector3Byte(0, 0, 0));
DestImage:=TDrawableImage.Create(Image, true, true);
ServerSetView.DrawableImage := DestImage; - Above I successfully processed the transparent color
- Next I add children to the TCastleImageControl above:
Image:=LoadImage(MemoryStream,‘image/bmp’, [TRGBAlphaImage]);
TRgbAlphaImage(Image).BackgroundColorToTransparent(Vector3Byte(0, 0, 0));
DestImage:=TDrawableImage.Create(Image, true, true);
ImageControl1.DrawableImage:=DestImage; - The above code also handles transparent colors but it does not make black transparent or black can you test it the way I did or is there any solution?
- Hope it works out. Thank you very much
- I am very sorry that I did not pay attention to the background color of the picture, some of which is (8,0,0). The problem does not exist because I did not pay attention to the trouble. Thank you again for your help