1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet208" 5 dependency "dwt" path="../../../../../../" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "glib-2.0" \ 14 "gmodule-2.0" \ 15 "gnomeui-2" \ 16 "gnomevfs-2" \ 17 "gobject-2.0" \ 18 "gthread-2.0" \ 19 "gtk-x11-2.0" \ 20 "pango-1.0" \ 21 "pangocairo-1.0" \ 22 "X11" \ 23 "Xcomposite" \ 24 "Xcursor" \ 25 "Xdamage" \ 26 "Xext" \ 27 "Xfixes" \ 28 "Xi" \ 29 "Xinerama" \ 30 "Xrandr" \ 31 "Xrender" \ 32 "Xtst" \ 33 platform="linux" 34 +/ 35 36 /******************************************************************************* 37 * Copyright (c) 2000, 2005 IBM Corporation and others. 38 * All rights reserved. This program and the accompanying materials 39 * are made available under the terms of the Eclipse Public License v1.0 40 * which accompanies this distribution, and is available at 41 * http://www.eclipse.org/legal/epl-v10.html 42 * 43 * Contributors: 44 * IBM Corporation - initial API and implementation 45 * D Port: 46 * Thomas Demmer <t_demmer AT web DOT de> 47 *******************************************************************************/ 48 module org.eclipse.swt.snippets.Snippet208; 49 50 /* 51 * Change hue, saturation and brightness of a color 52 * 53 * For a list of all SWT example snippets see 54 * http://www.eclipse.org/swt/snippets/ 55 * 56 * @since 3.2 57 */ 58 import org.eclipse.swt.SWT; 59 import org.eclipse.swt.graphics.Image; 60 import org.eclipse.swt.graphics.ImageData; 61 import org.eclipse.swt.graphics.RGB; 62 import org.eclipse.swt.graphics.PaletteData; 63 import org.eclipse.swt.layout.GridData; 64 import org.eclipse.swt.layout.GridLayout; 65 import org.eclipse.swt.widgets.Display; 66 import org.eclipse.swt.widgets.Label; 67 import org.eclipse.swt.widgets.Shell; 68 69 import java.lang.all; 70 71 void main () { 72 PaletteData palette = new PaletteData(0xff, 0xff00, 0xff0000); 73 74 // ImageData showing variations of hue 75 ImageData hueData = new ImageData(360, 100, 24, palette); 76 float hue = 0; 77 for (int x = 0; x < hueData.width; x++) { 78 for (int y = 0; y < hueData.height; y++) { 79 int pixel = palette.getPixel(new RGB(hue, 1f, 1f)); 80 hueData.setPixel(x, y, pixel); 81 } 82 hue += 360f / hueData.width; 83 } 84 85 // ImageData showing saturation on x axis and brightness on y axis 86 ImageData saturationBrightnessData = new ImageData(360, 360, 24, palette); 87 float saturation = 0f; 88 float brightness = 1f; 89 for (int x = 0; x < saturationBrightnessData.width; x++) { 90 brightness = 1f; 91 for (int y = 0; y < saturationBrightnessData.height; y++) { 92 int pixel = palette.getPixel(new RGB(360f, saturation, brightness)); 93 saturationBrightnessData.setPixel(x, y, pixel); 94 brightness -= 1f / saturationBrightnessData.height; 95 } 96 saturation += 1f / saturationBrightnessData.width; 97 } 98 99 Display display = new Display(); 100 Image hueImage = new Image(display, hueData); 101 Image saturationImage = new Image(display, saturationBrightnessData); 102 Shell shell = new Shell(display); 103 shell.setText("Hue, Saturation, Brightness"); 104 GridLayout gridLayout = new GridLayout(2, false); 105 gridLayout.verticalSpacing = 10; 106 gridLayout.marginWidth = gridLayout.marginHeight = 16; 107 shell.setLayout(gridLayout); 108 109 Label label = new Label(shell, SWT.CENTER); 110 label.setImage(hueImage); 111 GridData data = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 2, 1); 112 label.setLayoutData(data); 113 114 label = new Label(shell, SWT.CENTER); //spacer 115 label = new Label(shell, SWT.CENTER); 116 label.setText("Hue"); 117 data = new GridData(SWT.CENTER, SWT.CENTER, false, false); 118 label.setLayoutData(data); 119 label = new Label(shell, SWT.CENTER); //spacer 120 data = new GridData(SWT.CENTER, SWT.CENTER, false, false, 2, 1); 121 label.setLayoutData(data); 122 123 label = new Label(shell, SWT.LEFT); 124 label.setText("Brightness"); 125 data = new GridData(SWT.LEFT, SWT.CENTER, false, false); 126 label.setLayoutData(data); 127 128 label = new Label(shell, SWT.CENTER); 129 label.setImage(saturationImage); 130 data = new GridData(SWT.CENTER, SWT.CENTER, false, false); 131 label.setLayoutData (data); 132 133 label = new Label(shell, SWT.CENTER); //spacer 134 label = new Label(shell, SWT.CENTER); 135 label.setText("Saturation"); 136 data = new GridData(SWT.CENTER, SWT.CENTER, false, false); 137 label.setLayoutData(data); 138 139 shell.pack(); 140 shell.open(); 141 while (!shell.isDisposed()) { 142 if (!display.readAndDispatch()) { 143 display.sleep(); 144 } 145 } 146 hueImage.dispose(); 147 saturationImage.dispose(); 148 display.dispose(); 149 }