1 7 package com.sun.java.swing.plaf.nimbus; 8 9 import java.awt.Color ; 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 13 22 class DuoDerivedColor extends Color implements PropertyChangeListener { 23 private final Color color1, color2; 24 private final float midPoint; 25 private int argbValue; 26 27 DuoDerivedColor(Color color1, Color color2, float midPoint) { 28 super(0); 29 this.color1 = color1; 30 this.color2 = color2; 31 this.midPoint = midPoint; 32 if (color1 instanceof DerivedColor) 33 ((DerivedColor)color1).addPropertyChangeListener(this); 34 if (color2 instanceof DerivedColor) 35 ((DerivedColor)color2).addPropertyChangeListener(this); 36 rederiveColor(); 37 } 38 39 public void propertyChange(PropertyChangeEvent evt) { 40 rederiveColor(); 41 } 42 43 47 public void rederiveColor() { 48 int r = color1.getRed() + 49 (int) ((color2.getRed() - color1.getRed()) * midPoint + 0.5f); 50 int g = color1.getGreen() + 51 (int) ((color2.getGreen() - color1.getGreen()) * midPoint + 52 0.5f); 53 int b = color1.getBlue() + 54 (int) ((color2.getBlue() - color1.getBlue()) * midPoint + 0.5f); 55 int a = color1.getAlpha() + 56 (int) ((color2.getAlpha() - color1.getAlpha()) * midPoint + 57 0.5f); 58 argbValue = ((a & 0xFF) << 24) | 59 ((r & 0xFF) << 16) | 60 ((g & 0xFF) << 8) | 61 (b & 0xFF); 62 } 63 64 @Override 65 public boolean equals(Object o) { 66 if (this == o) return true; 67 if (!(o instanceof DuoDerivedColor)) return false; 68 DuoDerivedColor that = (DuoDerivedColor) o; 69 if (Float.compare(that.midPoint, midPoint) != 0) return false; 70 if (color1 != null ? !color1.equals(that.color1) : that.color1 != null) 71 return false; 72 if (color2 != null ? !color2.equals(that.color2) : that.color2 != null) 73 return false; 74 return true; 75 } 76 77 @Override 78 public int hashCode() { 79 int result = (color1 != null ? color1.hashCode() : 0); 80 result = 31 * result + (color2 != null ? color2.hashCode() : 0); 81 result = 31 * result + midPoint != +0.0f ? 82 Float.floatToIntBits(midPoint) : 0; 83 return result; 84 } 85 86 98 @Override public int getRGB() { 99 return argbValue; 100 } 101 102 105 static class UIResource extends DuoDerivedColor implements javax.swing.plaf.UIResource { 106 UIResource(Color c1, Color c2, float midPoint) { 107 super(c1, c2, midPoint); 108 } 109 } 110 } 111 | Popular Tags |