1 11 package org.eclipse.ui.internal.themes; 12 13 import java.util.Hashtable ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.core.runtime.IExecutableExtension; 18 import org.eclipse.swt.graphics.RGB; 19 import org.eclipse.ui.themes.ColorUtil; 20 import org.eclipse.ui.themes.IColorFactory; 21 22 52 public class RGBContrastFactory implements IColorFactory, IExecutableExtension { 53 private String fg, bg1, bg2; 54 55 62 double voltage_to_intensity_srgb(double val) { 63 64 if (val < 0.0) { 65 return 0.0; 66 } 67 if (val > 1.0) { 68 return 1.0; 69 } 70 71 if (val <= 0.04045) { 72 return val / 12.92; 73 } 74 return Math.pow((val + 0.055) / 1.055, 2.4); 75 } 76 77 84 double lightness(RGB color) { 85 double r = voltage_to_intensity_srgb(color.red / 255.0); 86 double g = voltage_to_intensity_srgb(color.green / 255.0); 87 double b = voltage_to_intensity_srgb(color.blue / 255.0); 88 double l = (0.3139 * r) + (0.6395 * g) + (0.0466 * b); 89 double m = (0.1516 * r) + (0.7482 * g) + (0.1000 * b); 90 double s = (0.0177 * r) + (0.1095 * g) + (0.8729 * b); 91 double lp, mp, sp; 92 93 if (l < 0.0) { 94 lp = -Math.pow(-l, 0.43); 95 } else { 96 lp = Math.pow(l, 0.43); 97 } 98 if (m < 0.0) { 99 mp = -Math.pow(-m, 0.43); 100 } else { 101 mp = Math.pow(m, 0.43); 102 } 103 if (s < 0.0) { 104 sp = -Math.pow(-s, 0.43); 105 } else { 106 sp = Math.pow(s, 0.43); 107 } 108 109 return (0.4000 * lp) + (0.4000 * mp) + (0.2000 * sp); 110 } 111 112 public RGB createColor() { 113 117 RGB cfg, cbg1, cbg2; 118 119 if (fg != null) { 120 cfg = ColorUtil.getColorValue(fg); 121 } else { 122 cfg = new RGB(255, 255, 255); 123 } 124 if (bg1 != null) { 125 cbg1 = ColorUtil.getColorValue(bg1); 126 } else { 127 cbg1 = new RGB(0, 0, 0); 128 } 129 if (bg2 != null) { 130 cbg2 = ColorUtil.getColorValue(bg2); 131 } else { 132 cbg2 = new RGB(0, 0, 0); 133 } 134 135 double lfg = lightness(cfg); 136 double lbg1 = lightness(cbg1); 137 double lbg2 = lightness(cbg2); 138 139 if (Math.abs(lbg1 - lfg) > Math.abs(lbg2 - lfg)) { 140 return cbg1; 141 } else { 142 return cbg2; 143 } 144 } 145 146 156 public void setInitializationData(IConfigurationElement config, 157 String propertyName, Object data) throws CoreException { 158 if (data instanceof Hashtable ) { 159 Hashtable table = (Hashtable ) data; 160 fg = (String ) table.get("foreground"); bg1 = (String ) table.get("background1"); bg2 = (String ) table.get("background2"); } 164 165 } 166 } 167 | Popular Tags |