[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[platform-swt-dev] Color mask issues with PalleteData
|
Hi all,
I have noticied "strange" behavior with SWT PalleteData and different color Depths.
With a 16 bit-color display, PalleteData uses a mask with 5 bit per color and the representation orientation is RGB, from most significant bit to less significant bit. Also, the not used bits are all at the left.
-------------------------------------------------
Image depth: 16
Direct pallete: true
-
Black: 00000000000000000000000000000000
Red: 00000000000000000
111110000000000
Green: 00000000000000000000001111100000
Blue: 00000000000000000000000000011111
White: 00000000000000000111111111111111
-------------------------------------------------
But with a 32 bit-color display, PalleteData uses a mask with 8 bit per color and representation orientation is BGR, from most significant bit to less significant bit. And here, the not user bits are all at the right!
-------------------------------------------------
Image depth: 32
Direct pallete: true
-
Black: 00000000000000000000000000000000
Red: 0000000000000000
1111111100000000
Green: 00000000111111110000000000000000
Blue: 11111111000000000000000000000000
White: 11111111111111111111111100000000
-------------------------------------------------
The question is: there is any good reason to color-bits orientation changing from a color-depth to another?!
The code snippet used to generate this report is attached (i using SWT 3.2 / JDK 1.5).
Thanks for any explanation,
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ColorTest {
final static Display display = Display.getDefault();
public static void main(String args[]) {
new ColorTest();
}
Image lastImg = null;
final static int stripHeight = 30;
final static int stripWidth = 150;
final Color[] colors = new Color[] {
display.getSystemColor(SWT.COLOR_BLACK),
display.getSystemColor(SWT.COLOR_RED),
display.getSystemColor(SWT.COLOR_GREEN),
display.getSystemColor(SWT.COLOR_BLUE),
display.getSystemColor(SWT.COLOR_WHITE)
};
final String[] colorNames = new String[] { "Black", "Red", "Green", "Blue", "White" };
public ColorTest() {
Shell shell = new Shell(display, SWT.TOOL | SWT.DIALOG_TRIM);
shell.setText("Color Test");
shell.setSize(stripWidth, colors.length * stripHeight + (shell.getSize().y - shell.getClientArea().height));
shell.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
if (lastImg == null) {
lastImg = generateImage();
debugImageColors();
}
e.gc.drawImage(lastImg, 0, 0);
}
});
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
if (lastImg != null)
lastImg.dispose();
}
});
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
protected Image generateImage() {
Image img = new Image(display, stripWidth, colors.length * stripHeight);
int y = 0;
GC gc = new GC(img);
for (Color c : colors) {
gc.setBackground(c);
gc.fillRectangle(0, y, stripWidth, stripHeight);
y += stripHeight;
}
gc.dispose();
return img;
}
private void debugImageColors() {
int y = 0;
System.out.println("Image depth: " + lastImg.getImageData().depth);
System.out.println("Direct pallete: " + lastImg.getImageData().palette.isDirect);
System.out.println("-");
for (String colorName : colorNames) {
int pixelColor = lastImg.getImageData().getPixel(0, y);
StringBuilder representation = new StringBuilder();
representation.append(Integer.toBinaryString(pixelColor));
int appendLen = (32 - representation.length());
for (int i = 0; i < appendLen; i++)
representation.insert(0, "0");
System.out.println(String.format("%10s: %32s", new Object[] { colorName, representation.toString() }));
y += stripHeight;
}
}
}