Mercurial > projects > dwt2
comparison org.eclipse.jface.text/src/org/eclipse/jface/internal/text/revisions/Colors.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2006, 2007 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.jface.internal.text.revisions.Colors; | |
14 | |
15 import org.eclipse.jface.internal.text.revisions.HunkComputer; // packageimport | |
16 import org.eclipse.jface.internal.text.revisions.LineIndexOutOfBoundsException; // packageimport | |
17 import org.eclipse.jface.internal.text.revisions.Hunk; // packageimport | |
18 import org.eclipse.jface.internal.text.revisions.ChangeRegion; // packageimport | |
19 import org.eclipse.jface.internal.text.revisions.Range; // packageimport | |
20 import org.eclipse.jface.internal.text.revisions.RevisionPainter; // packageimport | |
21 import org.eclipse.jface.internal.text.revisions.RevisionSelectionProvider; // packageimport | |
22 | |
23 | |
24 import java.lang.all; | |
25 import java.util.Set; | |
26 | |
27 | |
28 import org.eclipse.swt.SWT; | |
29 import org.eclipse.swt.graphics.RGB; | |
30 import org.eclipse.core.runtime.Assert; | |
31 | |
32 /** | |
33 * Utility for color operations. | |
34 * | |
35 * @since 3.3 | |
36 */ | |
37 public final class Colors { | |
38 /* | |
39 * Implementation note: Color computation assumes sRGB, which is probably not true, and does not | |
40 * always give good results. CIE based algorithms would be better, see | |
41 * http://www.w3.org/TR/PNG-ColorAppendix.html and http://en.wikipedia.org/wiki/Lab_color_space | |
42 */ | |
43 | |
44 /** | |
45 * Returns the human-perceived brightness of a color as float in [0.0, 1.0]. The used RGB | |
46 * weights come from http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC9. | |
47 * | |
48 * @param rgb the color | |
49 * @return the gray-scale value | |
50 */ | |
51 public static float brightness(RGB rgb) { | |
52 return Math.min(1f, (0.2126f * rgb.red + 0.7152f * rgb.green + 0.0722f * rgb.blue + 0.5f) / 255f); | |
53 } | |
54 | |
55 /** | |
56 * Normalizes a color in its perceived brightness. Yellows are darkened, while blues and reds | |
57 * are lightened. Depending on the hue, the brightness range within the RGB gamut may be | |
58 * different, outside values are clipped. Note that this is an approximation; the returned RGB | |
59 * is not guaranteed to have the requested {@link #brightness(RGB) brightness}. | |
60 * | |
61 * @param color the color to normalize | |
62 * @param brightness the requested brightness, in [0, 1] | |
63 * @return a normalized version of <code>color</code> | |
64 * @see #brightness(RGB) | |
65 */ | |
66 public static RGB adjustBrightness(RGB color, float brightness_) { | |
67 float[] hsi= toHSI(color); | |
68 float psychoFactor= brightness_ - brightness(color); | |
69 float weight= 0.5f; // found by trial and error | |
70 hsi[2]= Math.max(0, Math.min(1.0f, hsi[2] + psychoFactor * weight)); | |
71 color= fromHSI(hsi); | |
72 return color; | |
73 } | |
74 | |
75 /** | |
76 * Converts an {@link RGB} to an <a href="http://en.wikipedia.org/wiki/HSL_color_space">HSI</a> | |
77 * triplet. | |
78 * | |
79 * @param color the color to convert | |
80 * @return the HSI float array of length 3 | |
81 */ | |
82 private static float[] toHSI(RGB color) { | |
83 float r = color.red / 255f; | |
84 float g = color.green / 255f; | |
85 float b = color.blue / 255f; | |
86 float max = Math.max(Math.max(r, g), b); | |
87 float min = Math.min(Math.min(r, g), b); | |
88 float delta = max - min; | |
89 float maxPlusMin= max + min; | |
90 float intensity = maxPlusMin / 2; | |
91 float saturation= intensity < 0.5 ? delta / maxPlusMin : delta / (2 - maxPlusMin); | |
92 | |
93 float hue = 0; | |
94 if (delta !is 0) { | |
95 if (r is max) { | |
96 hue = (g - b) / delta; | |
97 } else { | |
98 if (g is max) { | |
99 hue = 2 + (b - r) / delta; | |
100 } else { | |
101 hue = 4 + (r - g) / delta; | |
102 } | |
103 } | |
104 hue *= 60; | |
105 if (hue < 0) hue += 360; | |
106 } | |
107 return [hue, saturation, intensity]; | |
108 } | |
109 | |
110 /** | |
111 * Converts a <a href="http://en.wikipedia.org/wiki/HSL_color_space">HSI</a> triplet to an RGB. | |
112 * | |
113 * @param hsi the HSI values | |
114 * @return the RGB corresponding to the HSI spec | |
115 */ | |
116 private static RGB fromHSI(float[] hsi) { | |
117 float r, g, b; | |
118 float hue= hsi[0]; | |
119 float saturation= hsi[1]; | |
120 float intensity= hsi[2]; | |
121 if (saturation is 0) { | |
122 r = g = b = intensity; | |
123 } else { | |
124 float temp2= intensity < 0.5f ? intensity * (1.0f + saturation) : (intensity + saturation) - (intensity * saturation); | |
125 float temp1= 2f * intensity - temp2; | |
126 if (hue is 360) hue = 0; | |
127 hue /= 360; | |
128 | |
129 r= hue2RGB(temp1, temp2, hue + 1f/3f); | |
130 g= hue2RGB(temp1, temp2, hue); | |
131 b= hue2RGB(temp1, temp2, hue - 1f/3f); | |
132 } | |
133 | |
134 int red = cast(int)(r * 255 + 0.5); | |
135 int green = cast(int)(g * 255 + 0.5); | |
136 int blue = cast(int)(b * 255 + 0.5); | |
137 return new RGB(red, green, blue); | |
138 } | |
139 | |
140 private static float hue2RGB(float t1, float t2, float hue) { | |
141 if (hue < 0) | |
142 hue += 1; | |
143 else if (hue > 1) | |
144 hue -= 1; | |
145 if (6f * hue < 1) | |
146 return t1 +(t2 - t1) * 6f * hue; | |
147 if (2f * hue < 1) | |
148 return t2; | |
149 if (3f * hue < 2) | |
150 return t1 + (t2 - t1) * (2f/3f - hue) * 6f; | |
151 return t1; | |
152 } | |
153 | |
154 /** | |
155 * Returns an RGB that lies between the given foreground and background | |
156 * colors using the given mixing factor. A <code>factor</code> of 1.0 will produce a | |
157 * color equal to <code>fg</code>, while a <code>factor</code> of 0.0 will produce one | |
158 * equal to <code>bg</code>. | |
159 * @param bg the background color | |
160 * @param fg the foreground color | |
161 * @param factor the mixing factor, must be in [0, 1] | |
162 * | |
163 * @return the interpolated color | |
164 */ | |
165 public static RGB blend(RGB bg, RGB fg, float factor) { | |
166 Assert.isLegal(bg !is null); | |
167 Assert.isLegal(fg !is null); | |
168 Assert.isLegal(factor >= 0f && factor <= 1f); | |
169 | |
170 float complement= 1f - factor; | |
171 return new RGB( | |
172 cast(int) (complement * bg.red + factor * fg.red), | |
173 cast(int) (complement * bg.green + factor * fg.green), | |
174 cast(int) (complement * bg.blue + factor * fg.blue) | |
175 ); | |
176 } | |
177 | |
178 /** | |
179 * Returns an array of colors in a smooth palette from <code>start</code> to <code>end</code>. | |
180 * <p> | |
181 * The returned array has size <code>steps</code>, and the color at index 0 is <code>start</code>, the color | |
182 * at index <code>steps - 1</code> is <code>end</code>. | |
183 * | |
184 * @param start the start color of the palette | |
185 * @param end the end color of the palette | |
186 * @param steps the requested size, must be > 0 | |
187 * @return an array of <code>steps</code> colors in the palette from <code>start</code> to <code>end</code> | |
188 */ | |
189 public static RGB[] palette(RGB start, RGB end, int steps) { | |
190 Assert.isLegal(start !is null); | |
191 Assert.isLegal(end !is null); | |
192 Assert.isLegal(steps > 0); | |
193 | |
194 if (steps is 1) | |
195 return [ start ]; | |
196 | |
197 float step= 1.0f / (steps - 1); | |
198 RGB[] gradient= new RGB[steps]; | |
199 for (int i= 0; i < steps; i++) | |
200 gradient[i]= blend(start, end, step * i); | |
201 | |
202 return gradient; | |
203 } | |
204 | |
205 /** | |
206 * Returns an array of colors with hues evenly distributed on the hue wheel defined by the <a | |
207 * href="http://en.wikipedia.org/wiki/HSV_color_space">HSB color space</a>. The returned array | |
208 * has size <code>steps</code>. The distance <var>d</var> between two successive colors is | |
209 * in [120°, 180°]. | |
210 * <p> | |
211 * The color at a given <code>index</code> has the hue returned by | |
212 * {@linkplain #computeHue(int) computeHue(index)}; i.e. the computed hues are not equidistant, | |
213 * but adaptively distributed on the color wheel. | |
214 * </p> | |
215 * <p> | |
216 * The first six colors returned correspond to the following {@link SWT} color constants: | |
217 * {@link SWT#COLOR_RED red}, {@link SWT#COLOR_GREEN green}, {@link SWT#COLOR_BLUE blue}, | |
218 * {@link SWT#COLOR_YELLOW yellow}, {@link SWT#COLOR_CYAN cyan}, | |
219 * {@link SWT#COLOR_MAGENTA magenta}. | |
220 * </p> | |
221 * | |
222 * @param steps the requested size, must be >= 2 | |
223 * @return an array of <code>steps</code> colors evenly distributed on the color wheel | |
224 */ | |
225 public static RGB[] rainbow(int steps) { | |
226 Assert.isLegal(steps >= 2); | |
227 | |
228 RGB[] rainbow= new RGB[steps]; | |
229 for (int i= 0; i < steps; i++) | |
230 rainbow[i]= new RGB(computeHue(i), 1f, 1f); | |
231 | |
232 return rainbow; | |
233 } | |
234 | |
235 /** | |
236 * Returns an indexed hue in [0°, 360°), distributing the hues evenly on the hue wheel | |
237 * defined by the <a href="http://en.wikipedia.org/wiki/HSV_color_space">HSB (or HSV) color | |
238 * space</a>. The distance <var>d</var> between two successive colors is in [120°, 180°]. | |
239 * <p> | |
240 * The first six colors returned correspond to the following {@link SWT} color constants: | |
241 * {@link SWT#COLOR_RED red}, {@link SWT#COLOR_GREEN green}, {@link SWT#COLOR_BLUE blue}, | |
242 * {@link SWT#COLOR_YELLOW yellow}, {@link SWT#COLOR_CYAN cyan}, | |
243 * {@link SWT#COLOR_MAGENTA magenta}. | |
244 * </p> | |
245 * | |
246 * @param index the index of the color, must be >= 0 | |
247 * @return a color hue in [0°, 360°) | |
248 * @see RGB#RGB(float, float, float) | |
249 */ | |
250 public static float computeHue(int index) { | |
251 Assert.isLegal(index >= 0); | |
252 /* | |
253 * Base 3 gives a nice partitioning for RGB colors with red, green, blue being the colors | |
254 * 0,1,2, and yellow, cyan, magenta colors 3,4,5. | |
255 */ | |
256 final int base= 3; | |
257 final float range= 360f; | |
258 | |
259 // partition the baseRange by using the least significant bit to select one half of the | |
260 // partitioning | |
261 int baseIndex= index / base; | |
262 float baseRange= range / base; | |
263 float baseOffset= 0f; | |
264 while (baseIndex > 0) { | |
265 baseRange /= 2; | |
266 int lsb= baseIndex % 2; | |
267 baseOffset += lsb * baseRange; | |
268 baseIndex >>= 1; | |
269 } | |
270 | |
271 final int baseMod= index % base; | |
272 final float hue= baseOffset + baseMod * range / base; | |
273 Assert.isTrue(hue >= 0 && hue < 360); | |
274 return hue; | |
275 } | |
276 | |
277 private this() { | |
278 // not instantiatable | |
279 } | |
280 | |
281 } |