eirikbakke commented on PR #7930:
URL: https://github.com/apache/netbeans/pull/7930#issuecomment-2609964518
@neilcsmith-net Your main objection is with the fadeColor method that has a
hard-coded alpha value in it, right?
Is there any reason the alpha value cannot come from one of the
parameterized Color instances instead?
Here is a more general "blend" function that takes alpha values into account:
```
public static Color blendOver(final Color src, final Color dest, float
extraSourceAlpha) {
Preconditions.checkArgument(extraSourceAlpha >= 0.0f && extraSourceAlpha
<= 1.0f);
// Small optimization.
if (src.getAlpha() == 0 || extraSourceAlpha == 0.0f)
return dest;
/* Based on the formulas from the Porter and Duff paper, as presented in
the Javadoc for
java.awt.AlphaComposite. */
// "The 'extra' alpha component from the AlphaComposite instance"
final float Aac = extraSourceAlpha;
final float Csr[] = src.getRGBComponents(null);
final float Cdr[] = dest.getRGBComponents(null);
final float Asr = Csr[3];
final float Adr = Cdr[3];
final float As = Asr * Aac;
final float Ad = Adr;
// Use the functions for SRC_OVER.
final float Fs = 1.0f;
final float Fd = 1.0f - As;
final float Ar = As * Fs + Ad * Fd;
final float Adf = Ar;
// Avoid division by zero below.
if (Adf == 0.0f)
return new Color(0, 0, 0, 0);
final float Cdf[] = new float[3];
for (int i = 0; i < 3; i++) {
/* The zero check ensures correct behavior in the case of a zero-alpha
Color with NaN color
components. */
final float Cs = (As == 0.0f) ? 0.0f : Csr[i] * As;
final float Cd = (Ad == 0.0f) ? 0.0f : Cdr[i] * Ad;
final float Cr = Cs * Fs + Cd * Fd;
Cdf[i] = Cr / Ar;
}
return new Color(Cdf[0], Cdf[1], Cdf[2], Adf);
}
```
If useful, I could add it to org.openide.util.Utilities. I've been using it
for several years.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists