6434110: Color constructor parameter name is misleading

Reviewed-by: prr, aivanov
This commit is contained in:
Sergey Bylokhov 2026-02-25 20:09:48 +00:00
parent 9d4fbbe36d
commit 36d67ffd01
2 changed files with 100 additions and 23 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -334,7 +334,7 @@ public class Color implements Paint, java.io.Serializable {
* The actual color used in rendering depends
* on finding the best match given the color space
* available for a given output device.
* Alpha is defaulted to 255.
* Alpha defaults to 255.
*
* @throws IllegalArgumentException if {@code r}, {@code g}
* or {@code b} are outside of the range
@ -378,12 +378,15 @@ public class Color implements Paint, java.io.Serializable {
}
/**
* Creates an opaque sRGB color with the specified combined RGB value
* consisting of the red component in bits 16-23, the green component
* in bits 8-15, and the blue component in bits 0-7. The actual color
* used in rendering depends on finding the best match given the
* color space available for a particular output device. Alpha is
* defaulted to 255.
* Creates an opaque sRGB color with the specified RGB value consisting of
* <ul>
* <li>the red component in bits 16-23,
* <li>the green component in bits 8-15, and
* <li>the blue component in bits 0-7.
* </ul>
* The actual color used in rendering depends on finding the best match
* given the color space available for a particular output device. Alpha
* defaults to 255.
*
* @param rgb the combined RGB components
* @see java.awt.image.ColorModel#getRGBdefault
@ -397,14 +400,17 @@ public class Color implements Paint, java.io.Serializable {
}
/**
* Creates an sRGB color with the specified combined RGBA value consisting
* of the alpha component in bits 24-31, the red component in bits 16-23,
* the green component in bits 8-15, and the blue component in bits 0-7.
* If the {@code hasalpha} argument is {@code false}, alpha
* is defaulted to 255.
* Creates an sRGB color with the specified ARGB value consisting of
* <ul>
* <li>the alpha component in bits 24-31,
* <li>the red component in bits 16-23,
* <li>the green component in bits 8-15, and
* <li>the blue component in bits 0-7.
* </ul>
* If the {@code hasAlpha} argument is {@code false}, alpha defaults to 255.
*
* @param rgba the combined RGBA components
* @param hasalpha {@code true} if the alpha bits are valid;
* @param argb the combined ARGB components
* @param hasAlpha {@code true} if the alpha bits are valid;
* {@code false} otherwise
* @see java.awt.image.ColorModel#getRGBdefault
* @see #getRed
@ -413,17 +419,17 @@ public class Color implements Paint, java.io.Serializable {
* @see #getAlpha
* @see #getRGB
*/
public Color(int rgba, boolean hasalpha) {
if (hasalpha) {
value = rgba;
public Color(int argb, boolean hasAlpha) {
if (hasAlpha) {
value = argb;
} else {
value = 0xff000000 | rgba;
value = 0xff000000 | argb;
}
}
/**
* Creates an opaque sRGB color with the specified red, green, and blue
* values in the range (0.0 - 1.0). Alpha is defaulted to 1.0. The
* values in the range (0.0 - 1.0). Alpha defaults to 1.0. The
* actual color used in rendering depends on finding the best
* match given the color space available for a particular output
* device.
@ -570,9 +576,14 @@ public class Color implements Paint, java.io.Serializable {
/**
* Returns the RGB value representing the color in the default sRGB
* {@link ColorModel}.
* (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are
* blue).
* {@link ColorModel}, consisting of
* <ul>
* <li>the alpha component in bits 24-31,
* <li>the red component in bits 16-23,
* <li>the green component in bits 8-15, and
* <li>the blue component in bits 0-7.
* </ul>
*
* @return the RGB value of the color in the default sRGB
* {@code ColorModel}.
* @see java.awt.image.ColorModel#getRGBdefault

View File

@ -0,0 +1,66 @@
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Color;
/**
* @test
* @bug 6434110
* @summary Verify Color(int, boolean) constructor uses ARGB bit layout
*/
public final class ColorARGBConstructorTest {
public static void main(String[] args) {
for (int argb : new int[]{0x00000000, 0x01020304, 0xC0903020,
0x40302010, 0xD08040C0, 0x80000000,
0x7FFFFFFF, 0xFFFFFFFF, 0xFF000000,
0x00FF0000, 0x0000FF00, 0x000000FF})
{
verify(argb, true);
verify(argb, false);
}
}
private static void verify(int argb, boolean hasAlpha) {
var c = new Color(argb, hasAlpha);
int expRGB = hasAlpha ? argb : (0xFF000000 | argb);
int expA = hasAlpha ? (argb >>> 24) : 0xFF;
int expR = (argb >> 16) & 0xFF;
int expG = (argb >> 8) & 0xFF;
int expB = argb & 0xFF;
check("RGB", expRGB, c.getRGB(), argb, hasAlpha);
check("Alpha", expA, c.getAlpha(), argb, hasAlpha);
check("Red", expR, c.getRed(), argb, hasAlpha);
check("Green", expG, c.getGreen(), argb, hasAlpha);
check("Blue", expB, c.getBlue(), argb, hasAlpha);
}
private static void check(String comp, int exp, int got, int argb,
boolean hasAlpha)
{
if (exp != got) {
throw new RuntimeException("0x%08X(%b) %s: 0x%08X != 0x%08X"
.formatted(argb, hasAlpha, comp, exp, got));
}
}
}