Skip to content

Commit 3a174d1

Browse files
committed
Use HSLColor for a correct lighter and darker implementation
The existing lighter and darker implementation scales RGB values which can lead to color channel saturation (i.e., r, g, or b is 0 or 255). Using HSL color space avoids this.
1 parent 5fcd8a0 commit 3a174d1

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

org.eclipse.draw2d/src/org/eclipse/draw2d/FigureUtilities.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.eclipse.swt.graphics.GC;
2323
import org.eclipse.swt.widgets.Shell;
2424

25+
import org.eclipse.draw2d.colors.HSLColor;
2526
import org.eclipse.draw2d.geometry.Dimension;
2627
import org.eclipse.draw2d.geometry.Rectangle;
2728
import org.eclipse.draw2d.internal.InternalDraw2dUtils;
@@ -31,7 +32,6 @@
3132
*/
3233
public class FigureUtilities {
3334

34-
private static final float RGB_VALUE_MULTIPLIER = 0.6f;
3535
private static GC gc;
3636
private static Font appliedFont;
3737
private static FontMetrics metrics;
@@ -45,8 +45,7 @@ public class FigureUtilities {
4545
* @since 2.0
4646
*/
4747
public static Color darker(Color color) {
48-
return new Color(null, (int) (color.getRed() * RGB_VALUE_MULTIPLIER),
49-
(int) (color.getGreen() * RGB_VALUE_MULTIPLIER), (int) (color.getBlue() * RGB_VALUE_MULTIPLIER));
48+
return HSLColor.fromColor(color).darker(0.4).toColor();
5049
}
5150

5251
/**
@@ -210,13 +209,7 @@ public static int getTextWidth(String s, Font f) {
210209
* @since 2.0
211210
*/
212211
public static Color lighter(Color rgb) {
213-
int r = rgb.getRed();
214-
int g = rgb.getGreen();
215-
int b = rgb.getBlue();
216-
217-
return new Color(null, Math.max(2, Math.min((int) (r / RGB_VALUE_MULTIPLIER), 255)),
218-
Math.max(2, Math.min((int) (g / RGB_VALUE_MULTIPLIER), 255)),
219-
Math.max(2, Math.min((int) (b / RGB_VALUE_MULTIPLIER), 255)));
212+
return HSLColor.fromColor(rgb).lighter(0.4).toColor();
220213
}
221214

222215
/**

0 commit comments

Comments
 (0)