001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.designer;
042:
043: import java.awt.Color;
044:
045: import org.netbeans.modules.visualweb.css2.PageBox;
046:
047: // For CVS archaeology: Most of the code in this file used to be in SelectionManager.java
048:
049: /**
050: * This class manages color selections for the designer surface
051: * @todo This file needs some cleanup after being split out from SelectionManager.
052: *
053: * @author Tor Norbye
054: */
055: public class ColorManager {
056: private WebForm webform;
057:
058: /** Last pagebox we've analyzed and chosen colors for */
059: private PageBox pageBox;
060: public Color insertColor;
061: public Color selectionColor;
062: public Color selectionColorReverse;
063: public Color selectionBoundsColor;
064: public Color primaryColor;
065: public Color dropTargetColor;
066: public Color hierarchyForegroundColor;
067: public Color hierarchyBackgroundColor;
068: public Color draggerColor;
069: public Color draggerColorBorder;
070: public Color resizerColor;
071: public Color resizerColorBorder;
072: public Color marqueeColor;
073: public Color marqueeColorBorder;
074: public Color gridColor;
075:
076: /** Creates a new instance of ColorManager */
077: public ColorManager(WebForm webform) {
078: this .webform = webform;
079: }
080:
081: public void resetPageBox() {
082: pageBox = null;
083: }
084:
085: public void sync() {
086: DesignerPane pane = webform.getPane();
087:
088: if ((pane != null) && (pane.getPageBox() != pageBox)) {
089: pageBox = pane.getPageBox();
090: initializeColors();
091: }
092: }
093:
094: private void initializeColors() {
095: PageBox pb = webform.getPane().getPageBox();
096:
097: if ((pb != null) && pb.isDarkBackground()) {
098: setDarkBackgroundColors();
099: } else {
100: setLightBackgroundColors();
101: }
102: }
103:
104: /** @todo Make colors user-configurable. Better yet, look them
105: * up from the UA stylesheet, default.css!
106: */
107: private void setLightBackgroundColors() {
108: insertColor = Color.LIGHT_GRAY;
109: gridColor = Color.GRAY;
110: selectionColor = Color.BLACK;
111: selectionColorReverse = Color.WHITE;
112: selectionBoundsColor = Color.DARK_GRAY;
113:
114: //primaryColor = Color.lightGray;
115: primaryColor = Color.YELLOW;
116: dropTargetColor = Color.BLUE;
117: draggerColor = new Color(0, 0, 0, 30);
118: draggerColorBorder = new Color(0, 0, 0, 100);
119: marqueeColor = draggerColor;
120: marqueeColorBorder = draggerColorBorder;
121: resizerColor = draggerColor;
122: resizerColorBorder = draggerColorBorder;
123:
124: if (SelectionManager.PAINT_SELECTION_HIERARCHY) {
125: hierarchyForegroundColor = Color.BLACK;
126: hierarchyBackgroundColor = new Color(200, 200, 200, 128);
127: }
128: }
129:
130: /** @todo Make colors user-configurable. Better yet, look them
131: * up from the UA stylesheet, default.css!
132: */
133: private void setDarkBackgroundColors() {
134: insertColor = Color.LIGHT_GRAY;
135: gridColor = Color.GRAY;
136: selectionColor = Color.WHITE;
137: selectionColorReverse = Color.BLACK;
138: selectionBoundsColor = Color.DARK_GRAY;
139: primaryColor = Color.YELLOW;
140: dropTargetColor = Color.BLUE;
141: draggerColor = new Color(200, 200, 200, 30);
142: draggerColorBorder = new Color(200, 200, 200, 100);
143: marqueeColor = draggerColor;
144: marqueeColorBorder = draggerColorBorder;
145: resizerColor = draggerColor;
146: resizerColorBorder = draggerColorBorder;
147:
148: if (SelectionManager.PAINT_SELECTION_HIERARCHY) {
149: hierarchyForegroundColor = Color.WHITE;
150: hierarchyBackgroundColor = new Color(200, 200, 200, 128);
151: }
152: }
153:
154: /** Check whether the given color is "dark".
155: * @todo Move to ColorManager
156: */
157: public static boolean isDark(Color color) {
158: // Decide whether this color is "light" or "dark".
159: // Can't just do a simple "(r+g+b)/3 > 128" check since for
160: // example saturated yellow is six times brighter than saturated blue.
161: // So compute the luminance. I found various different luminance
162: // formulas but they were similar enough that any one would be
163: // close enough for my purposes:
164: float luminance255 = // no point dividing by 255 to normalize to 1.0
165: (0.27f * color.getRed()) + (0.67f * color.getGreen())
166: + (0.06f * color.getBlue());
167:
168: // Could split at 50% (128) but since this method is used to
169: // decide when to switch to reverse video, go a little bit
170: // darker since visually it looks better with dark selection
171: // handles for example and it's light enough a bit further down
172: // the scale - let's go down to 25% before we hit reverse video
173: return luminance255 < 64.0f;
174: }
175: }
|