+ private static Color getDistinctForeground(RGB rgb) {
+ /* Calculate the relative luminance of the color, high value is bright */
+ final int luminanceThreshold = 128;
+ /* Relative luminance (Y) coefficients as defined in ITU.R Rec. 709 */
+ final double redCoefficient = 0.2126;
+ final double greenCoefficient = 0.7152;
+ final double blueCoefficient = 0.0722;
+ int luminance = (int) (redCoefficient * rgb.red + greenCoefficient * rgb.green + blueCoefficient * rgb.blue);
+ /* Use black over bright colors and white over dark colors */
+ return Display.getDefault().getSystemColor(
+ luminance > luminanceThreshold ? SWT.COLOR_BLACK : SWT.COLOR_WHITE);
+ }
+