As it was said in comments - I doubt that this particular function is bottleneck. But still you can optimize it.
If you go deeper and de-compile getBrightness function, you will see that it is actually converts R,G,B values to HSB and then returns only B. It uses com.sun.javafx.util.Utils.RGBtoHSB function for that
By going further, in com.sun.javafx.util.Utils.RGBtoHSB we will see that to calculate brightness, you need to pick maximal value from R,G,B
double cmax = (r > g) ? r : g;
if (b > cmax) cmax = b;
brightness = cmax;
so, we can either re-use this knowledge, like that (no array allocation, useless calculations of hue/saturation):
if ((clr.getRed() > cutoff) || (clr.getGreen() > cutoff) || (clr.getBlue() > cutoff)) {
return Color.WHITE;
} else {
return Color.BLACK;
}
or optimize it further (to eliminate if statements):
private static final Color[] BW_VALUES = {Color.BLACK, Color.WHITE};
public static Color convertToBW_1(final Color clr, final double cutoff) {
/**
* rd will be 0, if red channel under cutoff, ie black. rd will be 1, if red channel is above cutoff, ie white
*/
final byte rd = (byte) (Double.doubleToRawLongBits(cutoff - clr.getRed()) >>> 63);
final byte gd = (byte) (Double.doubleToRawLongBits(cutoff - clr.getGreen()) >>> 63);
final byte bd = (byte) (Double.doubleToRawLongBits(cutoff - clr.getBlue()) >>> 63);
/**
* if at least one value is 1, then return white.
*/
return BW_VALUES[rd | gd | bd];
}
According to my quick measurements - this variant 2 times faster, but your mileage may vary