下面列出了java.awt.Color#getRGB ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
@Override
public Color operateColor(Color color) {
if (color.getRGB() == 0) {
return color;
}
int red, green, blue;
int v = color.getRed();
v = v - (v % redMod) + redOffset;
red = Math.min(Math.max(v, 0), 255);
v = color.getGreen();
v = v - (v % greenMod) + greenOffset;
green = Math.min(Math.max(v, 0), 255);
v = color.getBlue();
v = v - (v % blueMod) + blueOffset;
blue = Math.min(Math.max(v, 0), 255);
Color mappedColor = new Color(red, green, blue);
countColor(mappedColor);
return mappedColor;
}
/**
* Returns the next color for a given object.
*
* @param object The object whose next color is determined.
*
* @return The next color of the object.
*/
public Color next(final T object) {
if (!m_objects.containsKey(object)) {
m_objects.put(object, Color.GREEN);
return Color.GREEN;
}
final Color oldColor = m_objects.get(object);
final int newLowest = Math.min((oldColor.getRGB() & 0xFF) + 0x40, 0xFF) & 0xFF;
final int newMiddle = Math.min((oldColor.getRGB() & 0xFF0000) + 0x400000, 0xFF0000) & 0xFF0000;
final Color newColor = new Color((oldColor.getRGB() & 0x00FF00) + newMiddle + newLowest);
if (newColor == Color.WHITE) {
m_objects.remove(object);
} else {
m_objects.put(object, newColor);
}
return newColor;
}
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
void check(Color c) {
SwingUtilities.updateComponentTreeUI(label);
label.paint(g);
if (label.getBackground().getRGB() != c.getRGB()) {
System.err.println("Color mismatch!");
System.err.println(" found: " + label.getBackground());
System.err.println(" expected: " + c);
throw new RuntimeException("Test failed");
}
}
/** Creates a <CODE>java.awt.Image</CODE>. This image only
* contains the bars without any text.
* @param foreground the color of the bars
* @param background the color of the background
* @return the image
*/
public java.awt.Image createAwtImage(Color foreground, Color background) {
int f = foreground.getRGB();
int g = background.getRGB();
Canvas canvas = new Canvas();
String bCode = keepNumbers(code);
if (generateChecksum)
bCode += getChecksum(bCode);
int len = bCode.length();
int nn = (int)n;
int fullWidth = len * (3 + 2 * nn) + (6 + nn );
byte bars[] = getBarsInter25(bCode);
boolean print = true;
int ptr = 0;
int height = (int)barHeight;
int pix[] = new int[fullWidth * height];
for (int k = 0; k < bars.length; ++k) {
int w = (bars[k] == 0 ? 1 : nn);
int c = g;
if (print)
c = f;
print = !print;
for (int j = 0; j < w; ++j)
pix[ptr++] = c;
}
for (int k = fullWidth; k < pix.length; k += fullWidth) {
System.arraycopy(pix, 0, pix, k, fullWidth);
}
Image img = canvas.createImage(new MemoryImageSource(fullWidth, height, pix, 0, fullWidth));
return img;
}
/**
* Draws a rectangle in the image. It�s useful for debugging purposes.
* @param x rect�s start position in x-axis
* @param y rect�s start positioj in y-axis
* @param w rect�s width
* @param h rect�s height
* @param c rect�s color
*/
public void drawRect(int x, int y, int w, int h, Color c){
int color = c.getRGB();
for(int i=x; i<x+w; i++){
setIntColor(i, y, color);
setIntColor(i, y+(h-1), color);
}
for(int i=y; i<y+h; i++){
setIntColor(x, i, color);
setIntColor(x+(w-1), i, color);
}
}
void check(Color c) {
SwingUtilities.updateComponentTreeUI(label);
label.paint(g);
if (label.getBackground().getRGB() != c.getRGB()) {
System.err.println("Color mismatch!");
System.err.println(" found: " + label.getBackground());
System.err.println(" expected: " + c);
throw new RuntimeException("Test failed");
}
}
/**
* DOCUMENT ME!
*
* @param c DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
private Color unwrap(Color c) {
if (c instanceof UIResource) {
return new Color(c.getRGB());
}
return c;
}
private Color unwrap(Color c) {
if (c instanceof UIResource) {
return new Color(c.getRGB());
}
return c;
}
private Color unwrap(Color c) {
if (c instanceof UIResource) {
return new Color(c.getRGB());
}
return c;
}
public ColorUIResource(Color c) {
super(c.getRGB(), (c.getRGB() & 0xFF000000) != 0xFF000000);
}
private static Color halfAlpha(Color color) {
int rgb = color.getRGB() & 0xffffff;
int alpha = color.getAlpha() / 2;
return new Color(rgb | (alpha << 24), true);
}
protected Expression instantiate(Object oldInstance, Encoder out) {
Color color = (Color) oldInstance;
Object[] args = new Object[] {color.getRGB()};
return new Expression(color, ColorUIResource.class, "new", args);
}
private Color unwrap(Color c) {
if (c instanceof UIResource) {
return new Color(c.getRGB());
}
return c;
}
public ColorUIResource(Color c) {
super(c.getRGB(), (c.getRGB() & 0xFF000000) != 0xFF000000);
}
/**
* Compare two images and return an image that shows the differences and the
* similarity between them, as a percent.
*/
public static ImageCompareResult compare(BufferedImage templateImage, BufferedImage imageToCompare, Double maxPercentColorDistance, Color ignoredPixelsColor) {
try {
if (maxPercentColorDistance == null) {
maxPercentColorDistance = 0.15;
}
BufferedImage diffImage = new BufferedImage(
Math.min(templateImage.getWidth(), imageToCompare.getWidth()),
Math.min(templateImage.getHeight(), imageToCompare.getHeight()),
BufferedImage.TYPE_3BYTE_BGR);
double maxColorDistance = Math.sqrt(
Math.pow(255, 2)
+ Math.pow(255, 2)
+ Math.pow(255, 2));
long diffPixelCount = 0;
int red = Color.RED.getRGB();
int gray = Color.LIGHT_GRAY.getRGB();
int reallyLightGray = Color.decode("#FAFAFA").getRGB();
for (int x = 0; x < diffImage.getWidth(); x++) {
for (int y = 0; y < diffImage.getHeight(); y++) {
int color1 = templateImage.getRGB(x, y);
int red1 = (color1 >> 16) & 0x000000FF;
int green1 = (color1 >> 8) & 0x000000FF;
int blue1 = (color1) & 0x000000FF;
int color2 = imageToCompare.getRGB(x, y);
int red2 = (color2 >> 16) & 0x000000FF;
int green2 = (color2 >> 8) & 0x000000FF;
int blue2 = (color2) & 0x000000FF;
if (ignoredPixelsColor != null) {
int ignoredColor = ignoredPixelsColor.getRGB();
if (color1 == ignoredColor || color2 == ignoredColor) {
diffImage.setRGB(x, y, reallyLightGray);
continue;
}
}
double colorDistance = Math.sqrt(
Math.pow(red1 - red2, 2)
+ Math.pow(green1 - green2, 2)
+ Math.pow(blue1 - blue2, 2));
double percentDistance = colorDistance / maxColorDistance;
// Make new color of the pixel either red or light gray,
// depending on how different the actual screen capture was
// from the reference image.
if (percentDistance >= maxPercentColorDistance) {
diffPixelCount++;
diffImage.setRGB(x, y, red);
} else if (percentDistance > 0.01) {
diffImage.setRGB(x, y, gray);
} else {
diffImage.setRGB(x, y, reallyLightGray);
}
}
}
double similarity = 1 - ((double) diffPixelCount / (diffImage.getWidth() * diffImage.getHeight()));
return new ImageCompareResult(diffImage, similarity, diffPixelCount);
} catch (Exception exc) {
throw new RuntimeException(exc);
}
}
/**
* Constructs a {@code ColorUIResource}.
* @param c the color
*/
public ColorUIResource(Color c) {
super(c.getRGB(), (c.getRGB() & 0xFF000000) != 0xFF000000);
}
/**
* Constructs a {@code ColorUIResource}.
* @param c the color
*/
public ColorUIResource(Color c) {
super(c.getRGB(), (c.getRGB() & 0xFF000000) != 0xFF000000);
}