117
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * D Port:
|
|
11 * Thomas Demmer <t_demmer AT web DOT de>
|
|
12 *******************************************************************************/
|
|
13 module color.Snippet208;
|
|
14
|
|
15 /*
|
|
16 * Change hue, saturation and brightness of a color
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 *
|
|
21 * @since 3.2
|
|
22 */
|
|
23 import dwt.DWT;
|
|
24 import dwt.graphics.Image;
|
|
25 import dwt.graphics.ImageData;
|
|
26 import dwt.graphics.RGB;
|
|
27 import dwt.graphics.PaletteData;
|
|
28 import dwt.layout.GridData;
|
|
29 import dwt.layout.GridLayout;
|
|
30 import dwt.widgets.Display;
|
|
31 import dwt.widgets.Label;
|
|
32 import dwt.widgets.Shell;
|
|
33
|
|
34 import dwt.dwthelper.utils;
|
|
35
|
|
36 void main (String [] args) {
|
|
37 PaletteData palette = new PaletteData(0xff, 0xff00, 0xff0000);
|
|
38
|
|
39 // ImageData showing variations of hue
|
|
40 ImageData hueData = new ImageData(360, 100, 24, palette);
|
|
41 float hue = 0;
|
|
42 for (int x = 0; x < hueData.width; x++) {
|
|
43 for (int y = 0; y < hueData.height; y++) {
|
|
44 int pixel = palette.getPixel(new RGB(hue, 1f, 1f));
|
|
45 hueData.setPixel(x, y, pixel);
|
|
46 }
|
|
47 hue += 360f / hueData.width;
|
|
48 }
|
|
49
|
|
50 // ImageData showing saturation on x axis and brightness on y axis
|
|
51 ImageData saturationBrightnessData = new ImageData(360, 360, 24, palette);
|
|
52 float saturation = 0f;
|
|
53 float brightness = 1f;
|
|
54 for (int x = 0; x < saturationBrightnessData.width; x++) {
|
|
55 brightness = 1f;
|
|
56 for (int y = 0; y < saturationBrightnessData.height; y++) {
|
|
57 int pixel = palette.getPixel(new RGB(360f, saturation, brightness));
|
|
58 saturationBrightnessData.setPixel(x, y, pixel);
|
|
59 brightness -= 1f / saturationBrightnessData.height;
|
|
60 }
|
|
61 saturation += 1f / saturationBrightnessData.width;
|
|
62 }
|
|
63
|
|
64 Display display = new Display();
|
|
65 Image hueImage = new Image(display, hueData);
|
|
66 Image saturationImage = new Image(display, saturationBrightnessData);
|
|
67 Shell shell = new Shell(display);
|
|
68 shell.setText("Hue, Saturation, Brightness");
|
|
69 GridLayout gridLayout = new GridLayout(2, false);
|
|
70 gridLayout.verticalSpacing = 10;
|
|
71 gridLayout.marginWidth = gridLayout.marginHeight = 16;
|
|
72 shell.setLayout(gridLayout);
|
|
73
|
|
74 Label label = new Label(shell, DWT.CENTER);
|
|
75 label.setImage(hueImage);
|
|
76 GridData data = new GridData(DWT.RIGHT, DWT.CENTER, false, false, 2, 1);
|
|
77 label.setLayoutData(data);
|
|
78
|
|
79 label = new Label(shell, DWT.CENTER); //spacer
|
|
80 label = new Label(shell, DWT.CENTER);
|
|
81 label.setText("Hue");
|
|
82 data = new GridData(DWT.CENTER, DWT.CENTER, false, false);
|
|
83 label.setLayoutData(data);
|
|
84 label = new Label(shell, DWT.CENTER); //spacer
|
|
85 data = new GridData(DWT.CENTER, DWT.CENTER, false, false, 2, 1);
|
|
86 label.setLayoutData(data);
|
|
87
|
|
88 label = new Label(shell, DWT.LEFT);
|
|
89 label.setText("Brightness");
|
|
90 data = new GridData(DWT.LEFT, DWT.CENTER, false, false);
|
|
91 label.setLayoutData(data);
|
|
92
|
|
93 label = new Label(shell, DWT.CENTER);
|
|
94 label.setImage(saturationImage);
|
|
95 data = new GridData(DWT.CENTER, DWT.CENTER, false, false);
|
|
96 label.setLayoutData (data);
|
|
97
|
|
98 label = new Label(shell, DWT.CENTER); //spacer
|
|
99 label = new Label(shell, DWT.CENTER);
|
|
100 label.setText("Saturation");
|
|
101 data = new GridData(DWT.CENTER, DWT.CENTER, false, false);
|
|
102 label.setLayoutData(data);
|
|
103
|
|
104 shell.pack();
|
|
105 shell.open();
|
|
106 while (!shell.isDisposed()) {
|
|
107 if (!display.readAndDispatch()) {
|
|
108 display.sleep();
|
|
109 }
|
|
110 }
|
|
111 hueImage.dispose();
|
|
112 saturationImage.dispose();
|
|
113 display.dispose();
|
|
114 }
|