connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/mylyn/NotificationPopupColors.java
changeset 1104 e84724c7f393
equal deleted inserted replaced
1103:a5d7a2345c4a 1104:e84724c7f393
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2004, 2009 Tasktop Technologies and others.
       
     3  * All rights reserved. This program and the accompanying materials
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  *
       
     8  * Contributors:
       
     9  *     Benjamin Pasero - initial API and implementation
       
    10  *     Tasktop Technologies - improvements
       
    11  *******************************************************************************/
       
    12 
       
    13 package com.nokia.carbide.remoteconnections.internal.ui.mylyn;
       
    14 
       
    15 import org.eclipse.jface.resource.DeviceResourceException;
       
    16 import org.eclipse.jface.resource.ResourceManager;
       
    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 /**
       
    23  * Helper Class to create the colors for the {@link AbstractNotificationPopup}.
       
    24  * <p>
       
    25  * Note: Copied from FormColors of UI Forms.
       
    26  * </p>
       
    27  * 
       
    28  * @author Benjamin Pasero (initial contribution from RSSOwl, see bug 177974)
       
    29  * @author Mik Kersten
       
    30  */
       
    31 public class NotificationPopupColors {
       
    32 
       
    33 	private final Display display;
       
    34 
       
    35 	private Color titleText;
       
    36 
       
    37 	private Color gradientBegin;
       
    38 
       
    39 	private Color gradientEnd;
       
    40 
       
    41 	private Color border;
       
    42 
       
    43 	private final ResourceManager resourceManager;
       
    44 
       
    45 	public NotificationPopupColors(Display display, ResourceManager resourceManager) {
       
    46 		this.display = display;
       
    47 		this.resourceManager = resourceManager;
       
    48 
       
    49 		createColors();
       
    50 	}
       
    51 
       
    52 	private void createColors() {
       
    53 		createBorderColor();
       
    54 		createGradientColors();
       
    55 		// previously used SWT.COLOR_TITLE_INACTIVE_FOREGROUND, but too light on Windows XP
       
    56 		titleText = getColor(resourceManager, getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
       
    57 	}
       
    58 
       
    59 	public Color getGradientBegin() {
       
    60 		return gradientBegin;
       
    61 	}
       
    62 
       
    63 	public Color getGradientEnd() {
       
    64 		return gradientEnd;
       
    65 	}
       
    66 
       
    67 	public Color getBorder() {
       
    68 		return border;
       
    69 	}
       
    70 
       
    71 	public Color getTitleText() {
       
    72 		return titleText;
       
    73 	}
       
    74 
       
    75 	private void createBorderColor() {
       
    76 		RGB tbBorder = getSystemColor(SWT.COLOR_TITLE_BACKGROUND);
       
    77 		RGB bg = getImpliedBackground().getRGB();
       
    78 
       
    79 		// Group 1
       
    80 		// Rule: If at least 2 of the RGB values are equal to or between 180 and
       
    81 		// 255, then apply specified opacity for Group 1
       
    82 		// Examples: Vista, XP Silver, Wn High Con #2
       
    83 		// Keyline = TITLE_BACKGROUND @ 70% Opacity over LIST_BACKGROUND
       
    84 		if (testTwoPrimaryColors(tbBorder, 179, 256)) {
       
    85 			tbBorder = blend(tbBorder, bg, 70);
       
    86 		} else if (testTwoPrimaryColors(tbBorder, 120, 180)) {
       
    87 			tbBorder = blend(tbBorder, bg, 50);
       
    88 		} else {
       
    89 			tbBorder = blend(tbBorder, bg, 30);
       
    90 		}
       
    91 
       
    92 		border = getColor(resourceManager, tbBorder);
       
    93 	}
       
    94 
       
    95 	private void createGradientColors() {
       
    96 		RGB titleBg = getSystemColor(SWT.COLOR_TITLE_BACKGROUND);
       
    97 		Color bgColor = getImpliedBackground();
       
    98 		RGB bg = bgColor.getRGB();
       
    99 		RGB bottom, top;
       
   100 
       
   101 		// Group 1
       
   102 		// Rule: If at least 2 of the RGB values are equal to or between 180 and
       
   103 		// 255, then apply specified opacity for Group 1
       
   104 		// Examples: Vista, XP Silver, Wn High Con #2
       
   105 		// Gradient Bottom = TITLE_BACKGROUND @ 30% Opacity over LIST_BACKGROUND
       
   106 		// Gradient Top = TITLE BACKGROUND @ 0% Opacity over LIST_BACKGROUND
       
   107 		if (testTwoPrimaryColors(titleBg, 179, 256)) {
       
   108 			bottom = blend(titleBg, bg, 30);
       
   109 			top = bg;
       
   110 		}
       
   111 
       
   112 		// Group 2
       
   113 		// Rule: If at least 2 of the RGB values are equal to or between 121 and
       
   114 		// 179, then apply specified opacity for Group 2
       
   115 		// Examples: XP Olive, OSX Graphite, Linux GTK, Wn High Con Black
       
   116 		// Gradient Bottom = TITLE_BACKGROUND @ 20% Opacity over LIST_BACKGROUND
       
   117 		// Gradient Top = TITLE BACKGROUND @ 0% Opacity over LIST_BACKGROUND
       
   118 		else if (testTwoPrimaryColors(titleBg, 120, 180)) {
       
   119 			bottom = blend(titleBg, bg, 20);
       
   120 			top = bg;
       
   121 		}
       
   122 
       
   123 		// Group 3
       
   124 		// Rule: If at least 2 of the RGB values are equal to or between 0 and
       
   125 		// 120, then apply specified opacity for Group 3
       
   126 		// Examples: XP Default, Wn Classic Standard, Wn Marine, Wn Plum, OSX
       
   127 		// Aqua, Wn High Con White, Wn High Con #1
       
   128 		// Gradient Bottom = TITLE_BACKGROUND @ 10% Opacity over LIST_BACKGROUND
       
   129 		// Gradient Top = TITLE BACKGROUND @ 0% Opacity over LIST_BACKGROUND
       
   130 		else {
       
   131 			bottom = blend(titleBg, bg, 10);
       
   132 			top = bg;
       
   133 		}
       
   134 
       
   135 		gradientBegin = getColor(resourceManager, top);
       
   136 		gradientEnd = getColor(resourceManager, bottom);
       
   137 	}
       
   138 
       
   139 	private RGB blend(RGB c1, RGB c2, int ratio) {
       
   140 		int r = blend(c1.red, c2.red, ratio);
       
   141 		int g = blend(c1.green, c2.green, ratio);
       
   142 		int b = blend(c1.blue, c2.blue, ratio);
       
   143 		return new RGB(r, g, b);
       
   144 	}
       
   145 
       
   146 	private int blend(int v1, int v2, int ratio) {
       
   147 		int b = (ratio * v1 + (100 - ratio) * v2) / 100;
       
   148 		return Math.min(255, b);
       
   149 	}
       
   150 
       
   151 	private boolean testTwoPrimaryColors(RGB rgb, int from, int to) {
       
   152 		int total = 0;
       
   153 		if (testPrimaryColor(rgb.red, from, to)) {
       
   154 			total++;
       
   155 		}
       
   156 		if (testPrimaryColor(rgb.green, from, to)) {
       
   157 			total++;
       
   158 		}
       
   159 		if (testPrimaryColor(rgb.blue, from, to)) {
       
   160 			total++;
       
   161 		}
       
   162 		return total >= 2;
       
   163 	}
       
   164 
       
   165 	private boolean testPrimaryColor(int value, int from, int to) {
       
   166 		return value > from && value < to;
       
   167 	}
       
   168 
       
   169 	private RGB getSystemColor(int code) {
       
   170 		return getDisplay().getSystemColor(code).getRGB();
       
   171 	}
       
   172 
       
   173 	private Color getImpliedBackground() {
       
   174 		return display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
       
   175 	}
       
   176 
       
   177 	private Display getDisplay() {
       
   178 		return display;
       
   179 	}
       
   180 
       
   181 	private Color getColor(ResourceManager manager, RGB rgb) {
       
   182 		try {
       
   183 			return manager.createColor(rgb);
       
   184 		} catch (DeviceResourceException e) {
       
   185 			return manager.getDevice().getSystemColor(SWT.COLOR_BLACK);
       
   186 		}
       
   187 	}
       
   188 }