1 11 package org.eclipse.jface.internal.text.revisions; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.graphics.RGB; 17 18 23 public final class Colors { 24 29 30 37 public static float brightness(RGB rgb) { 38 return Math.min(1f, (0.2126f * rgb.red + 0.7152f * rgb.green + 0.0722f * rgb.blue + 0.5f) / 255f); 39 } 40 41 52 public static RGB adjustBrightness(RGB color, float brightness) { 53 float[] hsi= toHSI(color); 54 float psychoFactor= brightness - brightness(color); 55 float weight= 0.5f; hsi[2]= Math.max(0, Math.min(1.0f, hsi[2] + psychoFactor * weight)); 57 color= fromHSI(hsi); 58 return color; 59 } 60 61 68 private static float[] toHSI(RGB color) { 69 float r = color.red / 255f; 70 float g = color.green / 255f; 71 float b = color.blue / 255f; 72 float max = Math.max(Math.max(r, g), b); 73 float min = Math.min(Math.min(r, g), b); 74 float delta = max - min; 75 float maxPlusMin= max + min; 76 float intensity = maxPlusMin / 2; 77 float saturation= intensity < 0.5 ? delta / maxPlusMin : delta / (2 - maxPlusMin); 78 79 float hue = 0; 80 if (delta != 0) { 81 if (r == max) { 82 hue = (g - b) / delta; 83 } else { 84 if (g == max) { 85 hue = 2 + (b - r) / delta; 86 } else { 87 hue = 4 + (r - g) / delta; 88 } 89 } 90 hue *= 60; 91 if (hue < 0) hue += 360; 92 } 93 return new float[] {hue, saturation, intensity}; 94 } 95 96 102 private static RGB fromHSI(float[] hsi) { 103 float r, g, b; 104 float hue= hsi[0]; 105 float saturation= hsi[1]; 106 float intensity= hsi[2]; 107 if (saturation == 0) { 108 r = g = b = intensity; 109 } else { 110 float temp2= intensity < 0.5f ? intensity * (1.0f + saturation) : (intensity + saturation) - (intensity * saturation); 111 float temp1= 2f * intensity - temp2; 112 if (hue == 360) hue = 0; 113 hue /= 360; 114 115 r= hue2RGB(temp1, temp2, hue + 1f/3f); 116 g= hue2RGB(temp1, temp2, hue); 117 b= hue2RGB(temp1, temp2, hue - 1f/3f); 118 } 119 120 int red = (int)(r * 255 + 0.5); 121 int green = (int)(g * 255 + 0.5); 122 int blue = (int)(b * 255 + 0.5); 123 return new RGB(red, green, blue); 124 } 125 126 private static float hue2RGB(float t1, float t2, float hue) { 127 if (hue < 0) 128 hue += 1; 129 else if (hue > 1) 130 hue -= 1; 131 if (6f * hue < 1) 132 return t1 +(t2 - t1) * 6f * hue; 133 if (2f * hue < 1) 134 return t2; 135 if (3f * hue < 2) 136 return t1 + (t2 - t1) * (2f/3f - hue) * 6f; 137 return t1; 138 } 139 140 151 public static RGB blend(RGB bg, RGB fg, float factor) { 152 Assert.isLegal(bg != null); 153 Assert.isLegal(fg != null); 154 Assert.isLegal(factor >= 0f && factor <= 1f); 155 156 float complement= 1f - factor; 157 return new RGB( 158 (int) (complement * bg.red + factor * fg.red), 159 (int) (complement * bg.green + factor * fg.green), 160 (int) (complement * bg.blue + factor * fg.blue) 161 ); 162 } 163 164 175 public static RGB[] palette(RGB start, RGB end, int steps) { 176 Assert.isLegal(start != null); 177 Assert.isLegal(end != null); 178 Assert.isLegal(steps > 0); 179 180 if (steps == 1) 181 return new RGB[] { start }; 182 183 float step= 1.0f / (steps - 1); 184 RGB[] gradient= new RGB[steps]; 185 for (int i= 0; i < steps; i++) 186 gradient[i]= blend(start, end, step * i); 187 188 return gradient; 189 } 190 191 211 public static RGB[] rainbow(int steps) { 212 Assert.isLegal(steps >= 2); 213 214 RGB[] rainbow= new RGB[steps]; 215 for (int i= 0; i < steps; i++) 216 rainbow[i]= new RGB(computeHue(i), 1f, 1f); 217 218 return rainbow; 219 } 220 221 236 public static float computeHue(final int index) { 237 Assert.isLegal(index >= 0); 238 242 final int base= 3; 243 final float range= 360f; 244 245 int baseIndex= index / base; 248 float baseRange= range / base; 249 float baseOffset= 0f; 250 while (baseIndex > 0) { 251 baseRange /= 2; 252 int lsb= baseIndex % 2; 253 baseOffset += lsb * baseRange; 254 baseIndex >>= 1; 255 } 256 257 final int baseMod= index % base; 258 final float hue= baseOffset + baseMod * range / base; 259 Assert.isTrue(hue >= 0 && hue < 360); 260 return hue; 261 } 262 263 private Colors() { 264 } 266 267 } 268 | Popular Tags |