@@ -117,6 +117,12 @@ public final class Image extends Resource implements Drawable {
117
117
*/
118
118
private int styleFlag = SWT .IMAGE_COPY ;
119
119
120
+ /**
121
+ * Sets the color to which to map the transparent pixel.
122
+ * For further info see {@link #setBackground(Color)}
123
+ */
124
+ private RGB backgroundColor ;
125
+
120
126
/**
121
127
* Attribute to cache current native zoom level
122
128
*/
@@ -1087,6 +1093,10 @@ public boolean equals (Object object) {
1087
1093
public Color getBackground () {
1088
1094
if (isDisposed ()) SWT .error (SWT .ERROR_GRAPHIC_DISPOSED );
1089
1095
if (transparentPixel == -1 ) return null ;
1096
+ if (backgroundColor != null ) {
1097
+ // if a background color was set explicitly, we use the cached color directly
1098
+ return Color .win32_new (device , (backgroundColor .blue << 16 ) | (backgroundColor .green << 8 ) | backgroundColor .red );
1099
+ }
1090
1100
1091
1101
/* Get the HDC for the device */
1092
1102
long hDC = device .internal_new_GC (null );
@@ -1838,33 +1848,10 @@ public void setBackground(Color color) {
1838
1848
if (isDisposed ()) SWT .error (SWT .ERROR_GRAPHIC_DISPOSED );
1839
1849
if (color == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
1840
1850
if (color .isDisposed ()) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
1841
- zoomLevelToImageHandle .values ().forEach (imageHandle -> setBackground (color , imageHandle .handle ));
1842
- }
1843
-
1844
- private void setBackground (Color color , long handle ) {
1845
1851
if (transparentPixel == -1 ) return ;
1846
1852
transparentColor = -1 ;
1847
-
1848
- /* Get the HDC for the device */
1849
- long hDC = device .internal_new_GC (null );
1850
-
1851
- /* Change the background color in the image */
1852
- BITMAP bm = new BITMAP ();
1853
- OS .GetObject (handle , BITMAP .sizeof , bm );
1854
- long hdcMem = OS .CreateCompatibleDC (hDC );
1855
- OS .SelectObject (hdcMem , handle );
1856
- int maxColors = 1 << bm .bmBitsPixel ;
1857
- byte [] colors = new byte [maxColors * 4 ];
1858
- int numColors = OS .GetDIBColorTable (hdcMem , 0 , maxColors , colors );
1859
- int offset = transparentPixel * 4 ;
1860
- colors [offset ] = (byte )color .getBlue ();
1861
- colors [offset + 1 ] = (byte )color .getGreen ();
1862
- colors [offset + 2 ] = (byte )color .getRed ();
1863
- OS .SetDIBColorTable (hdcMem , 0 , numColors , colors );
1864
- OS .DeleteDC (hdcMem );
1865
-
1866
- /* Release the HDC for the device */
1867
- device .internal_dispose_GC (hDC , null );
1853
+ backgroundColor = color .getRGB ();
1854
+ zoomLevelToImageHandle .values ().forEach (imageHandle -> imageHandle .setBackground (backgroundColor ));
1868
1855
}
1869
1856
1870
1857
private int getZoom () {
@@ -2403,9 +2390,37 @@ public ImageHandle(long handle, int zoom) {
2403
2390
this .handle = handle ;
2404
2391
this .zoom = zoom ;
2405
2392
updateBoundsInPixelsFromNative ();
2393
+ if (backgroundColor != null ) {
2394
+ setBackground (backgroundColor );
2395
+ }
2406
2396
setImageMetadataForHandle (this , zoom );
2407
2397
}
2408
2398
2399
+ private void setBackground (RGB color ) {
2400
+ if (transparentPixel == -1 ) return ;
2401
+
2402
+ /* Get the HDC for the device */
2403
+ long hDC = device .internal_new_GC (null );
2404
+
2405
+ /* Change the background color in the image */
2406
+ BITMAP bm = new BITMAP ();
2407
+ OS .GetObject (handle , BITMAP .sizeof , bm );
2408
+ long hdcMem = OS .CreateCompatibleDC (hDC );
2409
+ OS .SelectObject (hdcMem , handle );
2410
+ int maxColors = 1 << bm .bmBitsPixel ;
2411
+ byte [] colors = new byte [maxColors * 4 ];
2412
+ int numColors = OS .GetDIBColorTable (hdcMem , 0 , maxColors , colors );
2413
+ int offset = transparentPixel * 4 ;
2414
+ colors [offset ] = (byte )color .blue ;
2415
+ colors [offset + 1 ] = (byte )color .green ;
2416
+ colors [offset + 2 ] = (byte )color .red ;
2417
+ OS .SetDIBColorTable (hdcMem , 0 , numColors , colors );
2418
+ OS .DeleteDC (hdcMem );
2419
+
2420
+ /* Release the HDC for the device */
2421
+ device .internal_dispose_GC (hDC , null );
2422
+ }
2423
+
2409
2424
private void updateBoundsInPixelsFromNative () {
2410
2425
switch (type ) {
2411
2426
case SWT .BITMAP :
0 commit comments