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