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