1 11 package org.eclipse.ui.forms; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.graphics.Color; 19 import org.eclipse.swt.graphics.RGB; 20 import org.eclipse.swt.widgets.Display; 21 22 30 public class FormColors { 31 36 public static final String TITLE = IFormColors.TITLE; 37 38 43 public static final String BORDER = IFormColors.BORDER; 44 45 50 public static final String SEPARATOR = IFormColors.SEPARATOR; 51 52 57 public static final String TB_BG = IFormColors.TB_BG; 58 59 64 public static final String TB_FG = IFormColors.TB_FG; 65 66 71 public static final String TB_GBG = IFormColors.TB_GBG; 72 73 78 public static final String TB_BORDER = IFormColors.TB_BORDER; 79 80 86 public static final String TB_TOGGLE = IFormColors.TB_TOGGLE; 87 88 94 public static final String TB_TOGGLE_HOVER = IFormColors.TB_TOGGLE_HOVER; 95 96 protected Map colorRegistry = new HashMap (10); 97 98 protected Color background; 99 100 protected Color foreground; 101 102 private boolean shared; 103 104 protected Display display; 105 106 protected Color border; 107 108 114 public FormColors(Display display) { 115 this.display = display; 116 initialize(); 117 } 118 119 124 public Display getDisplay() { 125 return display; 126 } 127 128 135 protected void initialize() { 136 background = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND); 137 foreground = display.getSystemColor(SWT.COLOR_LIST_FOREGROUND); 138 initializeColorTable(); 139 updateBorderColor(); 140 } 141 142 146 protected void initializeColorTable() { 147 createTitleColor(); 148 createColor(IFormColors.SEPARATOR, getColor(IFormColors.TITLE).getRGB()); 149 RGB black = getSystemColor(SWT.COLOR_BLACK); 150 RGB borderRGB = getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT); 151 createColor(IFormColors.BORDER, blend(borderRGB, black, 80)); 152 } 153 154 162 public void initializeSectionToolBarColors() { 163 if (colorRegistry.containsKey(IFormColors.TB_BG)) 164 return; 165 createTitleBarGradientColors(); 166 createTitleBarOutlineColors(); 167 createTwistieColors(); 168 } 169 170 181 protected void initializeFormHeaderColors() { 182 if (colorRegistry.containsKey(IFormColors.H_BOTTOM_KEYLINE2)) 183 return; 184 createFormHeaderColors(); 185 } 186 187 196 public RGB getSystemColor(int code) { 197 return getDisplay().getSystemColor(code).getRGB(); 198 } 199 200 211 public Color createColor(String key, RGB rgb) { 212 return createColor(key, rgb.red, rgb.green, rgb.blue); 213 } 214 215 226 public Color getInactiveBackground() { 227 String key = "__ncbg__"; Color color = getColor(key); 229 if (color == null) { 230 RGB sel = getSystemColor(SWT.COLOR_LIST_SELECTION); 231 RGB ncbg = blend(sel, getSystemColor(SWT.COLOR_WHITE), 5); 233 color = createColor(key, ncbg); 234 } 235 return color; 236 } 237 238 255 public Color createColor(String key, int r, int g, int b) { 256 Color c = new Color(display, r, g, b); 257 Color prevC = (Color) colorRegistry.get(key); 258 if (prevC != null) 259 prevC.dispose(); 260 colorRegistry.put(key, c); 261 return c; 262 } 263 264 269 protected void updateBorderColor() { 270 if (isWhiteBackground()) 271 border = getColor(IFormColors.BORDER); 272 else { 273 border = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 274 Color bg = getImpliedBackground(); 275 if (border.getRed() == bg.getRed() 276 && border.getGreen() == bg.getGreen() 277 && border.getBlue() == bg.getBlue()) 278 border = display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW); 279 } 280 } 281 282 289 public void setBackground(Color bg) { 290 this.background = bg; 291 updateBorderColor(); 292 updateFormHeaderColors(); 293 } 294 295 302 public void setForeground(Color fg) { 303 this.foreground = fg; 304 } 305 306 311 public Color getBackground() { 312 return background; 313 } 314 315 320 public Color getForeground() { 321 return foreground; 322 } 323 324 330 public Color getBorderColor() { 331 return border; 332 } 333 334 341 public boolean isWhiteBackground() { 342 Color bg = getImpliedBackground(); 343 return bg.getRed() == 255 && bg.getGreen() == 255 344 && bg.getBlue() == 255; 345 } 346 347 355 public Color getColor(String key) { 356 if (key.startsWith(IFormColors.TB_PREFIX)) 357 initializeSectionToolBarColors(); 358 else if (key.startsWith(IFormColors.H_PREFIX)) 359 initializeFormHeaderColors(); 360 return (Color) colorRegistry.get(key); 361 } 362 363 366 public void dispose() { 367 Iterator e = colorRegistry.values().iterator(); 368 while (e.hasNext()) 369 ((Color) e.next()).dispose(); 370 colorRegistry = null; 371 } 372 373 377 public void markShared() { 378 this.shared = true; 379 } 380 381 386 public boolean isShared() { 387 return shared; 388 } 389 390 402 public static RGB blend(RGB c1, RGB c2, int ratio) { 403 int r = blend(c1.red, c2.red, ratio); 404 int g = blend(c1.green, c2.green, ratio); 405 int b = blend(c1.blue, c2.blue, ratio); 406 return new RGB(r, g, b); 407 } 408 409 423 public static boolean testAnyPrimaryColor(RGB rgb, int from, int to) { 424 if (testPrimaryColor(rgb.red, from, to)) 425 return true; 426 if (testPrimaryColor(rgb.green, from, to)) 427 return true; 428 if (testPrimaryColor(rgb.blue, from, to)) 429 return true; 430 return false; 431 } 432 433 447 public static boolean testTwoPrimaryColors(RGB rgb, int from, int to) { 448 int total = 0; 449 if (testPrimaryColor(rgb.red, from, to)) 450 total++; 451 if (testPrimaryColor(rgb.green, from, to)) 452 total++; 453 if (testPrimaryColor(rgb.blue, from, to)) 454 total++; 455 return total >= 2; 456 } 457 458 469 private static int blend(int v1, int v2, int ratio) { 470 int b = (ratio * v1 + (100 - ratio) * v2) / 100; 471 return Math.min(255, b); 472 } 473 474 private Color getImpliedBackground() { 475 if (getBackground() != null) 476 return getBackground(); 477 return getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 478 } 479 480 private static boolean testPrimaryColor(int value, int from, int to) { 481 return value > from && value < to; 482 } 483 484 private void createTitleColor() { 485 491 RGB bg = getImpliedBackground().getRGB(); 492 RGB listSelection = getSystemColor(SWT.COLOR_LIST_SELECTION); 493 RGB listForeground = getSystemColor(SWT.COLOR_LIST_FOREGROUND); 494 RGB rgb = listSelection; 495 496 if (testTwoPrimaryColors(listSelection, -1, 121)) 503 rgb = listSelection; 504 else if (testTwoPrimaryColors(listSelection, 120, 256) 514 || (bg.red == 0 && bg.green == 0 && bg.blue == 0)) 515 rgb = blend(listSelection, listForeground, 50); 516 createColor(IFormColors.TITLE, rgb); 524 } 525 526 private void createTwistieColors() { 527 RGB rgb = getColor(IFormColors.TITLE).getRGB(); 528 RGB white = getSystemColor(SWT.COLOR_WHITE); 529 createColor(TB_TOGGLE, rgb); 530 rgb = blend(rgb, white, 60); 531 createColor(TB_TOGGLE_HOVER, rgb); 532 } 533 534 private void createTitleBarGradientColors() { 535 RGB tbBg = getSystemColor(SWT.COLOR_TITLE_BACKGROUND); 536 RGB bg = getImpliedBackground().getRGB(); 537 538 if (testTwoPrimaryColors(tbBg, 179, 256)) 545 tbBg = blend(tbBg, bg, 30); 546 547 else if (testTwoPrimaryColors(tbBg, 120, 180)) 554 tbBg = blend(tbBg, bg, 20); 555 556 else { 563 tbBg = blend(tbBg, bg, 10); 564 } 565 566 createColor(IFormColors.TB_BG, tbBg); 567 568 createColor(TB_GBG, tbBg); 570 } 571 572 private void createTitleBarOutlineColors() { 573 RGB tbBorder = getSystemColor(SWT.COLOR_TITLE_BACKGROUND); 575 RGB bg = getImpliedBackground().getRGB(); 576 if (testTwoPrimaryColors(tbBorder, 179, 256)) 582 tbBorder = blend(tbBorder, bg, 70); 583 584 589 else if (testTwoPrimaryColors(tbBorder, 120, 180)) 591 tbBorder = blend(tbBorder, bg, 50); 592 593 598 else { 600 tbBorder = blend(tbBorder, bg, 30); 601 } 602 createColor(FormColors.TB_BORDER, tbBorder); 603 } 604 605 private void updateFormHeaderColors() { 606 if (colorRegistry.containsKey(IFormColors.H_GRADIENT_END)) { 607 disposeIfFound(IFormColors.H_GRADIENT_END); 608 disposeIfFound(IFormColors.H_GRADIENT_START); 609 disposeIfFound(IFormColors.H_BOTTOM_KEYLINE1); 610 disposeIfFound(IFormColors.H_BOTTOM_KEYLINE2); 611 disposeIfFound(IFormColors.H_HOVER_LIGHT); 612 disposeIfFound(IFormColors.H_HOVER_FULL); 613 initializeFormHeaderColors(); 614 } 615 } 616 617 private void disposeIfFound(String key) { 618 Color color = getColor(key); 619 if (color != null) { 620 colorRegistry.remove(key); 621 color.dispose(); 622 } 623 } 624 625 private void createFormHeaderColors() { 626 createFormHeaderGradientColors(); 627 createFormHeaderKeylineColors(); 628 createFormHeaderDNDColors(); 629 } 630 631 private void createFormHeaderGradientColors() { 632 RGB titleBg = getSystemColor(SWT.COLOR_TITLE_BACKGROUND); 633 Color bgColor = getImpliedBackground(); 634 RGB bg = bgColor.getRGB(); 635 RGB bottom, top; 636 if (testTwoPrimaryColors(titleBg, 179, 256)) { 643 bottom = blend(titleBg, bg, 30); 644 top = bg; 645 } 646 647 else if (testTwoPrimaryColors(titleBg, 120, 180)) { 654 bottom = blend(titleBg, bg, 20); 655 top = bg; 656 } 657 658 else { 666 bottom = blend(titleBg, bg, 10); 667 top = bg; 668 } 669 createColor(IFormColors.H_GRADIENT_END, top); 670 createColor(IFormColors.H_GRADIENT_START, bottom); 671 } 672 673 private void createFormHeaderKeylineColors() { 674 RGB titleBg = getSystemColor(SWT.COLOR_TITLE_BACKGROUND); 675 Color bgColor = getImpliedBackground(); 676 RGB bg = bgColor.getRGB(); 677 RGB keyline2; 678 createColor(IFormColors.H_BOTTOM_KEYLINE1, new RGB(255, 255, 255)); 680 681 if (testTwoPrimaryColors(titleBg, 179, 256)) 688 keyline2 = blend(titleBg, bg, 70); 689 690 else if (testTwoPrimaryColors(titleBg, 120, 180)) 696 keyline2 = blend(titleBg, bg, 50); 697 698 704 else 706 keyline2 = blend(titleBg, bg, 30); 707 createColor(IFormColors.H_BOTTOM_KEYLINE2, keyline2); 709 } 710 711 private void createFormHeaderDNDColors() { 712 RGB titleBg = getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT); 713 Color bgColor = getImpliedBackground(); 714 RGB bg = bgColor.getRGB(); 715 RGB light, full; 716 light = blend(titleBg, bg, 40); 723 full = blend(titleBg, bg, 60); 728 createColor(IFormColors.H_HOVER_LIGHT, light); 731 createColor(IFormColors.H_HOVER_FULL, full); 732 } 733 } 734 | Popular Tags |