001: package com.jidesoft.plaf.xerto;
002:
003: import com.jidesoft.plaf.UIDefaultsLookup;
004:
005: import javax.swing.*;
006: import java.awt.*;
007:
008: /**
009: * XertoUtils
010: *
011: * @author Created by Jasper Potts (17-Mar-2005)
012: */
013: public class XertoUtils {
014: // =================================================================================================================
015: // Xerto Color Methods
016:
017: private static final Color BASE_COLOR;
018: private static final Color CONTROL_COLOR;
019: private static final Color LIGHT_CONTROL_COLOR;
020: private static final Color MID_CONTROL_COLOR;
021: private static final Color CONTROL_VERY_LIGHT_SHADOW_COLOR;
022: private static final Color CONTROL_LIGHT_SHADOW_COLOR;
023: private static final Color CONTROL_MID_SHADOW_COLOR;
024: private static final Color CONTROL_DARK_SHADOW_COLOR;
025:
026: private static final Color SELECTION_COLOR;
027: private static final Color INACTIVE_CAPTION_COLOR;
028: private static final Color SELECTED_TAB_BACKGROUND_COLOR;
029: private static final Color TAB_FORGROUND_COLOR;
030: private static final Color FRAME_ACTIVE_TITLE_TOP_COLOR;
031: private static final Color FRAME_ACTIVE_TITLE_BOTTOM_COLOR;
032: private static final Color FRAME_INACTIVE_TITLE_TOP_COLOR;
033: private static final Color FRAME_INACTIVE_TITLE_BOTTOM_COLOR;
034:
035: static {
036: UIDefaults uiDefaults = UIManager.getDefaults();
037: BASE_COLOR = uiDefaults.getColor("activeCaption");
038: CONTROL_COLOR = uiDefaults.getColor("control");
039: INACTIVE_CAPTION_COLOR = uiDefaults.getColor("inactiveCaption");
040: LIGHT_CONTROL_COLOR = uiDefaults
041: .getColor("MenuItem.background");
042: // get hsb base colors
043: float[] oControlHSB = Color
044: .RGBtoHSB(CONTROL_COLOR.getRed(), CONTROL_COLOR
045: .getGreen(), CONTROL_COLOR.getBlue(), null);
046: float[] oBaseHSB = Color.RGBtoHSB(BASE_COLOR.getRed(),
047: BASE_COLOR.getGreen(), BASE_COLOR.getBlue(), null);
048: // calculate colors
049: MID_CONTROL_COLOR = Color.getHSBColor(oControlHSB[0], Math.max(
050: oControlHSB[1] - 0.05f, 0),
051: (oControlHSB[2] < 0.95f) ? oControlHSB[2] + 0.05f
052: : 0.98f);
053: SELECTED_TAB_BACKGROUND_COLOR = Color.getHSBColor(
054: oControlHSB[0], oControlHSB[1],
055: (oControlHSB[1] == 0) ? 0.75f : 0.85f);
056: TAB_FORGROUND_COLOR = Color.getHSBColor(oControlHSB[0],
057: (oControlHSB[1] > 0.01) ? 0.45f : oControlHSB[0], 0.2f);
058: CONTROL_VERY_LIGHT_SHADOW_COLOR = Color.getHSBColor(
059: oControlHSB[0], oControlHSB[1], oControlHSB[2] - 0.02f);
060: CONTROL_LIGHT_SHADOW_COLOR = Color.getHSBColor(oControlHSB[0],
061: oControlHSB[1], oControlHSB[2] - 0.06f);
062: CONTROL_MID_SHADOW_COLOR = Color.getHSBColor(oControlHSB[0],
063: oControlHSB[1], oControlHSB[2] - 0.16f);
064: CONTROL_DARK_SHADOW_COLOR = Color.getHSBColor(oControlHSB[0],
065: oControlHSB[1], oControlHSB[2] - 0.32f);
066: SELECTION_COLOR = Color.getHSBColor(oBaseHSB[0],
067: (oBaseHSB[1] > 0.01) ? 0.45f : oBaseHSB[0], 0.80f);
068: FRAME_ACTIVE_TITLE_TOP_COLOR = Color.getHSBColor(oBaseHSB[0],
069: (oBaseHSB[1] > 0.01) ? 0.3f : oBaseHSB[0], 0.90f);
070: FRAME_ACTIVE_TITLE_BOTTOM_COLOR = Color.getHSBColor(
071: oBaseHSB[0],
072: (oBaseHSB[1] > 0.01) ? 0.45f : oBaseHSB[0], 0.70f);
073: FRAME_INACTIVE_TITLE_TOP_COLOR = Color.getHSBColor(
074: oControlHSB[0], oControlHSB[1], 0.75f);
075: FRAME_INACTIVE_TITLE_BOTTOM_COLOR = Color.getHSBColor(
076: oControlHSB[0], oControlHSB[1], 0.5f);
077: }
078:
079: public static Color getBaseColor() {
080: return BASE_COLOR;
081: }
082:
083: public static Color getInActiveCaptionColor() {
084: return INACTIVE_CAPTION_COLOR;
085: }
086:
087: public static Color getControlColor() {
088: return CONTROL_COLOR;
089: }
090:
091: public static Color getMidControlColor() {
092: return MID_CONTROL_COLOR;
093: }
094:
095: public static Color getLightControlColor() {
096: return LIGHT_CONTROL_COLOR;
097: }
098:
099: public static Color getSelectedTabBackgroundColor() {
100: return SELECTED_TAB_BACKGROUND_COLOR;
101: }
102:
103: public static Color getTabForgroundColor() {
104: return TAB_FORGROUND_COLOR;
105: }
106:
107: public static Color getControlVeryLightShadowColor() {
108: return CONTROL_VERY_LIGHT_SHADOW_COLOR;
109: }
110:
111: public static Color getControlLightShadowColor() {
112: return CONTROL_LIGHT_SHADOW_COLOR;
113: }
114:
115: public static Color getControlMidShadowColor() {
116: return CONTROL_MID_SHADOW_COLOR;
117: }
118:
119: public static Color getControlDarkShadowColor() {
120: return CONTROL_DARK_SHADOW_COLOR;
121: }
122:
123: public static Color getSelectionColor() {
124: return SELECTION_COLOR;
125: }
126:
127: public static Color getApplicationFrameBackgroundColor() {
128: return getControlColor();
129: }
130:
131: public static Color getFrameBorderColor() {
132: return UIDefaultsLookup.getColor("controlShadow");
133: }
134:
135: public static Color getFrameActiveTitleTopColor() {
136: return FRAME_ACTIVE_TITLE_TOP_COLOR;
137: }
138:
139: public static Color getFrameActiveTitleBottomColor() {
140: return FRAME_ACTIVE_TITLE_BOTTOM_COLOR;
141: }
142:
143: public static Color getFrameInactiveTitleTopColor() {
144: return FRAME_INACTIVE_TITLE_TOP_COLOR;
145: }
146:
147: public static Color getFrameInactiveTitleBottomColor() {
148: return FRAME_INACTIVE_TITLE_BOTTOM_COLOR;
149: }
150:
151: // =================================================================================================================
152: // Jide Color Methods
153:
154: public static Color getLighterColor(Color backColor) {
155: int r = getLighterColor(backColor.getRed());
156: int g = getLighterColor(backColor.getGreen());
157: int b = getLighterColor(backColor.getBlue());
158: if (r >= 255)
159: r = 255;
160: if (g >= 255)
161: g = 255;
162: if (b >= 255)
163: b = 255;
164: return new Color(r, g, b);
165: }
166:
167: public static int getLighterColor(int x) {
168: return (int) (x * 36.0 / 255.0 + 219.5);
169: }
170:
171: public static Color getMenuSelectionColor(Color backColor) {
172: int r = getMenuSelectionValue(backColor.getRed());
173: int g = getMenuSelectionValue(backColor.getGreen());
174: int b = getMenuSelectionValue(backColor.getBlue());
175: if (r >= 255)
176: r = 255;
177: if (g >= 255)
178: g = 255;
179: if (b >= 255)
180: b = 255;
181: return new Color(r, g, b);
182: }
183:
184: public static int getMenuSelectionValue(int x) {
185: return (int) (x * 76.0 / 255.0 + 179.5);
186: }
187:
188: public static Color getMenuBackgroundColor(Color color) {
189: if (Color.BLACK.equals(color)) {
190: return color;
191: } else if (Color.WHITE.equals(color)) {
192: return color;
193: }
194: int r = getMenuValue(color.getRed());
195: int g = getMenuValue(color.getGreen());
196: int b = getMenuValue(color.getBlue());
197: if (r >= 255)
198: r = 255;
199: if (g >= 255)
200: g = 255;
201: if (b >= 255)
202: b = 255;
203: return new Color(r, g, b);
204: }
205:
206: public static int getMenuValue(int x) {
207: return (int) (x * 36.0 / 255.0 + 219.5);
208: }
209:
210: public static Color getToolBarBackgroundColor(Color color) {
211: if (Color.BLACK.equals(color)) {
212: return color;
213: } else if (Color.WHITE.equals(color)) {
214: return color;
215: }
216: int r = getToolBarValue(color.getRed());
217: int g = getToolBarValue(color.getGreen());
218: int b = getToolBarValue(color.getBlue());
219: if (r >= 255)
220: r = 255;
221: if (g >= 255)
222: g = 255;
223: if (b >= 255)
224: b = 255;
225: return new Color(r, g, b);
226: }
227:
228: public static int getToolBarValue(int x) {
229: return (int) (x * 215 / 255 + 40);
230: }
231:
232: public static Color getGripperForegroundColor(Color backColor) {
233: int r = getGripperValue(backColor.getRed());
234: int g = getGripperValue(backColor.getGreen());
235: int b = getGripperValue(backColor.getBlue());
236: if (r >= 255)
237: r = 255;
238: if (g >= 255)
239: g = 255;
240: if (b >= 255)
241: b = 255;
242: return new Color(r, g, b);
243: }
244:
245: public static int getGripperValue(int x) {
246: if (x == 255) {
247: return 0;
248: } else if (x >= 0 && x <= 64) {
249: return (int) (x * 33 / 64 + 123);
250: } else {
251: return (int) ((x - 65) * 157 / 189 + 33);
252: }
253: }
254:
255: public static Color getDefaultBackgroundColor(Color backColor) {
256: Color backIDE;
257:
258: // Check for the 'Classic' control color
259: if ((backColor.getRed() == 212)
260: && (backColor.getGreen() == 208)
261: && (backColor.getBlue() == 200)) {
262: // Use the exact background for this color
263: backIDE = new Color(247, 243, 233);
264: } else if ((backColor.getRed() == 236)
265: && (backColor.getGreen() == 233)
266: && (backColor.getBlue() == 216)) {
267: // Check for the 'XP' control color
268: // Use the exact background for this color
269: backIDE = new Color(255, 251, 233);
270: } else {
271: // Calculate the IDE background color as only half as dark as the control color
272: int r = backColor.getRed() + 35;
273: int g = backColor.getGreen() + 35;
274: int b = backColor.getBlue() + 35;
275: if (r >= 255)
276: r = 255;
277: if (g >= 255)
278: g = 255;
279: if (b >= 255)
280: b = 255;
281: backIDE = new Color(r, g, b);
282: }
283:
284: return backIDE;
285: }
286:
287: final public static double RATIO1 = 0.67;
288: final public static double RATIO2 = 0.78;
289: final public static double RATIO3 = 0.86;
290:
291: final static Color DARK_GREEN = new Color(0, 128, 0);
292: final static Color DARK_MAGENTA = new Color(128, 0, 128);
293:
294: public static int getLightColor(int x, double ratio) {
295: return (int) ((255 - x) * ratio + x);
296: }
297:
298: public static Color getLighterColor(Color color, double ratio) {
299: if (DARK_GREEN.equals(color) || DARK_MAGENTA.equals(color)) {
300: return color;
301: }
302: int r = getLightColor(color.getRed(), ratio);
303: int g = getLightColor(color.getGreen(), ratio) + 1;
304: int b = getLightColor(color.getBlue(), ratio);
305: if (r >= 255)
306: r = 255;
307: if (g >= 255)
308: g = 255;
309: if (b >= 255)
310: b = 255;
311: return new Color(r, g, b);
312: }
313:
314: public static Color getHeighlightColor(Color bottomColor) {
315: float[] oHSB = new float[3];
316: Color.RGBtoHSB(bottomColor.getRed(), bottomColor.getGreen(),
317: bottomColor.getBlue(), oHSB);
318: oHSB[1] -= 0.07f;
319: if (oHSB[1] < 0)
320: oHSB[1] = 0f;
321: oHSB[2] += 0.04f;
322: if (oHSB[2] > 1)
323: oHSB[2] = 1f;
324: return Color.getHSBColor(oHSB[0], oHSB[1], oHSB[2]);
325: }
326:
327: public static Color getEmBaseColor(Color bottomColor) {
328: float[] oHSB = new float[3];
329: Color.RGBtoHSB(bottomColor.getRed(), bottomColor.getGreen(),
330: bottomColor.getBlue(), oHSB);
331: oHSB[1] += 0.1f;
332: if (oHSB[1] > 1)
333: oHSB[1] = 1f;
334: oHSB[2] -= 0.1f;
335: if (oHSB[2] < 0)
336: oHSB[2] = 0f;
337: return Color.getHSBColor(oHSB[0], oHSB[1], oHSB[2]);
338: }
339:
340: public static Color getTextColor(Color backgroundColor) {
341: float[] oHSB = new float[3];
342: Color.RGBtoHSB(backgroundColor.getRed(), backgroundColor
343: .getGreen(), backgroundColor.getBlue(), oHSB);
344: return (oHSB[2] > 0.53) ? UIDefaultsLookup
345: .getColor("controlText") : Color.WHITE;
346: }
347:
348: public static Color getSelectedAndFocusedButtonColor(Color backColor) {
349: return getLighterColor(backColor, RATIO1);
350: }
351:
352: public static Color getFocusedButtonColor(Color backColor) {
353: return getLighterColor(backColor, RATIO2);
354: }
355:
356: public static Color getSelectedButtonColor(Color backColor) {
357: return getLighterColor(backColor, RATIO3);
358: }
359: }
|