JDK-4774180 : Alpha value lost when calling Color.brighter() or Color.darker()
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 1.2.1,1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_2000
  • CPU: generic,x86
  • Submitted: 2002-11-05
  • Updated: 2021-02-11
  • Resolved: 2021-02-11
Related Reports
Duplicate :  
Duplicate :  
Description

Name: jk109818			Date: 11/05/2002


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-
b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OPERATING SYSTEM VERSION :
Windows 2000 Professional, Service
Pack 3

A DESCRIPTION OF THE PROBLEM :
When calling java.awt.Color.darker() or java.awt.Color.brighter()
on an instance with an alpha value other than 255, the resulting color will
have an alpha value of 255. This is clear when observing the part of the
source code for java.awt.Color:

-----------------------
public Color brighter() {
  int r
= getRed();
  int g = getGreen();
  int b = getBlue();

  /* From 2D
group:
   * 1. black.brighter() should return grey
   * 2. applying
brighter to blue will always return blue, brighter
   * 3. non pure color
(non zero rgb) will eventually return white
   */
  int i = (int)(1.0/(1.0-
FACTOR));
  if ( r == 0 && g == 0 && b == 0) {
     return new Color(i, i, i);
  }
  
if ( r > 0 && r < i ) r = i;
  if ( g > 0 && g < i ) g = i;
  if ( b > 0 && b < i ) b = i;

  return
new Color(Math.min((int)(r/FACTOR), 255),
                   
Math.min((int)(g/FACTOR), 255),
                   Math.min((int)(b/FACTOR),
255));
}
-----------------------
    public Color darker() {
	return new
Color(Math.max((int)(getRed()  *FACTOR), 0),
			 
Math.max((int)(getGreen()*FACTOR), 0),
			 
Math.max((int)(getBlue() *FACTOR), 0));
    }
-----------------------

The alpha value of
"this Color" is simply not applied on the Color created by the two methods.
I found out about this when calling
java.awt.Graphics2D.draw3DRect(...). I used a Color as constructed
using
  new Color(1.0f, 0.0f, 0.0f, 0.2f)
when drawing the 3D rect, but
found out the darker and brighter borders rendered were opaque instead of
0.2 times opaque.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Override javax.swing.JLabel.paint()
2. set the color to a
transparent red color: g.setColor(new Color(1.0f, 0.0f, 0.0f,
0.2f);
3. draw a 3D rect: g.draw3DRect(5, 5, 30, 30, true);

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected: darker and lighter borders for the 3D rect using the same alpha
value as the source Color.

Actual result: darker and lighter borders
for the 3D rect using an alpha value of 1.0f (or 255).


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class ColorAlphaBug extends javax.swing.JFrame {
  public ColorAlphaBug() {
    
super("Color alpha bug");
    getContentPane().add(new Label3DRect());
  }
  public static
void main(String[] args) {
    ColorAlphaBug instance = new ColorAlphaBug();
    
instance.setSize(100,100);
    instance.setVisible(true);
  }

  class Label3DRect
extends javax.swing.JLabel {
    public Label3DRect() {
      super();
    }
    public void
paint(java.awt.Graphics g) {
      java.awt.Graphics2D g2d = (java.awt.Graphics2D)g;
      
g2d.setColor(new java.awt.Color(1.0f,0.0f,0.0f,0.2f));
      
g2d.draw3DRect(5,5,60,60,true);
    }
  }
}

---------- END SOURCE ----------
(Review ID: 165897) 
======================================================================

Comments
EVALUATION Color.java is owned by 2D. ###@###.### 2002-11-05
05-11-2002