diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java index 2c04c23902f..4ecc29fbcb6 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java @@ -14,6 +14,8 @@ package org.eclipse.swt.graphics; +import static org.eclipse.swt.internal.image.ImageColorTransformer.DEFAULT_DISABLED_IMAGE_TRANSFORMER; + import java.io.*; import java.util.*; import java.util.function.*; @@ -435,12 +437,14 @@ private void createRepFromSourceAndApplyFlag(NSBitmapImageRep srcRep, int srcWid long data = rep.bitmapData(); C.memmove(data, srcData, srcWidth * srcHeight * 4); if (flag != SWT.IMAGE_COPY) { - final int redOffset, greenOffset, blueOffset; + final int redOffset, greenOffset, blueOffset, alphaOffset; if (srcBpp == 32 && (srcBitmapFormat & OS.NSAlphaFirstBitmapFormat) == 0) { redOffset = 0; greenOffset = 1; blueOffset = 2; + alphaOffset = 3; } else { + alphaOffset = 0; redOffset = 1; greenOffset = 2; blueOffset = 3; @@ -448,16 +452,6 @@ private void createRepFromSourceAndApplyFlag(NSBitmapImageRep srcRep, int srcWid /* Apply transformation */ switch (flag) { case SWT.IMAGE_DISABLE: { - Color zeroColor = this.device.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW); - RGB zeroRGB = zeroColor.getRGB(); - byte zeroRed = (byte)zeroRGB.red; - byte zeroGreen = (byte)zeroRGB.green; - byte zeroBlue = (byte)zeroRGB.blue; - Color oneColor = this.device.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); - RGB oneRGB = oneColor.getRGB(); - byte oneRed = (byte)oneRGB.red; - byte oneGreen = (byte)oneRGB.green; - byte oneBlue = (byte)oneRGB.blue; byte[] line = new byte[(int)srcBpr]; for (int y=0; y ImageColorTransformer.forRGB(0.5f, 0.5f, 0.5f, 0.5f); + case IMAGE_DISABLEMENT_ALGORITHM_DESATURATED -> ImageColorTransformer.forHSB(1.0f, 0.2f, 0.9f, 0.5f); + default -> ImageColorTransformer.forGrayscaledContrastBrightness(0.2f, 2.9f); + }; + + RGBA adaptPixelValue(int red, int green, int blue, int alpha); + + public static ImageColorTransformer forHSB(float hueFactor, float saturationFactor, float brightnessFactor, + float alphaFactor) { + return (red, green, blue, alpha) -> { + float[] hsba = new RGBA(red, green, blue, alpha).getHSBA(); + float hue = Math.min(hueFactor * hsba[0], 1.0f); + float saturation = Math.min(saturationFactor * hsba[1], 1.0f); + float brightness = Math.min(brightnessFactor * hsba[2], 1.0f); + float alphaResult = Math.min(alphaFactor * hsba[3], 255.0f); + return new RGBA(hue, saturation, brightness, alphaResult); + }; + } + + public static ImageColorTransformer forRGB(float redFactor, float greenFactor, float blueFactor, + float alphaFactor) { + return (red, green, blue, alpha) -> { + int redResult = (int) Math.min(redFactor * red, 255.0f); + int greenResult = (int) Math.min(greenFactor * green, 255.0f); + int blueResult = (int) Math.min(blueFactor * blue, 255.0f); + int alphaResult = (int) Math.min(alphaFactor * alpha, 255.0f); + return new RGBA(redResult, greenResult, blueResult, alphaResult); + }; + } + + public static ImageColorTransformer forIntensityThreshold(Device device) { + RGBA lowIntensity = device.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW).getRGBA(); + RGBA highIntensity = device.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGBA(); + return (red, green, blue, alpha) -> { + int intensity = red * red + green * green + blue * blue; + RGBA usedGraytone = intensity < 98304 ? lowIntensity : highIntensity; + return new RGBA(usedGraytone.rgb.red, usedGraytone.rgb.green, usedGraytone.rgb.blue, alpha); + }; + } + + public static ImageColorTransformer forGrayscaledContrastBrightness(float contrast, float brightness) { + return (red, green, blue, alpha) -> { + int grayValue = Math.min((77 * red + 151 * green + 28 * blue) / 255, 255); + int resultValue = (int) Math.min(Math.max((contrast * (grayValue * brightness - 128) + 128), 0), 255); + return new RGBA(resultValue, resultValue, resultValue, alpha); + }; + } + +} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java index a21871a55b7..dd8581c1348 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java @@ -14,6 +14,8 @@ package org.eclipse.swt.graphics; +import static org.eclipse.swt.internal.image.ImageColorTransformer.DEFAULT_DISABLED_IMAGE_TRANSFORMER; + import java.io.*; import java.util.*; @@ -307,15 +309,30 @@ public Image(Device device, Image srcImage, int flag) { byte[] line = new byte[stride]; for (int y=0; y