1 11 package org.eclipse.ui.themes; 12 13 import java.lang.reflect.Field ; 14 import java.lang.reflect.Modifier ; 15 import java.util.ArrayList ; 16 17 import org.eclipse.jface.resource.StringConverter; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.graphics.RGB; 20 import org.eclipse.swt.widgets.Display; 21 22 28 public final class ColorUtil { 29 30 private static Field [] cachedFields; 31 32 40 private static RGB process(String value) { 41 Field [] fields = getFields(); 42 try { 43 for (int i = 0; i < fields.length; i++) { 44 Field field = fields[i]; 45 if (field.getName().equals(value)) { 46 return getSystemColor(field.getInt(null)); 47 } 48 } 49 } catch (IllegalArgumentException e) { 50 } catch (IllegalAccessException e) { 53 } 56 return getSystemColor(SWT.COLOR_BLACK); 57 } 58 59 65 private static Field [] getFields() { 66 if (cachedFields == null) { 67 Class clazz = SWT.class; 68 Field [] allFields = clazz.getDeclaredFields(); 69 ArrayList applicableFields = new ArrayList (allFields.length); 70 71 for (int i = 0; i < allFields.length; i++) { 72 Field field = allFields[i]; 73 if (field.getType() == Integer.TYPE 74 && Modifier.isStatic(field.getModifiers()) 75 && Modifier.isPublic(field.getModifiers()) 76 && Modifier.isFinal(field.getModifiers()) 77 && field.getName().startsWith("COLOR")) { 79 applicableFields.add(field); 80 } 81 } 82 cachedFields = (Field []) applicableFields.toArray(new Field [applicableFields.size()]); 83 } 84 return cachedFields; 85 } 86 87 100 public static RGB blend(RGB c1, RGB c2, int ratio) { 101 int r = blend(c1.red, c2.red, ratio); 102 int g = blend(c1.green, c2.green, ratio); 103 int b = blend(c1.blue, c2.blue, ratio); 104 return new RGB(r, g, b); 105 } 106 107 private static int blend(int v1, int v2, int ratio) { 108 int b = (ratio * v1 + (100 - ratio) * v2) / 100; 109 return Math.min(255, b); 110 } 111 112 122 public static RGB blend(RGB val1, RGB val2) { 123 int red = blend(val1.red, val2.red); 124 int green = blend(val1.green, val2.green); 125 int blue = blend(val1.blue, val2.blue); 126 return new RGB(red, green, blue); 127 } 128 129 139 private static int blend(int temp1, int temp2) { 140 return (Math.abs(temp1 - temp2) / 2) + Math.min(temp1, temp2); 141 } 142 143 150 private static RGB getSystemColor(int colorId) { 151 return Display.getCurrent().getSystemColor(colorId).getRGB(); 152 } 153 154 161 public static RGB getColorValue(String rawValue) { 162 if (rawValue == null) { 163 return null; 164 } 165 166 rawValue = rawValue.trim(); 167 168 if (!isDirectValue(rawValue)) { 169 return process(rawValue); 170 } 171 172 return StringConverter.asRGB(rawValue); 173 } 174 175 182 public static RGB[] getColorValues(String [] rawValues) { 183 RGB[] values = new RGB[rawValues.length]; 184 for (int i = 0; i < rawValues.length; i++) { 185 values[i] = getColorValue(rawValues[i]); 186 } 187 return values; 188 } 189 190 197 private static boolean isDirectValue(String rawValue) { 198 return rawValue.indexOf(',') >= 0; 199 } 200 201 204 private ColorUtil() { 205 } 207 } 208 | Popular Tags |