I have created the following methods for custom transparency handling:
1. HighlightSelectedColor(Color: TColor):
This method makes all pixels in a TImageEnView transparent except the passed color, effectively highlighting the passed color in the image while hiding all other colors
2. ResetColorsOpaque:
This method resets all colors in the image to opaque again
My question is: Are there built-in methods in ImageEn to simplify or optimize these methods?
procedure TForm1.HighlightSelectedColor(Color: TColor);
var
x, y: Integer;
IEBitmap: TIEBitmap;
AlphaBitmap: TIEBitmap;
RGBColor: TColor;
PixelColor: TRGB;
Red, Green, Blue: Byte;
AlphaScanLine: PByteArray;
begin
IEBitmap := ImageEnView1.IEBitmap; // Reference to the ImageEnView bitmap
// Ensure the bitmap has an alpha channel
if not IEBitmap.HasAlphaChannel then
IEBitmap.AlphaChannel; // Accessing AlphaChannel property will create it if it doesn't exist
AlphaBitmap := IEBitmap.AlphaChannel; // Reference the alpha channel bitmap
RGBColor := ColorToRGB(Color);
// Extract the RGB components from the selected color
Red := GetRValue(RGBColor);
Green := GetGValue(RGBColor);
Blue := GetBValue(RGBColor);
// Process each pixel to make non-selected colors transparent
for y := 0 to IEBitmap.Height - 1 do
begin
AlphaScanLine := AlphaBitmap.ScanLine[y];
for x := 0 to IEBitmap.Width - 1 do
begin
// Access the pixel color as TRGB
PixelColor := IEBitmap.Pixels_ie24RGB[x, y];
// Compare each color component
if (PixelColor.r <> Red) or (PixelColor.g <> Green) or (PixelColor.b <> Blue) then
begin
AlphaScanLine[x] := 0; // Make pixel fully transparent
end
else
begin
AlphaScanLine[x] := 255; // Make pixel fully opaque
end;
end;
end;
// Ensure the alpha channel is in sync
IEBitmap.SyncAlphaChannel;
// Update the ImageEnView
ImageEnView1.Update;
end;
procedure TForm1.ResetColorsOpaque;
var
x, y: Integer;
IEBitmap: TIEBitmap;
AlphaBitmap: TIEBitmap;
AlphaScanLine: PByteArray;
begin
IEBitmap := ImageEnView1.IEBitmap; // Reference to the ImageEnView bitmap
// Ensure the bitmap has an alpha channel
if not IEBitmap.HasAlphaChannel then
IEBitmap.AlphaChannel; // Accessing AlphaChannel property will create it if it doesn't exist
AlphaBitmap := IEBitmap.AlphaChannel; // Reference the alpha channel bitmap
// Process each pixel to make all colors fully opaque
for y := 0 to IEBitmap.Height - 1 do
begin
AlphaScanLine := AlphaBitmap.ScanLine[y];
for x := 0 to IEBitmap.Width - 1 do
begin
AlphaScanLine[x] := 255; // Make pixel fully opaque
end;
end;
// Ensure the alpha channel is in sync
IEBitmap.SyncAlphaChannel;
// Update the ImageEnView
ImageEnView1.Update;
end;