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