1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 23 import javax.swing.*; 24 import java.awt.*; 25 import java.awt.geom.Area ; 26 import java.awt.image.BufferedImage ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 36 final class ColorUtil { 37 private static Map <Integer , GradientPaint> gpCache = null; 38 private static Map <RenderingHints.Key, Object > hintsMap = null; 39 private static final boolean noGpCache = Boolean.getBoolean( 40 "netbeans.winsys.nogpcache"); private static final boolean noAntialias = 42 Boolean.getBoolean("nb.no.antialias"); 44 private static int focusedHeight = -1; 46 private static int unfocusedHeight = -1; 47 private static Icon unfocused = null; 49 private static Icon focused = null; 50 private static Rectangle scratch = new Rectangle(); 52 private static final int DEFAULT_IMAGE_WIDTH = 200; 56 60 public static final int SEL_TYPE = 1; 61 public static final int UNSEL_TYPE = 2; 62 public static final int FOCUS_TYPE = 4; 63 66 public static final int XP_REGULAR_TAB = 0; 67 public static final int XP_HIGHLIGHTED_TAB = 1; 68 71 public static final int XP_BORDER_RIGHT = 1; 72 public static final int XP_BORDER_BOTTOM = 2; 73 76 private static Icon XP_DRAG_IMAGE; 77 80 private static Icon VISTA_DRAG_IMAGE; 81 82 85 private ColorUtil() { 86 } 87 88 92 public static Color getMiddle(Color c1, Color c2) { 93 return new Color((c1.getRed() + c2.getRed()) / 2, 94 (c1.getGreen() + c2.getGreen()) / 2, 95 (c1.getBlue() + c2.getBlue()) / 2); 96 } 97 98 99 public static GradientPaint getGradientPaint(float x1, float y1, 100 Color upper, float x2, 101 float y2, Color lower) { 102 return getGradientPaint(x1, y1, upper, x2, y2, lower, false); 103 } 104 105 115 public static GradientPaint getGradientPaint(float x1, float y1, 116 Color upper, float x2, 117 float y2, Color lower, 118 boolean repeats) { 119 if (noGpCache) { 120 return new GradientPaint(x1, y1, upper, x2, y2, lower, repeats); 121 } 122 123 if (upper == null) { 125 upper = Color.BLUE; 126 } 127 128 if (lower == null) { 129 lower = Color.ORANGE; 130 } 131 132 if (gpCache == null) { 133 gpCache = new HashMap <Integer , GradientPaint>(20); 134 } 135 boolean horizontal = x1 == x2; 137 boolean vertical = y1 == y2; 138 if (horizontal && vertical) { 139 y1 = x1 + 28; 143 } else if (horizontal && !repeats) { 144 x1 = 0; 145 x2 = 0; 146 } else if (vertical && !repeats) { 147 y1 = 0; 148 y2 = 0; 149 } 150 152 long bits = Double.doubleToLongBits(x1) 154 + Double.doubleToLongBits(y1) * 37 + Double.doubleToLongBits( 155 x2) * 43 + Double.doubleToLongBits(y2) * 47; 156 int hash = ((((int) bits) ^ ((int) (bits >> 32))) 157 ^ upper.hashCode() ^ (lower.hashCode() * 17)) * (repeats ? 31 : 1); 158 159 Integer key = new Integer (hash); 160 GradientPaint result = (GradientPaint) gpCache.get(key); 161 if (result == null) { 162 result = 163 new GradientPaint(x1, y1, upper, x2, y2, lower, repeats); 164 if (gpCache.size() > 40) { 165 gpCache.clear(); 166 } 167 gpCache.put(key, result); 168 } 169 return result; 170 } 171 172 private static Map getHints() { 173 if (hintsMap == null) { 174 hintsMap = (Map <RenderingHints.Key, Object >)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); if (hintsMap == null) { 177 hintsMap = new HashMap <RenderingHints.Key, Object >(); 178 if (shouldAntialias()) { 179 hintsMap.put(RenderingHints.KEY_TEXT_ANTIALIASING, 180 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 181 } 182 } 183 if (shouldAntialias()) { 184 hintsMap.put(RenderingHints.KEY_ANTIALIASING, 185 RenderingHints.VALUE_ANTIALIAS_ON); 186 } 187 } 188 return hintsMap; 189 190 } 191 192 public static final void setupAntialiasing(Graphics g) { 193 if (noAntialias) return; 194 195 ((Graphics2D) g).addRenderingHints(getHints()); 196 } 197 198 private static final boolean antialias = Boolean.getBoolean( 199 "nb.cellrenderer.antialiasing") || ("GTK".equals(UIManager.getLookAndFeel().getID()) && gtkShouldAntialias()) || 202 Boolean.getBoolean ("swing.aatext") || "Aqua".equals(UIManager.getLookAndFeel().getID()); 204 205 public static final boolean shouldAntialias() { 206 return antialias; 207 } 208 209 private static final boolean gtkShouldAntialias() { 210 Object o = Toolkit.getDefaultToolkit().getDesktopProperty("gnome.Xft/Antialias"); return new Integer (1).equals(o); 212 } 213 214 public static boolean isBrighter(Color a, Color b) { 216 int[] ac = new int[]{a.getRed(), a.getGreen(), a.getBlue()}; 217 int[] bc = new int[]{b.getRed(), b.getGreen(), b.getBlue()}; 218 int dif = 0; 219 220 for (int i = 0; i < 3; i++) { 221 int currDif = ac[i] - bc[i]; 222 if (Math.abs(currDif) > Math.abs(dif)) { 223 dif = currDif; 224 } 225 } 226 return dif > 0; 227 } 228 229 private static int minMax(int i) { 230 if (i < 0) { 231 return 0; 232 } else if (i > 255) { 233 return 255; 234 } else { 235 return i; 236 } 237 } 238 239 public static int averageDifference(Color a, Color b) { 240 int[] ac = new int[]{a.getRed(), a.getGreen(), a.getBlue()}; 241 int[] bc = new int[]{b.getRed(), b.getGreen(), b.getBlue()}; 242 int dif = 0; 243 for (int i = 0; i < 3; i++) { 244 dif += bc[i] - ac[i]; 245 } 246 return dif / 3; 247 } 248 249 public static Color adjustComponentsTowards(Color toAdjust, Color towards) { 250 int r = toAdjust.getRed(); 251 int g = toAdjust.getGreen(); 252 int b = toAdjust.getBlue(); 253 254 int ra = towards.getRed(); 255 int ga = towards.getGreen(); 256 int ba = towards.getBlue(); 257 258 r += minMax((ra - r) / 3); 259 g += minMax((ga - g) / 3); 260 b += minMax((ba - b) / 3); 261 262 return new Color(r, g, b); 263 } 264 265 public static Color adjustTowards(Color toAdjust, int amount, 266 Color towards) { 267 int r = toAdjust.getRed(); 268 int g = toAdjust.getGreen(); 269 int b = toAdjust.getBlue(); 270 int factor = isBrighter(towards, toAdjust) ? 1 : -1; 271 r = minMax(r + (factor * amount)); 272 g = minMax(g + (factor * amount)); 273 b = minMax(b + (factor * amount)); 274 return new Color(r, g, b); 275 } 276 277 public static Color adjustBy(Color toAdjust, int amount) { 278 int r = minMax(toAdjust.getRed() + amount); 279 int g = minMax(toAdjust.getGreen() + amount); 280 int b = minMax(toAdjust.getBlue() + amount); 281 return new Color(r, g, b); 282 } 283 284 public static Color adjustBy(Color toAdjust, int[] amounts) { 285 int r = minMax(toAdjust.getRed() + amounts[0]); 286 int g = minMax(toAdjust.getGreen() + amounts[1]); 287 int b = minMax(toAdjust.getBlue() + amounts[2]); 288 return new Color(r, g, b); 289 } 290 291 294 private static float minMax(float f) { 295 return Math.max(0, Math.min(1, f)); 296 } 297 298 302 public static void paintViewTabBump(Graphics g, int x, int y, int width, 303 int height, int type) { 304 drawTexture(g, x, y, width, height, type, 0); 305 } 306 307 311 public static void paintDocTabBump(Graphics g, int x, int y, int width, 312 int height, int type) { 313 drawTexture(g, x, y, width, height, type, 2); 315 } 316 317 322 private static void _drawTexture(Graphics g, int x, int y, int width, 323 int height, int type, int yDecline) { 324 Color brightC = UIManager.getColor("TabbedPane.highlight"); 325 Color darkC; 326 if (type == FOCUS_TYPE) { 327 darkC = UIManager.getColor("TabbedPane.focus"); 328 } else { 329 darkC = UIManager.getColor("controlDkShadow"); 330 } 331 if (width % 2 != 0) { 334 width--; 335 } 336 if (height % 2 != 0) { 337 height--; 338 } 339 for (int curX = x; curX < x + width; curX++) { 340 g.setColor((curX - x) % 2 == 0 ? brightC : darkC); 341 for (int curY = y + ((curX - x + yDecline) % 4); curY < y + height; curY += 342 4) { 343 g.drawLine(curX, curY, curX, curY); 344 } 345 } 346 } 347 348 355 private static void drawTexture(Graphics g, int x, int y, int width, 356 int height, int type, int yDecline) { 357 358 359 if (!g.hitClip(x, y, width, height)) { 360 return; 361 } 362 if (type == FOCUS_TYPE) { 363 if (focused == null || height > focusedHeight * 2) { 364 Image img = createBitmap(height, type, yDecline); 366 focusedHeight = height; 370 371 focused = new ImageIcon(img); 372 } 373 blitBitmap(g, focused, x, y, width, height); 374 375 } else { 376 if (unfocused == null || unfocusedHeight > height * 2) { 377 Image img = createBitmap(height, type, yDecline); 379 unfocusedHeight = height; 383 unfocused = new ImageIcon(img); 384 } 385 blitBitmap(g, unfocused, x, y, width, height); 386 } 387 } 388 389 395 private static BufferedImage createBitmap(int height, int type, 396 int yDecline) { 397 398 BufferedImage result = GraphicsEnvironment.getLocalGraphicsEnvironment() 400 .getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage( 401 200, height * 2); 402 403 Graphics g = result.getGraphics(); 404 405 if (result.getAlphaRaster() == null) { 406 Color c = type == FOCUS_TYPE ? 407 MetalViewTabDisplayerUI.getActBgColor() : 408 MetalViewTabDisplayerUI.getInactBgColor(); 409 g.setColor(c); 410 g.fillRect(0, 0, DEFAULT_IMAGE_WIDTH, height * 2); 411 } 412 413 _drawTexture(g, 0, 0, DEFAULT_IMAGE_WIDTH, height * 2, type, yDecline); 415 return result; 416 } 417 418 423 private static void blitBitmap(Graphics g, Icon icon, int x, int y, int w, 424 int h) { 425 Shape clip = g.getClip(); 427 428 if (clip == null) { 429 g.setClip(x, y, w, h); 431 } else { 432 435 scratch.setBounds(x, y, w, h); 436 Area area = new Area (clip); 437 area.intersect(new Area (scratch)); 438 g.setClip(area); 439 } 440 int iwidth = icon.getIconWidth(); 441 int widthPainted = 0; 442 while (widthPainted < w) { 443 icon.paintIcon(null, g, x + widthPainted, y); 445 widthPainted += iwidth; 446 } 447 448 g.setClip(clip); 450 } 451 452 455 public static void paintXpTabHeader(int type, Graphics g, int x, int y, 456 int width) { 457 Color capBorderC = getXpHeaderColor(type, false); 458 Color capFillC = getXpHeaderColor(type, true); 459 g.setColor(capBorderC); 461 g.drawLine(x + 2, y, x + width - 3, y); 462 g.drawLine(x + 2, y, x, y + 2); 463 g.drawLine(x + width - 3, y, x + width - 1, y + 2); 464 g.setColor(capFillC); 465 g.drawLine(x + 2, y + 1, x + width - 3, y + 1); 466 g.drawLine(x + 1, y + 2, x + width - 2, y + 2); 467 } 470 471 474 public static void xpFillRectGradient(Graphics2D g, Rectangle rect, 475 Color brightC, Color darkC) { 476 xpFillRectGradient(g, rect.x, rect.y, rect.width, rect.height, brightC, 477 darkC, XP_BORDER_BOTTOM | XP_BORDER_RIGHT); 478 } 479 480 484 public static void xpFillRectGradient(Graphics2D g, int x, int y, 485 int width, int height, Color brightC, 486 Color darkC) { 487 xpFillRectGradient(g, x, y, width, height, brightC, darkC, 488 XP_BORDER_BOTTOM | XP_BORDER_RIGHT); 489 } 490 491 495 public static void xpFillRectGradient(Graphics2D g, int x, int y, 496 int width, int height, Color brightC, 497 Color darkC, int borderType) { 498 paintXpGradientBorder(g, x, y, width, height, darkC, borderType); 499 int gradWidth = ((borderType & XP_BORDER_RIGHT) != 0) ? 500 width - 2 : width; 501 int gradHeight = ((borderType & XP_BORDER_BOTTOM) != 0) ? 502 height - 2 : height; 503 paintXpGradientFill(g, x, y, gradWidth, gradHeight, brightC, darkC); 504 } 505 506 509 public static void paintXpTabDragTexture(Component control, Graphics g, 510 int x, int y, int height) { 511 if (XP_DRAG_IMAGE == null) { 512 XP_DRAG_IMAGE = initXpDragTextureImage(); 513 } 514 int count = height / 4; 515 int ypos = y; 516 for (int i = 0; i < count; i++) { 517 XP_DRAG_IMAGE.paintIcon(control, g, x, ypos); 518 ypos += 4; 519 } 520 } 521 522 526 public static void vistaFillRectGradient(Graphics2D g, Rectangle rect, 527 Color brightUpperC, Color darkUpperC, 528 Color brightLowerC, Color darkLowerC) { 529 vistaFillRectGradient(g, rect.x, rect.y, rect.width, rect.height, 530 brightUpperC, darkUpperC, brightLowerC, darkLowerC); 531 } 532 533 537 public static void vistaFillRectGradient(Graphics2D g, int x, int y, 538 int width, int height, 539 Color brightUpperC, Color darkUpperC, 540 Color brightLowerC, Color darkLowerC) { 541 paintVistaGradientFill( g, x, y, width, height/2, 542 brightUpperC, darkUpperC ); 543 paintVistaGradientFill( g, x, y+height/2, width, height-height/2, 544 brightLowerC, darkLowerC ); 545 } 546 547 551 public static void vistaFillRectGradient(Graphics2D g, Rectangle rect, 552 Color upperC, 553 Color brightLowerC, Color darkLowerC) { 554 vistaFillRectGradient( g, rect.x, rect.y, rect.width, rect.height, 555 upperC, brightLowerC, darkLowerC ); 556 } 557 561 public static void vistaFillRectGradient(Graphics2D g, int x, int y, 562 int width, int height, 563 Color upperC, 564 Color brightLowerC, Color darkLowerC) { 565 g.setColor( upperC ); 566 g.fillRect( x, y, width, height/2 ); 567 paintVistaGradientFill( g, x, y+height/2, width, height-height/2, 568 brightLowerC, darkLowerC ); 569 } 570 571 574 public static void paintVistaTabDragTexture(Component control, Graphics g, 575 int x, int y, int height) { 576 if (VISTA_DRAG_IMAGE == null) { 577 VISTA_DRAG_IMAGE = initVistaDragTextureImage(); 578 } 579 int count = height / 4; 580 int ypos = y; 581 g.setColor( Color.WHITE ); 582 for (int i = 0; i < count; i++) { 583 VISTA_DRAG_IMAGE.paintIcon(control, g, x, ypos); 584 g.drawLine( x+1, ypos+2, x+2, ypos+2 ); 585 g.drawLine( x+2, ypos+1, x+2, ypos+1 ); 586 ypos += 4; 587 } 588 } 589 595 public static Color adjustColor(Color c, int rDiff, int gDiff, int bDiff) { 596 if (c == null) { 597 c = Color.GRAY; 598 } 599 int red = Math.max(0, Math.min(255, c.getRed() + rDiff)); 600 int green = Math.max(0, Math.min(255, c.getGreen() + gDiff)); 601 int blue = Math.max(0, Math.min(255, c.getBlue() + bDiff)); 602 return new Color(red, green, blue); 603 } 604 605 608 private static void paintXpGradientBorder(Graphics g, int x, int y, 609 int width, int height, 610 Color darkC, int borderType) { 611 if ((borderType & XP_BORDER_RIGHT) != 0) { 613 Color color = adjustColor(darkC, -6, -5, -3); 614 g.setColor(color); 615 g.drawLine(x + width - 2, y, x + width - 2, y + height - 2); 616 color = adjustColor(darkC, -27, -26, -20); 617 g.setColor(color); 618 g.drawLine(x + width - 1, y, x + width - 1, y + height - 1); 619 } 620 if ((borderType & XP_BORDER_BOTTOM) != 0) { 621 Color color = adjustColor(darkC, -6, -5, -3); 622 g.setColor(color); 623 g.drawLine(x, y + height - 2, x + width - 2, y + height - 2); 624 color = adjustColor(darkC, -27, -26, -20); 625 g.setColor(color); 626 g.drawLine(x, y + height - 1, x + width - 1, y + height - 1); 627 } 628 } 629 630 633 private static void paintXpGradientFill(Graphics2D g, int x, int y, 634 int width, int height, 635 Color brightC, Color darkC) { 636 GradientPaint gradient = getGradientPaint(x, y, brightC, x, y + height, 637 darkC); 638 g.setPaint(gradient); 639 g.fillRect(x, y, width, height); 640 } 641 642 645 private static Color getXpHeaderColor(int type, boolean fill) { 646 String colorKey = null; 647 switch (type) { 648 case XP_REGULAR_TAB: 649 colorKey = fill ? "tab_unsel_fill_bright" : "tab_border"; 650 break; 651 case XP_HIGHLIGHTED_TAB: 652 colorKey = fill ? 653 "tab_highlight_header_fill" : "tab_highlight_header"; 654 break; 655 default: 656 throw new IllegalArgumentException ( 657 "Unknown type of tab header: " + type); 658 } 659 return UIManager.getColor(colorKey); 660 } 661 662 665 private static final Icon initXpDragTextureImage() { 666 BufferedImage i = new BufferedImage (3, 3, BufferedImage.TYPE_INT_RGB); 667 Color hl = UIManager.getColor("controlLtHighlight"); i.setRGB(2, 2, hl.getRGB()); 669 i.setRGB(2, 1, hl.getRGB()); 670 i.setRGB(1, 2, hl.getRGB()); 671 Color dk = UIManager.getColor("TabbedPane.darkShadow"); i.setRGB(1, 1, dk.getRGB()); 673 Color corners = UIManager.getColor("TabbedPane.light"); i.setRGB(0, 2, corners.getRGB()); 675 i.setRGB(2, 0, corners.getRGB()); 676 Color dk2 = UIManager.getColor("TabbedPane.shadow"); i.setRGB(0, 1, dk2.getRGB()); 678 i.setRGB(1, 0, dk2.getRGB()); 679 Color up = UIManager.getColor("inactiveCaptionBorder"); i.setRGB(0, 0, up.getRGB()); 681 return new ImageIcon(i); 682 } 683 684 687 private static void paintVistaGradientFill(Graphics2D g, int x, int y, 688 int width, int height, 689 Color brightC, Color darkC) { 690 GradientPaint gradient = getGradientPaint(x, y, brightC, x, y + height, 691 darkC); 692 g.setPaint(gradient); 693 g.fillRect(x, y, width, height); 694 } 695 696 699 private static final Icon initVistaDragTextureImage() { 700 BufferedImage i = new BufferedImage (2, 2, BufferedImage.TYPE_INT_RGB); 701 int grey = new Color(124,124,124).getRGB(); 702 i.setRGB(1, 0, grey); 703 i.setRGB(0, 1, grey); 704 i.setRGB(0, 0, new Color(162,163,164).getRGB()); 705 i.setRGB(1, 1, new Color(107,107,107).getRGB()); 706 return new ImageIcon(i); 707 } 708 709 public boolean isBlueprintTheme() { 710 return ("blueprint".equals( Toolkit.getDefaultToolkit().getDesktopProperty( 712 "gnome.Net/ThemeName"))); } 714 } 715 | Popular Tags |