1 11 package org.eclipse.ui.internal.themes; 12 13 import java.lang.reflect.Field ; 14 import java.lang.reflect.Modifier ; 15 16 import org.eclipse.jface.resource.StringConverter; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.graphics.RGB; 19 import org.eclipse.swt.widgets.Display; 20 21 26 public final class ColorUtils { 27 28 33 private static RGB process(String value) { 34 try { 35 Class clazz = SWT.class; Field [] fields = clazz.getDeclaredFields(); 37 for (int i = 0; i < fields.length; i++) { 38 Field field = fields[i]; 39 if (field.getType() == Integer.TYPE 40 && Modifier.isStatic(field.getModifiers()) 41 && Modifier.isPublic(field.getModifiers()) 42 && Modifier.isFinal(field.getModifiers())) { 43 if (value.equals(field.getName())) { 44 return getSystemColor(field.getInt(null)); 45 } 46 } 47 } 48 } catch (IllegalArgumentException e) { 49 } catch (IllegalAccessException e) { 51 } 53 return getSystemColor(SWT.COLOR_BLACK); 54 } 55 56 63 public static RGB blend(RGB val1, RGB val2) { 64 int red = blend(val1.red, val2.red); 65 int green = blend(val1.green, val2.green); 66 int blue = blend(val1.blue, val2.blue); 67 return new RGB(red, green, blue); 68 } 69 70 77 private static int blend(int temp1, int temp2) { 78 return (Math.abs(temp1 - temp2) / 2) + Math.min(temp1, temp2); 79 } 80 81 85 private static RGB getSystemColor(int colorId) { 86 return Display.getCurrent().getSystemColor(colorId).getRGB(); 87 } 88 89 95 public static RGB getColorValue(String rawValue) { 96 if (rawValue == null) 97 return null; 98 99 rawValue = rawValue.trim(); 100 101 if (!isDirectValue(rawValue)) { 102 return process(rawValue); 103 } 104 105 return StringConverter.asRGB(rawValue); 106 } 107 108 114 public static RGB[] getColorValues(String [] rawValues) { 115 RGB[] values = new RGB[rawValues.length]; 116 for (int i = 0; i < rawValues.length; i++) { 117 values[i] = getColorValue(rawValues[i]); 118 } 119 return values; 120 } 121 122 126 private static boolean isDirectValue(String rawValue) { 127 return rawValue.indexOf(',') >= 0; 128 } 129 130 133 private ColorUtils() { 134 } 136 } 137 | Popular Tags |